ATLAS Offline Software
Loading...
Searching...
No Matches
python.db.Databases Class Reference
Inheritance diagram for python.db.Databases:
Collaboration diagram for python.db.Databases:

Public Types

typedef HLT::TypeInformation::for_each_type_c< typenameEDMLIST::map, my_functor, my_result<>, my_arg< HLT::TypeInformation::get_cont, CONTAINER > >::type result

Public Member Functions

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

Static Public Attributes

dict FOLDERS

Detailed Description

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

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

Member Typedef Documentation

◆ result

Definition at line 90 of file EDM_MasterSearch.h.

Member Function Documentation

◆ build_folder()

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 415 of file DQUtils/python/db.py.

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

◆ fetch_for_writing()

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 451 of file DQUtils/python/db.py.

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

◆ get_folder()

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 304 of file DQUtils/python/db.py.

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

◆ get_instance()

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

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

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

◆ resolve_db_string()

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 358 of file DQUtils/python/db.py.

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

◆ resolve_folder_string()

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 269 of file DQUtils/python/db.py.

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

Member Data Documentation

◆ FOLDERS

dict 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 239 of file DQUtils/python/db.py.


The documentation for this class was generated from the following file: