The Gaudi Framework  master (37c0b60a)
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 178 of file Main.py.

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

◆ 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 230 of file Main.py.

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

◆ 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 292 of file Main.py.

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

◆ 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 268 of file Main.py.

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

Variable Documentation

◆ log

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

Definition at line 18 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:292
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:178
Gaudi.Main.toOpt
def toOpt(value)
Definition: Main.py:268
Gaudi.Main.getAllOpts
def getAllOpts(explicit_defaults=False)
Definition: Main.py:230