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