blob: 86bc004dea7b866bbc0a838ca5a0ecc995304368 [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
24 def GetInt(self, section, option):
25 """
26 Get an option value as an integer
27 """
28 return self.Get(section, option, type='int')
29
30 def GetBool(self, section, option):
31 """
32 Get an option value as a boolean
33 """
34 return self.Get(section, option, type='bool')
35
36 def Get(self, section, option, raw=0, vars=None, default=None,
37 type=None):
38 """
39 Get an option value for given section/option or return default.
40 If type is specified, return as type.
41 """
42 if type=='bool': getVal=self.getbool
43 elif type=='int': getVal=self.getint
44 else: getVal=self.get
45 if self.has_option(section,option):
46 return getVal(section, option, raw, vars)
47 else:
48 return default
49
50 def GetSectionList(self):
51 # only provided for consistency
52 return self.sections()
53
54 def GetOptionList(self,section):
55 """
56 Get an option list for given section
57 """
58 if self.has_section:
59 return self.options(section)
60 else: #return a default value
61 return []
62
63 def GetHighlight(self, theme, element):
64 fore = self.Get(theme, element + "-foreground")
65 back = self.Get(theme, element + "-background")
66 style = self.Ge(theme, element + "-fontStyle", default='')
67 return {"fg": fore,
68 "bg": back,
69 "fStyle": style}
70
71 def Load(self):
72 """
73 Load the configuration file from disk
74 """
75 self.read(self.file)
76
77class IdleUserConfParser(IdleConfParser):
78 """
79 IdleConfigParser specialised for user configuration handling
80 """
81 def Save(self):
82 """
83 write loaded user configuration file back to disk
84 """
85 # this is a user config, it can be written to disk
86 self.write()
87
88class IdleConf:
89 """
90 holds config parsers for all idle config files:
91 default config files
92 (idle install dir)/config-main.def
93 (idle install dir)/config-extensions.def
94 (idle install dir)/config-highlight.def
95 (idle install dir)/config-keys.def
96 user config files
97 (user home dir)/.idlerc/idle-main.cfg
98 (user home dir)/.idlerc/idle-extensions.cfg
99 (user home dir)/.idlerc/idle-highlight.cfg
100 (user home dir)/.idlerc/idle-keys.cfg
101 """
102 def __init__(self):
103 self.defaultCfg={}
104 self.userCfg={}
105 self.cfg={}
106 self.CreateConfigHandlers()
107 self.LoadCfgFiles()
108 #self.LoadCfg()
109
110 def CreateConfigHandlers(self):
111 """
112 set up a dictionary config parsers for default and user
113 configurations respectively
114 """
115 #build idle install path
116 if __name__ != '__main__': # we were imported
117 idledir=os.path.dirname(__file__)
118 else: # we were exec'ed (for testing only)
119 idledir=os.path.abspath(sys.path[0])
120 #print idledir
121 try: #build user home path
122 userdir = os.environ['HOME'] #real home directory
123 except KeyError:
124 userdir = os.getcwd() #hack for os'es without real homedirs
125 userdir=os.path.join(userdir,'.idlerc')
126 #print userdir
127 if not os.path.exists(userdir):
128 os.mkdir(userdir)
129 configTypes=('main','extensions','highlight','keys')
130 defCfgFiles={}
131 usrCfgFiles={}
132 for cfgType in configTypes: #build config file names
133 defCfgFiles[cfgType]=os.path.join(idledir,'config-'+cfgType+'.def')
134 usrCfgFiles[cfgType]=os.path.join(userdir,'idle-'+cfgType+'.cfg')
135 for cfgType in configTypes: #create config parsers
136 self.defaultCfg[cfgType]=IdleConfParser(defCfgFiles[cfgType])
137 self.userCfg[cfgType]=IdleUserConfParser(usrCfgFiles[cfgType])
138
139 def LoadCfgFiles(self):
140 """
141 load all configuration files.
142 """
143 for key in self.defaultCfg.keys():
144 self.defaultCfg[key].Load()
145 self.userCfg[key].Load() #same keys
146
147 def SaveUserCfgFiles(self):
148 """
149 write all loaded user configuration files back to disk
150 """
151 for key in self.userCfg.keys():
152 self.userCfg[key].Save()
153
154idleConf=IdleConf()
155
156### module test
157if __name__ == '__main__':
158 def dumpCfg(cfg):
159 print '\n',cfg,'\n'
160 for key in cfg.keys():
161 sections=cfg[key].sections()
162 print key
163 print sections
164 for section in sections:
165 options=cfg[key].options(section)
166 print section
167 print options
168 for option in options:
169 print option, '=', cfg[key].Get(section,option)
170 dumpCfg(idleConf.defaultCfg)
171 dumpCfg(idleConf.userCfg)
172 print idleConf.userCfg['main'].Get('Theme','name')
173 #print idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal')