blob: e0713521693bd10becc02f9c1a938d220b965192 [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##---------------------------------------------------------------------------##
Steven M. Gava2a63a072001-10-26 06:50:54 +00007"""
8Provides access to configuration information
9"""
Steven M. Gavac11ccf32001-09-24 09:43:17 +000010
11import os
12import sys
13from ConfigParser import ConfigParser, NoOptionError, NoSectionError
14
15class IdleConfParser(ConfigParser):
16 """
17 A ConfigParser specialised for idle configuration file handling
18 """
19 def __init__(self, cfgFile, cfgDefaults=None):
20 """
21 cfgFile - string, fully specified configuration file name
22 """
23 self.file=cfgFile
24 ConfigParser.__init__(self,defaults=cfgDefaults)
25
Steven M. Gava429a86a2001-10-23 10:42:12 +000026 def Get(self, section, option, default=None, type=None):
Steven M. Gavac11ccf32001-09-24 09:43:17 +000027 """
28 Get an option value for given section/option or return default.
29 If type is specified, return as type.
30 """
Steven M. Gava429a86a2001-10-23 10:42:12 +000031 if type=='bool': getVal=self.getboolean
Steven M. Gavac11ccf32001-09-24 09:43:17 +000032 elif type=='int': getVal=self.getint
33 else: getVal=self.get
34 if self.has_option(section,option):
Steven M. Gava429a86a2001-10-23 10:42:12 +000035 #return getVal(section, option, raw, vars)
36 return getVal(section, option)
Steven M. Gavac11ccf32001-09-24 09:43:17 +000037 else:
38 return default
39
Steven M. Gavac11ccf32001-09-24 09:43:17 +000040 def GetOptionList(self,section):
41 """
42 Get an option list for given section
43 """
44 if self.has_section:
45 return self.options(section)
46 else: #return a default value
47 return []
48
49 def GetHighlight(self, theme, element):
50 fore = self.Get(theme, element + "-foreground")
51 back = self.Get(theme, element + "-background")
52 style = self.Ge(theme, element + "-fontStyle", default='')
53 return {"fg": fore,
54 "bg": back,
55 "fStyle": style}
56
57 def Load(self):
58 """
59 Load the configuration file from disk
60 """
61 self.read(self.file)
62
63class IdleUserConfParser(IdleConfParser):
64 """
65 IdleConfigParser specialised for user configuration handling
66 """
67 def Save(self):
68 """
69 write loaded user configuration file back to disk
70 """
71 # this is a user config, it can be written to disk
72 self.write()
73
74class IdleConf:
75 """
76 holds config parsers for all idle config files:
77 default config files
78 (idle install dir)/config-main.def
79 (idle install dir)/config-extensions.def
80 (idle install dir)/config-highlight.def
81 (idle install dir)/config-keys.def
82 user config files
83 (user home dir)/.idlerc/idle-main.cfg
84 (user home dir)/.idlerc/idle-extensions.cfg
85 (user home dir)/.idlerc/idle-highlight.cfg
86 (user home dir)/.idlerc/idle-keys.cfg
87 """
88 def __init__(self):
89 self.defaultCfg={}
90 self.userCfg={}
91 self.cfg={}
92 self.CreateConfigHandlers()
93 self.LoadCfgFiles()
94 #self.LoadCfg()
95
96 def CreateConfigHandlers(self):
97 """
98 set up a dictionary config parsers for default and user
99 configurations respectively
100 """
101 #build idle install path
102 if __name__ != '__main__': # we were imported
103 idledir=os.path.dirname(__file__)
104 else: # we were exec'ed (for testing only)
105 idledir=os.path.abspath(sys.path[0])
106 #print idledir
107 try: #build user home path
108 userdir = os.environ['HOME'] #real home directory
109 except KeyError:
110 userdir = os.getcwd() #hack for os'es without real homedirs
111 userdir=os.path.join(userdir,'.idlerc')
112 #print userdir
113 if not os.path.exists(userdir):
114 os.mkdir(userdir)
115 configTypes=('main','extensions','highlight','keys')
116 defCfgFiles={}
117 usrCfgFiles={}
118 for cfgType in configTypes: #build config file names
119 defCfgFiles[cfgType]=os.path.join(idledir,'config-'+cfgType+'.def')
120 usrCfgFiles[cfgType]=os.path.join(userdir,'idle-'+cfgType+'.cfg')
121 for cfgType in configTypes: #create config parsers
122 self.defaultCfg[cfgType]=IdleConfParser(defCfgFiles[cfgType])
123 self.userCfg[cfgType]=IdleUserConfParser(usrCfgFiles[cfgType])
124
Steven M. Gava2a63a072001-10-26 06:50:54 +0000125 def GetOption(self, configType, section, option, default=None, type=None):
Steven M. Gava429a86a2001-10-23 10:42:12 +0000126 """
127 Get an option value for given config type and given general
128 configuration section/option or return a default. If type is specified,
129 return as type. Firstly the user configuration is checked, with a
130 fallback to the default configuration, and a final 'catch all'
131 fallback to a useable passed-in default if the option isn't present in
132 either the user or the default configuration.
133 configType must be one of ('main','extensions','highlight','keys')
134 """
135 if self.userCfg[configType].has_option(section,option):
136 return self.userCfg[configType].Get(section, option, type=type)
137 elif self.defaultCfg[configType].has_option(section,option):
138 return self.defaultCfg[configType].Get(section, option, type=type)
139 else:
140 return default
141
Steven M. Gava2a63a072001-10-26 06:50:54 +0000142 def GetSectionList(self, configSet, configType):
143 """
144 Get a list of sections from either the user or default config for
145 the given config type.
146 configSet must be either 'user' or 'default'
147 configType must be one of ('extensions','highlight','keys')
148 """
149 if not (configType in ('extensions','highlight','keys')):
150 raise 'Invalid configType specified'
151 if configSet == 'user':
152 cfgParser=self.userCfg[configType]
153 elif configSet == 'default':
154 cfgParser=self.defaultCfg[configType]
155 else:
156 raise 'Invalid configSet specified'
157
158 return cfgParser.sections()
159
160
161 def GetTheme(self, name=None):
162 """
163 Gets the requested theme or returns a final fallback theme in case
164 one can't be obtained from either the user or default config files.
165 """
166 pass
167
168
169 def GetKeys(self, name=None):
170 """
171 Gets the requested keybindings or returns a final fallback keybinding
172 set in case one can't be obtained from either the user or default
173 config files.
174 """
175 pass
176
177
Steven M. Gavac11ccf32001-09-24 09:43:17 +0000178 def LoadCfgFiles(self):
179 """
180 load all configuration files.
181 """
182 for key in self.defaultCfg.keys():
183 self.defaultCfg[key].Load()
184 self.userCfg[key].Load() #same keys
185
186 def SaveUserCfgFiles(self):
187 """
188 write all loaded user configuration files back to disk
189 """
190 for key in self.userCfg.keys():
191 self.userCfg[key].Save()
192
193idleConf=IdleConf()
194
195### module test
196if __name__ == '__main__':
197 def dumpCfg(cfg):
198 print '\n',cfg,'\n'
199 for key in cfg.keys():
200 sections=cfg[key].sections()
201 print key
202 print sections
203 for section in sections:
204 options=cfg[key].options(section)
205 print section
206 print options
207 for option in options:
208 print option, '=', cfg[key].Get(section,option)
209 dumpCfg(idleConf.defaultCfg)
210 dumpCfg(idleConf.userCfg)
211 print idleConf.userCfg['main'].Get('Theme','name')
212 #print idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal')