1
2 import Ganga.Utility.logging
3 logger = Ganga.Utility.logging.getLogger()
4
5 from Ganga.Utility.Config import ConfigError
6
7
8
9
10
19
20 from Ganga.Utility.Config import getConfig
21
22 display_config = getConfig('Display')
23
24 display_config.addOption('config_name_colour','fx.bold', 'colour print of the names of configuration sections and options')
25 display_config.addOption('config_docstring_colour','fg.green', 'colour print of the docstrings and examples')
26 display_config.addOption('config_value_colour','fx.bold', 'colour print of the configuration values')
27
28
30 """ A proxy for a single configuration unit.
31 This is a base class which is inherited by a concrete proxy class for each configuration unit.
32 In each inherited class descriptors (attreibutes) are set for all configuration options.
33 This is done by the bootstrap() function.
34 """
36 self.__dict__['_impl'] = impl
37
39 try:
40 return getattr(self,o)
41 except AttributeError,x:
42 raise ConfigError('Undefined option %s (%s)'%(o,str(x)))
43
54
55 __setitem__ = __setattr__
56
59
62
64 from Ganga.Utility.ColourText import ANSIMarkup, NoMarkup, getColour, Foreground, Background, Effects
65 import Ganga.Utility.external.textwrap as textwrap
66
67 if colour:
68 markup = ANSIMarkup()
69 else:
70 markup = NoMarkup()
71
72 fg = Foreground()
73 fx = Effects()
74
75 from Ganga.Utility.Config import getConfig
76
77 display_config = getConfig('Display')
78
79 name_colour = getColour(display_config['config_name_colour'])
80 docstring_colour = getColour(display_config['config_docstring_colour'])
81 value_colour = getColour(display_config['config_value_colour'])
82
83 levels = ['**','* ',' ']
84 levels = map(lambda x: markup(x,fg.red),levels)
85 from StringIO import StringIO
86 sio = StringIO()
87 print >>sio, '%s'%markup(self._impl.name,name_colour),':',markup(self._impl.docstring,docstring_colour)
88 opts = self._impl.options.keys()
89 opts.sort()
90 INDENT = ' '*2
91 for o in opts:
92 print >>sio, levels[self._impl.getEffectiveLevel(o)],' ',markup(o,name_colour),'=',markup(repr(self._impl[o]),value_colour)
93 print >>sio, textwrap.fill(markup(self._impl.options[o].docstring.strip(),docstring_colour),width=80, initial_indent=INDENT,subsequent_indent=INDENT)
94 typelist = self._impl.options[o].typelist
95 if not typelist:
96 typedesc = 'Type: '+str(type(self._impl.options[o].default_value))
97 else:
98 typedesc = 'Allowed types: '+str([t.split('.')[-1] for t in typelist])
99 print >>sio, markup(INDENT+typedesc,docstring_colour)
100 filter = self._impl.options[o].filter
101 if filter:
102 filter_doc = filter.__doc__
103 if not filter_doc: filter_doc = "undocumented"
104 print >>sio, markup(INDENT+"Filter: "+filter_doc,docstring_colour)
105 examples = self._impl.options[o].examples
106 if examples:
107 print >>sio,markup(INDENT+"Examples:",docstring_colour)
108 for e in examples.splitlines():
109 print >>sio, markup(INDENT+e.strip(),docstring_colour)
110
111
112 return sio.getvalue()
113
115 """ A proxy class for the main config object which contains all configuration sections.
116 The configuration section proxies are set as attributes by the bootstrap() function.
117 This class is used to create a singleton GPI config object.
118 """
119 - def __init__(self):
120 import Ganga.Utility.Config
121 self.__dict__['_impl'] = Ganga.Utility.Config.allConfigs
122
123 - def __getitem__(self,p):
124 try:
125 return getattr(self,p)
126 except AttributeError,x:
127 msg = 'configuration section "%s" not found' % str(p)
128 logger.error(msg)
129 raise ConfigError(msg)
130
131 - def __setattr__(self,p,v):
132 msg = 'cannot create new configuration sections in GPI'
133 logger.error(msg)
134 raise ConfigError(msg)
135
136 __setitem__ = __setattr__
137
138 - def __iter__(self):
139 return self._impl.__iter__()
140
142 return self._display(False)
143
144 - def _display(self,colour):
145 from Ganga.Utility.ColourText import ANSIMarkup, NoMarkup, Foreground, Background, Effects
146
147 if colour:
148 markup = ANSIMarkup()
149 else:
150 markup = NoMarkup()
151
152 fg = Foreground()
153
154 from StringIO import StringIO
155 sio = StringIO()
156 print >>sio, "Ganga Configuration"
157 sections = self._impl.keys()
158 sections.sort()
159 maxcol = 0
160 for p in sections:
161 if len(p)>maxcol:
162 maxcol = len(p)
163 if maxcol>50:
164 maxcol=50
165 for p in sections:
166 print >>sio,'%-*s : %s'%(maxcol,p,markup(self._impl[p].docstring.split('\n')[0],fg.boldgrey))
167 return sio.getvalue()
168
169
170 config = MainConfigProxy()
171
173 sections = config._impl.keys()
174 sections.sort()
175 def print_doc_text(text):
176 for line in text.splitlines():
177 print '#',line
178
179 for p in sections:
180 sect = config._impl[p]
181 if not sect.cfile:
182 continue
183 print
184 print "#======================================================================="
185 print "[%s]"%p
186
187 print_doc_text(sect.docstring)
188
189 opts = sect.options.keys()
190 opts.sort()
191 for o in opts:
192 if sect.options[o].cfile:
193 print
194 print_doc_text(sect.options[o].docstring)
195 print '#%s = %s'%(o,sect.options[o].default_value)
196
198
199 import Ganga.Utility.external.textwrap as textwrap
200
201 text = ''
202
203 sections = config._impl.keys()
204 sections.sort()
205 INDENT = "# "
206 INDENT_value = "# "
207 for p in sections:
208
209 sect = config._impl[p]
210 if not sect.cfile:
211 continue
212
213 text += "\n"
214 text += "#=======================================================================\n"
215 text += textwrap.fill(sect.docstring.strip(),width=80, initial_indent=INDENT,subsequent_indent=INDENT)+"\n"
216 text += "[%s]\n\n"%p
217
218 opts = sect.options.keys()
219 opts.sort()
220 for o in opts:
221 if sect.options[o].cfile:
222 text += ""
223 text += textwrap.fill(sect.options[o].docstring.strip(),width=80, initial_indent=INDENT,subsequent_indent=INDENT)+"\n"
224
225 examples = sect.options[o].examples
226 if examples:
227 text += INDENT+"Examples:\n"
228 for e in examples.splitlines():
229 text += INDENT+" "+e.strip()+"\n"
230 value = sect.options[o].default_value
231 try:
232 lines = value.splitlines()
233 if len(lines)>1:
234 value = "\n# ".join(lines)
235 except:
236 pass
237 text +='#%s = %s\n\n'%(o,value)
238
239 return text
240
242 """ Create a class derived from ConfigProxy with all option descriptors and insert it into MainConfigProxy class.
243 """
244 cfg = config._impl[name]
245 if not cfg.hidden:
246 d = {}
247 for opt in cfg:
248 o = cfg.options[opt]
249 if not o.check_defined():
250 if not cfg.is_open:
251 logger.error('undefined option [%s]%s removed from configuration',name,opt)
252 continue
253 else:
254 cfg.addOption(opt,o.value,'',override=True)
255 o = cfg.options[opt]
256 if not o.hidden:
257 d[opt] = ConfigDescriptor(opt)
258
259 cfg.deleteUndefinedOptions()
260
261 proxy_class = type(name,(ConfigProxy,), d)
262 setattr(MainConfigProxy,name,proxy_class(config._impl[name]))
263
265 """ Create an option description in an existing ConfigProxy class - an attribute of MainConfigProxy class.
266 """
267 if not config._impl[section_name].options[option_name].hidden:
268 setattr(getattr(MainConfigProxy,section_name).__class__,option_name,ConfigDescriptor(option_name))
269
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295