Classes:PluginConfig

From OpenLP

Jump to: navigation, search
  1. # -*- coding: utf-8 -*-
  2. # vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
  3. """
  4. OpenLP - Open Source Lyrics Projection
  5. Copyright (c) 2008 Raoul Snyman
  6. Portions copyright (c) 2008 Martin Thompson, Tim Bentley
  7.  
  8. This program is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free Software
  10. Foundation; version 2 of the License.
  11.  
  12. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  14. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License along with
  17. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. Place, Suite 330, Boston, MA 02111-1307 USA
  19. """
  20.  
  21. import os
  22. from openlp.core.utils import ConfigHelper
  23.  
  24. class PluginConfig(object):
  25.     """
  26.     This is a generic config helper for plugins.
  27.     """
  28.     def __init__(self, plugin_name):
  29.         """
  30.         Initialise the plugin config object, setting the section name to the
  31.         plugin name.
  32.         """
  33.         self.section = plugin_name.lower()
  34.  
  35.     def get_config(self, key, default=None):
  36.         """
  37.         Get a configuration value from the configuration registry.
  38.         """
  39.         return ConfigHelper.get_config(self.section, key, default)
  40.  
  41.     def set_config(self, key, value):
  42.         """
  43.         Set a configuration value in the configuration registry.
  44.         """
  45.         return ConfigHelper.set_config(self.section, key, value)
  46.  
  47.     def get_data_path(self):
  48.         app_data = ConfigHelper.get_data_path()
  49.         safe_name = self.section.replace(' ', '-')
  50.         plugin_data = self.get_config('data path', safe_name)
  51.         path = os.path.join(app_data, plugin_data)
  52.  
  53.         if not os.path.exists(path):
  54.             os.makedirs(path)
  55.  
  56.         return path
  57.  
  58.     def set_data_path(self, path):
  59.         return self.set_config('data path', os.path.basename(path))
  60.  
  61.     def get_files(self, default_suffixes=None):
  62.         returnfiles = []
  63.         suffix = self.get_config("suffix name", default_suffixes)
  64.         try:
  65.             files = os.listdir(self.get_data_path())
  66.         except:
  67.             return returnfiles
  68.         if suffix != None:
  69.             for f in files:
  70.                 if f.find('.') != -1:
  71.                     nme = f.split('.')
  72.                     bname = nme[0]
  73.                     sfx = nme[1].lower()
  74.                     sfx = sfx.lower()
  75.                     if suffix.find(sfx) > -1 : # only load files with the correct suffix
  76.                         returnfiles.append(f)
  77.             return returnfiles
  78.         else:
  79.             return files  # no filtering required
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox