blob: 26bb459cc33edbc6aaff016a76ed546e18c6bf0f [file] [log] [blame]
Steven M. Gavac11ccf32001-09-24 09:43:17 +00001##---------------------------------------------------------------------------##
2##
3## idle - configuration data handler, based on and replacing IdleConfig.py
4## elguavas
5##
6##---------------------------------------------------------------------------##
7"""Provides access to configuration information"""
8
9import os
10import sys
11from ConfigParser import ConfigParser, NoOptionError, NoSectionError
12
13class IdleConfParser(ConfigParser):
14 """
15 A ConfigParser specialised for idle configuration file handling
16 """
17 def __init__(self, cfgFile, cfgDefaults=None):
18 """
19 cfgFile - string, fully specified configuration file name
20 """
21 self.file=cfgFile
22 ConfigParser.__init__(self,defaults=cfgDefaults)
23
Steven M. Gava429a86a2001-10-23 10:42:12 +000024# def GetInt(self, section, option, *kw):
25# """
26# Get an option value as an integer
27# """
28# return self.Get(section, option, type='int', *kw)
29#
30# def GetBool(self, section, option, **kw):
31# """
32# Get an option value as a boolean
33# """
34# return self.Get(section, option, type='bool', *kw)
Steven M. Gavac11ccf32001-09-24 09:43:17 +000035
Steven M. Gava429a86a2001-10-23 10:42:12 +000036# def Get(self, section, option, raw=0, vars=None, default=None,
37# type=None):
38 def Get(self, section, option, default=None, type=None):
Steven M. Gavac11ccf32001-09-24 09:43:17 +000039 """
40 Get an option value for given section/option or return default.
41 If type is specified, return as type.
42 """
Steven M. Gava429a86a2001-10-23 10:42:12 +000043 if type=='bool': getVal=self.getboolean
Steven M. Gavac11ccf32001-09-24 09:43:17 +000044 elif type=='int': getVal=self.getint
45 else: getVal=self.get
46 if self.has_option(section,option):
Steven M. Gava429a86a2001-10-23 10:42:12 +000047 #return getVal(section, option, raw, vars)
48 return getVal(section, option)
Steven M. Gavac11ccf32001-09-24 09:43:17 +000049 else:
50 return default
51
52 def GetSectionList(self):
53 # only provided for consistency
54 return self.sections()
55
56 def GetOptionList(self,section):
57 """
58 Get an option list for given section
59 """
60 if self.has_section:
61 return self.options(section)
62 else: #return a default value
63 return []
64
65 def GetHighlight(self, theme, element):
66 fore = self.Get(theme, element + "-foreground")
67 back = self.Get(theme, element + "-background")
68 style = self.Ge(theme, element + "-fontStyle", default='')
69 return {"fg": fore,
70 "bg": back,
71 "fStyle": style}
72
73 def Load(self):
74 """
75 Load the configuration file from disk
76 """
77 self.read(self.file)
78
79class IdleUserConfParser(IdleConfParser):
80 """
81 IdleConfigParser specialised for user configuration handling
82 """
83 def Save(self):
84 """
85 write loaded user configuration file back to disk
86 """
87 # this is a user config, it can be written to disk
88 self.write()
89
90class IdleConf:
91 """
92 holds config parsers for all idle config files:
93 default config files
94 (idle install dir)/config-main.def
95 (idle install dir)/config-extensions.def
96 (idle install dir)/config-highlight.def
97 (idle install dir)/config-keys.def
98 user config files
99 (user home dir)/.idlerc/idle-main.cfg
100 (user home dir)/.idlerc/idle-extensions.cfg
101 (user home dir)/.idlerc/idle-highlight.cfg
102 (user home dir)/.idlerc/idle-keys.cfg
103 """
104 def __init__(self):
105 self.defaultCfg={}
106 self.userCfg={}
107 self.cfg={}
108 self.CreateConfigHandlers()
109 self.LoadCfgFiles()
110 #self.LoadCfg()
111
112 def CreateConfigHandlers(self):
113 """
114 set up a dictionary config parsers for default and user
115 configurations respectively
116 """
117 #build idle install path
118 if __name__ != '__main__': # we were imported
119 idledir=os.path.dirname(__file__)
120 else: # we were exec'ed (for testing only)
121 idledir=os.path.abspath(sys.path[0])
122 #print idledir
123 try: #build user home path
124 userdir = os.environ['HOME'] #real home directory
125 except KeyError:
126 userdir = os.getcwd() #hack for os'es without real homedirs
127 userdir=os.path.join(userdir,'.idlerc')
128 #print userdir
129 if not os.path.exists(userdir):
130 os.mkdir(userdir)
131 configTypes=('main','extensions','highlight','keys')
132 defCfgFiles={}
133 usrCfgFiles={}
134 for cfgType in configTypes: #build config file names
135 defCfgFiles[cfgType]=os.path.join(idledir,'config-'+cfgType+'.def')
136 usrCfgFiles[cfgType]=os.path.join(userdir,'idle-'+cfgType+'.cfg')
137 for cfgType in configTypes: #create config parsers
138 self.defaultCfg[cfgType]=IdleConfParser(defCfgFiles[cfgType])
139 self.userCfg[cfgType]=IdleUserConfParser(usrCfgFiles[cfgType])
140
Steven M. Gava429a86a2001-10-23 10:42:12 +0000141 def GetDefault(self, configType, section, option, default=None, type=None):
142 """
143 Get an option value for given config type and given general
144 configuration section/option or return a default. If type is specified,
145 return as type. Firstly the user configuration is checked, with a
146 fallback to the default configuration, and a final 'catch all'
147 fallback to a useable passed-in default if the option isn't present in
148 either the user or the default configuration.
149 configType must be one of ('main','extensions','highlight','keys')
150 """
151 if self.userCfg[configType].has_option(section,option):
152 return self.userCfg[configType].Get(section, option, type=type)
153 elif self.defaultCfg[configType].has_option(section,option):
154 return self.defaultCfg[configType].Get(section, option, type=type)
155 else:
156 return default
157
Steven M. Gavac11ccf32001-09-24 09:43:17 +0000158 def LoadCfgFiles(self):
159 """
160 load all configuration files.
161 """
162 for key in self.defaultCfg.keys():
163 self.defaultCfg[key].Load()
164 self.userCfg[key].Load() #same keys
165
166 def SaveUserCfgFiles(self):
167 """
168 write all loaded user configuration files back to disk
169 """
170 for key in self.userCfg.keys():
171 self.userCfg[key].Save()
172
173idleConf=IdleConf()
174
175### module test
176if __name__ == '__main__':
177 def dumpCfg(cfg):
178 print '\n',cfg,'\n'
179 for key in cfg.keys():
180 sections=cfg[key].sections()
181 print key
182 print sections
183 for section in sections:
184 options=cfg[key].options(section)
185 print section
186 print options
187 for option in options:
188 print option, '=', cfg[key].Get(section,option)
189 dumpCfg(idleConf.defaultCfg)
190 dumpCfg(idleConf.userCfg)
191 print idleConf.userCfg['main'].Get('Theme','name')
192 #print idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal')