ATLAS Offline Software
Public Member Functions | Static Public Attributes | List of all members
python.db.Databases Class Reference
Inheritance diagram for python.db.Databases:
Collaboration diagram for python.db.Databases:

Public Member Functions

def resolve_folder_string (cls, folder_name, db_override=None)
 
def get_folder (cls, folder_name, db_override=None, read_only=True, create_function=None, also_db=False)
 
def resolve_db_string (cls, db_string, read_only=True)
 
def get_instance (cls, db_string, read_only=True, create=False)
 
def build_folder (cls, db, folder_name, multiversion, record)
 
def fetch_for_writing (cls, orig_folder_name, multiversion=True, record=None, create=False, db_override=None)
 

Static Public Attributes

dictionary FOLDERS
 

Detailed Description

Databases helper class. Used as a singleton. (don't instantiate)
Specifies abbreviations for database connection strings and folders

Definition at line 235 of file DQUtils/python/db.py.

Member Function Documentation

◆ build_folder()

def python.db.Databases.build_folder (   cls,
  db,
  folder_name,
  multiversion,
  record 
)
Create `folder_name` on database instance `db`, with recordspecification
`record`.

Also creates folderset to which folder_name belongs if necessary.

Definition at line 417 of file DQUtils/python/db.py.

417  def build_folder(cls, db, folder_name, multiversion, record):
418  """
419  Create `folder_name` on database instance `db`, with recordspecification
420  `record`.
421 
422  Also creates folderset to which folder_name belongs if necessary.
423  """
424  from PyCool import cool
425 
426  folderset_path = dirname(folder_name)
427  try:
428  db.getFolderSet(folderset_path)
429  except Exception as error:
430  caught_error = "Folder set %s not found" % folderset_path
431  if caught_error not in error.args[0]:
432  raise
433 
434  log.debug("Folderset doesn't exist - creating it.")
435  db.createFolderSet(folderset_path, "", True)
436 
437  if not isinstance(record, cool.RecordSpecification):
438  record_spec = cool.RecordSpecification()
439  for field in record:
440  record_spec.extend(*field)
441  else:
442  record_spec = record
443 
444  FV = cool.FolderVersioning
445  versioning = FV.MULTI_VERSION if multiversion else FV.SINGLE_VERSION
446  folder_spec = cool.FolderSpecification(versioning, record_spec)
447  folder = db.createFolder(folder_name, folder_spec)
448  payload = cool.Record(record_spec)
449 
450  return folder, payload
451 

◆ fetch_for_writing()

def python.db.Databases.fetch_for_writing (   cls,
  orig_folder_name,
  multiversion = True,
  record = None,
  create = False,
  db_override = None 
)
Retrieve a folder for writing. Creates it if it doesn't exist.

`folder_name` specifies folder to be queried
`multiversion` specifies COOL versioning mode
`record` is a list of fields to be created in the form:
    [("<field name>", cool.StorageType.<field type>), ...]
    or if None, defaults to one Code record,
    or if isinstance(record, cool.RecordSpecification), uses this.
`create` should the database be created if it doesn't
`db_override` overrides automatic detection of database string

Definition at line 453 of file DQUtils/python/db.py.

453  def fetch_for_writing(cls, orig_folder_name, multiversion=True, record=None,
454  create=False, db_override=None):
455  """
456  Retrieve a folder for writing. Creates it if it doesn't exist.
457 
458  `folder_name` specifies folder to be queried
459  `multiversion` specifies COOL versioning mode
460  `record` is a list of fields to be created in the form:
461  [("<field name>", cool.StorageType.<field type>), ...]
462  or if None, defaults to one Code record,
463  or if isinstance(record, cool.RecordSpecification), uses this.
464  `create` should the database be created if it doesn't
465  `db_override` overrides automatic detection of database string
466  """
467  from PyCool import cool
468  if record is None:
469  record = [("Code", cool.StorageType.Int32)]
470 
471  database, folder_name = cls.resolve_folder_string(orig_folder_name)
472  if db_override:
473  database = db_override
474 
475  try:
476  db = cls.get_instance(database, False)
477 
478  except Exception as error:
479  if not create or "The database does not exist" not in error.args[0]:
480  raise
481 
482  from PyCool import cool
483  dbService = cool.DatabaseSvcFactory.databaseService()
484 
485  resolved_database, _ = cls.resolve_db_string(database)
486 
487  log.info("Database doesn't exist - creating it.")
488  db = dbService.createDatabase(resolved_database)
489 
490  try:
491  folder = db.getFolder(folder_name)
492  payload = cool.Record(folder.payloadSpecification())
493  except Exception as error:
494  if not create or "Folder %s not found" % folder_name not in error.args[0]:
495  raise
496 
497  log.debug("Folder doesn't exist - creating it.")
498  args = db, folder_name, multiversion, record
499  folder, payload = cls.build_folder(*args)
500 
501  return db, folder, payload

◆ get_folder()

def python.db.Databases.get_folder (   cls,
  folder_name,
  db_override = None,
  read_only = True,
  create_function = None,
  also_db = False 
)
Retrieve a folder. The `db_override` parameter allows over-riding the
database which comes from the folder string.

Parameters:
    `folder_name` : Folder name or alias to load
    `db_override` : If specified, causes an alternate database string
            to be used.
    `read_only`   : Specifies if a read-only database connection should
            be used.
    `create_function` : If specified, function to be called in case the
                folder doesn't exist. It is passed the database
                connection.
    `also_db`     : Make the return value (db, folder)

Definition at line 306 of file DQUtils/python/db.py.

306  def get_folder(cls, folder_name, db_override=None, read_only=True,
307  create_function=None, also_db=False):
308  """
309  Retrieve a folder. The `db_override` parameter allows over-riding the
310  database which comes from the folder string.
311 
312  Parameters:
313  `folder_name` : Folder name or alias to load
314  `db_override` : If specified, causes an alternate database string
315  to be used.
316  `read_only` : Specifies if a read-only database connection should
317  be used.
318  `create_function` : If specified, function to be called in case the
319  folder doesn't exist. It is passed the database
320  connection.
321  `also_db` : Make the return value (db, folder)
322  """
323 
324  if read_only:
325  assert not create_function, "`read_only` specified with `create`"
326 
327  database, folder = cls.resolve_folder_string(folder_name, db_override)
328  assert database, ("Unable to resolve database for (%s, %s)"
329  % (folder_name, db_override))
330 
331  create = bool(create_function)
332  db = cls.get_instance(database, read_only, create)
333 
334  try:
335  cool_folder = db.getFolder(folder)
336  except Exception as error:
337  log.debug('HELP! %s', error.args)
338  args = str(error)
339  log.debug('THIS IS %s', type(args))
340  log.debug('Value of boolean: %s', ("not found" in args))
341  if not ("cannot be established" in args or
342  "not found" in args
343  ):
344  log.exception("Unknown error encountered whilst opening "
345  "database connection to '%s'",
346  database)
347  raise
348 
349  if not create_function:
350  log.exception("Database does not exist, `create_function` not "
351  "specified")
352  raise
353 
354  cool_folder = create_function(db)
355  if also_db:
356  return db, cool_folder
357  return cool_folder
358 

◆ get_instance()

def python.db.Databases.get_instance (   cls,
  db_string,
  read_only = True,
  create = False 
)
Open a database connection

Definition at line 393 of file DQUtils/python/db.py.

393  def get_instance(cls, db_string, read_only=True, create=False):
394  """
395  Open a database connection
396  """
397  res_db_string, read_only = cls.resolve_db_string(db_string, read_only)
398 
399  try:
400  prev_stdout = sys.stdout
401  sys.stdout = StringIO()
402  try:
403  connection = indirectOpen(res_db_string, readOnly=read_only)
404  finally:
405  sys.stdout = prev_stdout
406  except Exception as e:
407  if ((e.args and "The database does not exist" in e.args[0]) or
408  str(e).find ('The database does not exist') >= 0) and not create:
409  log.info("Failed trying to connect to '%s'", res_db_string)
410  raise
411  from PyCool import cool
412  dbService = cool.DatabaseSvcFactory.databaseService()
413  connection = dbService.createDatabase(res_db_string)
414  return connection
415 

◆ resolve_db_string()

def python.db.Databases.resolve_db_string (   cls,
  db_string,
  read_only = True 
)
Take a database string - if it looks like a filename ending in ".db"
then assume it is a sqlite database with that name.

If the `db_string` is prepended with "WRITE|" then a writable connection
is requested.

If the db_string doesn't contain a "/", then "/" + DEFAULT_DBNAME is
appended.

Definition at line 360 of file DQUtils/python/db.py.

360  def resolve_db_string(cls, db_string, read_only=True):
361  """
362  Take a database string - if it looks like a filename ending in ".db"
363  then assume it is a sqlite database with that name.
364 
365  If the `db_string` is prepended with "WRITE|" then a writable connection
366  is requested.
367 
368  If the db_string doesn't contain a "/", then "/" + DEFAULT_DBNAME is
369  appended.
370  """
371  if "://" in db_string:
372  # Assume that the string is already resolved
373  return db_string, read_only
374 
375  if db_string.startswith("WRITE|"):
376  assert db_string.count("|") == 1, "Bad db_string format"
377  db_string = db_string.split("|")[1]
378  read_only = False
379 
380  sqlite_regex = re.compile(r"(?P<filename>.*?\.db)(?:/?(?P<dbname>[^/]+))?$")
381  matched = sqlite_regex.match(db_string)
382  if matched:
383  filename, dbname = matched.groups()
384  dbname = DEFAULT_DBNAME if not dbname else dbname
385  db_string = "sqlite://schema=%s;dbname=%s" % (filename, dbname)
386  else:
387  if "/" not in db_string:
388  return db_string + "/" + DEFAULT_DBNAME, read_only
389 
390  return db_string, read_only
391 

◆ resolve_folder_string()

def python.db.Databases.resolve_folder_string (   cls,
  folder_name,
  db_override = None 
)
Resolves a simplified folder URI.

Examples:   
    folder_name == "SHIFTOFL" 
will give a connection to COOLOFL_GLOBAL/COMP200
folder /GLOBAL/DETSTATUS/SHIFTOFL
    folder_name == "test.db::SHIFTOFL"
will give a connection to an sqlite database test.db

Priority:
    * Database specified in db_override
    * Database specified in `folder_name`
    * Database specified in cls.FOLDERS[folder_name]

Definition at line 271 of file DQUtils/python/db.py.

271  def resolve_folder_string(cls, folder_name, db_override=None):
272  """
273  Resolves a simplified folder URI.
274 
275  Examples:
276  folder_name == "SHIFTOFL"
277  will give a connection to COOLOFL_GLOBAL/COMP200
278  folder /GLOBAL/DETSTATUS/SHIFTOFL
279  folder_name == "test.db::SHIFTOFL"
280  will give a connection to an sqlite database test.db
281 
282  Priority:
283  * Database specified in db_override
284  * Database specified in `folder_name`
285  * Database specified in cls.FOLDERS[folder_name]
286  """
287  res_database = db_override
288 
289  # First check - is a database specified in the folder name?
290  if "::" in folder_name:
291  assert folder_name.count("::") == 1, "Bad folder format"
292  database, folder_name = folder_name.split("::")
293 
294  # If res_database hasn't already been set, do so
295  res_database = database if not res_database else res_database
296 
297  if folder_name in cls.FOLDERS:
298  database, res_folder = cls.FOLDERS[folder_name].split("::")
299  res_database = database if not res_database else res_database
300  else:
301  res_folder = folder_name
302 
303  return res_database, res_folder
304 

Member Data Documentation

◆ FOLDERS

dictionary python.db.Databases.FOLDERS
static
Initial value:
= {
"DQMFONL" : "COOLONL_GLOBAL::/GLOBAL/DETSTATUS/DQMFONL",
"DQMFONLLB" : "COOLONL_GLOBAL::/GLOBAL/DETSTATUS/DQMFONLLB",
"SHIFTONL" : "COOLONL_GLOBAL::/GLOBAL/DETSTATUS/SHIFTONL",
"DQMFOFL" : "COOLOFL_GLOBAL::/GLOBAL/DETSTATUS/DQMFOFL",
"DCSOFL" : "COOLOFL_GLOBAL::/GLOBAL/DETSTATUS/DCSOFL",
"DQCALCOFL" : "COOLOFL_GLOBAL::/GLOBAL/DETSTATUS/DQCALCOFL",
"SHIFTOFL" : "COOLOFL_GLOBAL::/GLOBAL/DETSTATUS/SHIFTOFL",
"LBSUMM" : "COOLOFL_GLOBAL::/GLOBAL/DETSTATUS/LBSUMM",
"VIRTUALFLAGS" : "COOLOFL_GLOBAL::/GLOBAL/DETSTATUS/VIRTUALFLAGS",
"DEFECTS" : "COOLOFL_GLOBAL::/GLOBAL/DETSTATUS/DEFECTS",
"SOR_Params" : "COOLONL_TDAQ::/TDAQ/RunCtrl/SOR_Params",
"EOR_Params" : "COOLONL_TDAQ::/TDAQ/RunCtrl/EOR_Params",
"SOR" : "COOLONL_TDAQ::/TDAQ/RunCtrl/SOR",
"EOR" : "COOLONL_TDAQ::/TDAQ/RunCtrl/EOR",
"LBLB" : "COOLONL_TRIGGER::/TRIGGER/LUMI/LBLB",
"LBTIME" : "COOLONL_TRIGGER::/TRIGGER/LUMI/LBTIME",
"LBLESTONL" : "COOLONL_TRIGGER::/TRIGGER/LUMI/LBLESTONL",
"LVL1COUNTERS" : "COOLONL_TRIGGER::/TRIGGER/LUMI/LVL1COUNTERS",
"HLTCOUNTERS" : "COOLOFL_TRIGGER::/TRIGGER/LUMI/HLTCOUNTERS",
"HLT/Menu" : "COOLONL_TRIGGER::/TRIGGER/HLT/Menu",
"LVL1/Menu" : "COOLONL_TRIGGER::/TRIGGER/LVL1/Menu",
"Prescales" : "COOLONL_TRIGGER::/TRIGGER/LVL1/Prescales",
}

Definition at line 241 of file DQUtils/python/db.py.


The documentation for this class was generated from the following file:
dirname
std::string dirname(std::string name)
Definition: utils.cxx:200
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
str
Definition: BTagTrackIpAccessor.cxx:11
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
python.AtlCoolLib.indirectOpen
def indirectOpen(coolstr, readOnly=True, debug=False)
Definition: AtlCoolLib.py:130
Trk::split
@ split
Definition: LayerMaterialProperties.h:38