The Gaudi Framework  v37r1 (a7f61348)
Gaudi.Main Namespace Reference

Classes

class  BootstrapHelper
 
class  gaudimain
 

Functions

def _getAllOpts_old (explicit_defaults=False)
 
def getAllOpts (explicit_defaults=False)
 
def toOpt (value)
 
def parseOpt (s)
 

Variables

 log = logging.getLogger(__name__)
 

Function Documentation

◆ _getAllOpts_old()

def Gaudi.Main._getAllOpts_old (   explicit_defaults = False)
private
Return all options from the old configuration system as a dictionary.

If explicit_defaults is true, include default values of unset properties in the dictionary.

Definition at line 180 of file Main.py.

180 def _getAllOpts_old(explicit_defaults=False):
181  """
182  Return all options from the old configuration system as a dictionary.
183 
184  If explicit_defaults is true, include default values of unset properties in the dictionary.
185  """
186  from itertools import chain
187 
188  from GaudiKernel.Proxy.Configurable import Configurable, getNeededConfigurables
189 
190  old_opts = {}
191 
192  # some algorithms may be generater when we call "getValuedProperties"
193  # so we need a few iterations before we get the full list
194  # (see GaudiConfig.ControlFlow)
195  needed_conf = []
196  count = 0
197  new_count = -1
198  while count != new_count:
199  count = new_count
200  needed_conf = getNeededConfigurables()
201  new_count = len(needed_conf)
202  for n in needed_conf:
203  c = Configurable.allConfigurables[n]
204  if hasattr(c, "getValuedProperties"):
205  c.getValuedProperties()
206 
207  for n in needed_conf:
208  c = Configurable.allConfigurables[n]
209  items = (
210  chain(c.getDefaultProperties().items(), c.getValuedProperties().items())
211  if explicit_defaults
212  else c.getValuedProperties().items()
213  )
214  for p, v in items:
215  # Note: AthenaCommon.Configurable does not have Configurable.PropertyReference
216  if hasattr(Configurable, "PropertyReference") and isinstance(
217  v, Configurable.PropertyReference
218  ):
219  # this is done in "getFullName", but the exception is ignored,
220  # so we do it again to get it
221  v = v.__resolve__()
222  if isinstance(v, str):
223  # properly escape quotes in the string (see gaudi/Gaudi#78)
224  v = '"%s"' % v.replace('"', '\\"')
225  elif hasattr(v, "__opt_value__"):
226  v = v.__opt_value__()
227  old_opts[".".join((n, p))] = str(v)
228 
229  return old_opts
230 
231 

◆ getAllOpts()

def Gaudi.Main.getAllOpts (   explicit_defaults = False)
Return all options from the old and new configuration system as a dictionary.

If explicit_defaults is true, include default values of unset properties in the dictionary.

Definition at line 232 of file Main.py.

232 def getAllOpts(explicit_defaults=False):
233  """
234  Return all options from the old and new configuration system as a dictionary.
235 
236  If explicit_defaults is true, include default values of unset properties in the dictionary.
237  """
238  import GaudiConfig2
239 
240  # We need to run normal (without defaults) collection first (regardless of explicit_defaults)
241  # as the conflicts detection makes sense only for explicitly set options
242  old_opts = _getAllOpts_old(False)
243  opts = GaudiConfig2.all_options(False)
244 
245  conflicts = [n for n in set(opts).intersection(old_opts) if opts[n] != old_opts[n]]
246  if conflicts:
247  conflicts.sort()
248  log.error("Some properties are set in old and new style configuration")
249  log.warning("name: old -> new")
250  for n in conflicts:
251  log.warning("%s: %s -> %s", n, old_opts[n], opts[n])
252  sys.exit(10)
253 
254  opts.update(old_opts)
255 
256  if explicit_defaults:
257  # If we are asked to print also the defaults, we collect everything
258  # and blindly merge to make sure we have the full set of configurables
259  all_opts = _getAllOpts_old(True)
260  all_opts.update(GaudiConfig2.all_options(True))
261  # the we override the dictionary with the explicitly set options
262  # (that leaves the defaults untouched and the set options with the
263  # correct value)
264  all_opts.update(opts)
265  opts = all_opts
266 
267  return opts
268 
269 

◆ parseOpt()

def Gaudi.Main.parseOpt (   s)
Helper to parse option strings to Python values.

Ideally it should just be "eval", but the string parser of Gaudi
is different from the Python one, so we get string options that
cannot be just evaluated.

>>> print(parseOpt('123'))
123
>>> print(parseOpt('"some\\n\\\\"text\\\\""'))
some
"text"
>>> print(parseOpt(''))
<BLANKLINE>

(see gaudi/Gaudi#78)

Definition at line 294 of file Main.py.

294 def parseOpt(s):
295  """
296  Helper to parse option strings to Python values.
297 
298  Ideally it should just be "eval", but the string parser of Gaudi
299  is different from the Python one, so we get string options that
300  cannot be just evaluated.
301 
302  >>> print(parseOpt('123'))
303  123
304  >>> print(parseOpt('"some\\n\\\\"text\\\\""'))
305  some
306  "text"
307  >>> print(parseOpt(''))
308  <BLANKLINE>
309 
310  (see gaudi/Gaudi#78)
311  """
312  import re
313 
314  quoted_string = re.compile(r'^"(.*)"$', re.DOTALL)
315  # FIXME: this is needed because we cannot use repr for strings
316  # (see gaudi/Gaudi#78)
317  if not s: # pass through empty strings
318  return s
319  m = quoted_string.match(s)
320  if m:
321  return m.group(1).replace('\\"', '"')
322  return eval(s)
323 
324 

◆ toOpt()

def Gaudi.Main.toOpt (   value)
Helper to convert values to old .opts format.

>>> print(toOpt('some "text"'))
"some \\"text\\""
>>> print(toOpt('first\\nsecond'))
"first
second"
>>> print(toOpt({'a': [1, 2, '3']}))
{"a": [1, 2, "3"]}

Definition at line 270 of file Main.py.

270 def toOpt(value):
271  """
272  Helper to convert values to old .opts format.
273 
274  >>> print(toOpt('some "text"'))
275  "some \\"text\\""
276  >>> print(toOpt('first\\nsecond'))
277  "first
278  second"
279  >>> print(toOpt({'a': [1, 2, '3']}))
280  {"a": [1, 2, "3"]}
281  """
282  if isinstance(value, six.string_types):
283  return '"{0}"'.format(value.replace('"', '\\"'))
284  elif isinstance(value, dict):
285  return "{{{0}}}".format(
286  ", ".join("{0}: {1}".format(toOpt(k), toOpt(v)) for k, v in value.items())
287  )
288  elif hasattr(value, "__iter__"):
289  return "[{0}]".format(", ".join(map(toOpt, value)))
290  else:
291  return repr(value)
292 
293 

Variable Documentation

◆ log

Gaudi.Main.log = logging.getLogger(__name__)

Definition at line 20 of file Main.py.

Containers::map
struct GAUDI_API map
Parametrisation class for map-like implementation.
Definition: KeyedObjectManager.h:35
GaudiKernel.Proxy.getNeededConfigurables
getNeededConfigurables
Definition: Proxy.py:30
Gaudi.Main.parseOpt
def parseOpt(s)
Definition: Main.py:294
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
Gaudi.Main._getAllOpts_old
def _getAllOpts_old(explicit_defaults=False)
Definition: Main.py:180
Gaudi.Main.toOpt
def toOpt(value)
Definition: Main.py:270
Gaudi.Main.getAllOpts
def getAllOpts(explicit_defaults=False)
Definition: Main.py:232
GaudiPython.Pythonizations.items
items
Definition: Pythonizations.py:546