clSettings.py
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 import functions
00025 import ConfigParser, os
00026
00027
00028
00029 class settings():
00030
00031
00032
00033 def __init__(self, parent=None):
00034
00035 self.parent = parent
00036 home = functions.preparedirs()
00037 install_dir = functions.install_dir()
00038
00039
00040 self.home_config = os.path.join(home, 'pyjama.cfg')
00041
00042
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
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
00066
00067
00068
00069
00070 def section_exists(self, section):
00071 return self.config.has_section(section)
00072
00073
00074
00075
00076
00077
00078
00079 def option_exists(self, section, option):
00080 return self.config.has_option(section, option)
00081
00082
00083
00084
00085
00086
00087
00088
00089
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
00100
00101
00102
00103
00104
00105
00106
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
00142
00143
00144
00145
00146 def remove_option(self, section, option):
00147 ret = self.config.remove_option(section, option)
00148 self.write_config()
00149 return ret
00150
00151
00152
00153
00154
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
00173
00174
00175
00176 def isbool(self, string):
00177 if string.upper() == "TRUE" or string.upper() == "FALSE":
00178 return True
00179 return False
00180
00181
00182
00183
00184 def parsebool(self, string):
00185 return string[0].upper()=="T"
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195