clSettings.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 ## @package clSettings
00022 # Settings Module
00023 
00024 import functions
00025 import ConfigParser, os
00026 
00027 ## Main config class
00028 # holds functions to access config file
00029 class settings():
00030     ## The Constructor
00031     # @param self The Object pointer
00032     # @param parent Pyjama Object Pointer
00033     def __init__(self, parent=None):
00034         ## Pointer to Pyjama
00035         self.parent = parent
00036         home = functions.preparedirs()
00037         install_dir = functions.install_dir()
00038 
00039         ## stores the config file URI
00040         self.home_config = os.path.join(home, 'pyjama.cfg')
00041 
00042         ## Parser Object Pointer
00043         self.config = ConfigParser.SafeConfigParser()
00044         self.config.readfp(open(os.path.join(install_dir, 'pyjama.cfg')))
00045         self.config.read([self.home_config])
00046 
00047         ## List holding options from pyjama.cfg
00048         self.options = {}
00049 
00050         sections = self.config.sections()
00051         for section in sections:
00052             items = self.config.items(section)
00053             for item, value in items:
00054                 pos = value.find("#")
00055                 pos = value.find(";")
00056                 if pos > -1:
00057                     value = value[0:pos]
00058                 if value.isdigit():
00059                     self.options[item] = int(value)
00060                 elif (self.isbool(value)):
00061                     self.options[item] = self.parsebool(value)
00062                 else:
00063                     self.options[item] = value
00064 
00065     ## Tests whether a section exists or not
00066     # @param self The Object pointer
00067     # @param self The Object pointer
00068     # @param section A string
00069     # @return bool
00070     def section_exists(self, section):
00071         return self.config.has_section(section)
00072 
00073     ## Tests whether an option exists or not
00074     # @param self The Object pointer
00075     # @param self The Object Pointer
00076     # @param section The section in which the option should be found as string
00077     # @param option The option to test for as string
00078     # @return bool
00079     def option_exists(self, section, option):
00080         return self.config.has_option(section, option)
00081 
00082     ## Set a option's value and write it to pyjama.cfg
00083     # @param self The Object pointer
00084     # @param section The section as string
00085     # @param option The option as string
00086     # @param value The value to set as string
00087     # @param func A function to run over the value
00088     # before it is written to config
00089     # @return bool
00090     def set_value(self, section, option, value, func=None):
00091         if not self.section_exists(section):
00092             self.config.add_section(section)
00093         if func is not None:
00094             self.config.set(section, option, str(func(value)))
00095         else:
00096             self.config.set(section, option, str(value))
00097         self.write_config()
00098 
00099     ## Reads a value from an option
00100     # @param self The Object pointer
00101     # @param section A section as string
00102     # @param option An option as string
00103     # @param default A default value
00104     # @param func A function to run over the value
00105     # read out - think of float conversions etc.
00106     # @return The option's value or default if section or option do not exist
00107     def get_value(self, section, option, default=None, func=None):
00108         if not self.section_exists(section): 
00109             if self.parent is not None:
00110                 if self.parent.debug:
00111                     print ("No section '%s:%s': returning default value (%s)" % (section, option, str(default)))
00112             if func is None:
00113                 return default
00114             else:
00115                 return func(default)
00116         if not self.option_exists(section, option): 
00117             if self.parent is not None:
00118                 if self.parent.debug:
00119                     print ("No option '%s:%s': returning default value (%s)" % (section, option, str(default)))
00120             if func is None:
00121                 return default
00122             else:
00123                 return func(default)
00124         value = self.config.get(section, option)
00125         pos = value.find("#")
00126         pos = value.find(";")
00127         if pos > -1:
00128             value = value[0:pos]
00129         if value.isdigit():
00130             ret = int(value)
00131         elif (self.isbool(value)):
00132             ret = self.parsebool(value)
00133         else:
00134             ret = value
00135 
00136         if func is None:
00137             return ret
00138         else:
00139             return func(ret)
00140 
00141     ## Removes an option from pyjama.cfg
00142     # @param self The Object pointer
00143     # @param section The option's section
00144     # @param option The option to delete
00145     # @return bool
00146     def remove_option(self, section, option):
00147         ret = self.config.remove_option(section, option)
00148         self.write_config()
00149         return ret
00150 
00151     ## Writes pyjama.cfg to disc
00152     # In most cases this is done by set_value automatically
00153     # @param self The Object pointer
00154     # @return None
00155     def write_config(self):
00156         if not functions.is_writeable(self.home_config):
00157             try:
00158                 self.parent.Events.raise_event("error", "No write access for %s" % self.home_config)
00159             except:
00160                 print "No write access for %s" % self.home_config
00161             return
00162         fh = open(self.home_config,"w")
00163         if fh:
00164             self.config.write(fh)    
00165         else:
00166             if self.parent:
00167                 self.parent.Events.raise_event("error", "Error writing configuration to %s" % self.home_config)
00168             else:
00169                 print "Error writing configuration to %s" % self.home_config
00170         fh.close()
00171 
00172     ## Tests if a string contains a boolean
00173     # @param self The Object pointer
00174     # @param string The strint to test
00175     # @return bool
00176     def isbool(self, string):
00177         if string.upper() == "TRUE" or string.upper() == "FALSE":
00178             return True
00179         return False
00180 
00181     ## Return the boolean value a string contains
00182     # @param self The Object pointer
00183     # @return bool
00184     def parsebool(self, string):
00185         return string[0].upper()=="T"
00186 
00187 
00188 
00189 
00190 
00191 #x = config.get("JAMENDO", "CACHING_TIME_SHORT") 
00192 
00193 #getint 
00194 #getfloat
00195 #getboolean

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8