SND@LHC Software
Loading...
Searching...
No Matches
conditionsDatabase.factory.APIFactory Class Reference

This class creates an instance of the specified database API. More...

Public Member Functions

 __init__ (self)
 
 construct_DB_API (self, path=None)
 Returns an instance of the specified database API based on a configuration file.
 

Static Public Attributes

str db_type = "mongo"
 
 connection_dict = config[db_type]
 

Private Member Functions

 __read_config_file (self, path)
 Loads the configuration from the config file, including the database type and also the connection information.
 

Private Attributes

 __supported_db_types
 

Detailed Description

This class creates an instance of the specified database API.

TODO uncomment for python >= 3.8: @final

Definition at line 15 of file factory.py.

Constructor & Destructor Documentation

◆ __init__()

conditionsDatabase.factory.APIFactory.__init__ (   self)

Definition at line 17 of file factory.py.

17 def __init__(self):
18 # supported_db_types is the list of different database back-ends which are supported
19 self.__supported_db_types = ["mongo"]
20

Member Function Documentation

◆ __read_config_file()

conditionsDatabase.factory.APIFactory.__read_config_file (   self,
  path 
)
private

Loads the configuration from the config file, including the database type and also the connection information.

Parameters
pathThe path to the configuration file. It could be "", then the default path will be considered The default path is $FAIRSHIP/conditionsDatabase/config.yml
Exceptions
KeyErrorIf the configuration file is incomplete
Returns
The connection dictionary

Definition at line 55 of file factory.py.

55 def __read_config_file(self, path):
56 ret = {}
57
58 if not path:
59 home_dir = str(os.getenv('FAIRSHIP'))
60 path = home_dir + "/conditionsDatabase/config.yml"
61 else:
62 path_details = path.split(".")
63 file_extention = path_details[len(path_details) - 1]
64 if not(file_extention == "yml" or file_extention == "yaml"):
65 print("The file extension is incorrect. A YAML file is required.")
66 return None
67
68 try:
69 with open(path, 'r') as ymlfile:
70 cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
71 except IOError:
72 print("The configuration file does not exit or Invalid path to "
73 "the file:", str(sys.exc_info()[0]))
74 return None
75
76 connection_dict = {'db_name': '', 'user': None, 'password': None, 'host': "", 'port': 0}
77 db_type = None
78
79 try:
80 db_type = cfg["db_type"]
81 if db_type is None:
82 print("db type should be defined")
83 return None
84
85 if db_type not in cfg.keys():
86 print("db config is not defined")
87 return None
88
89 connection_dict["host"] = cfg[db_type]["host"]
90 connection_dict["db_name"] = cfg[db_type]["db_name"]
91 connection_dict["user"] = cfg[db_type]["user"]
92 connection_dict["password"] = cfg[db_type]["password"]
93 connection_dict["port"] = cfg[db_type]["port"]
94 ret[db_type.lower()] = connection_dict
95 #evh print
96 #print (" connection_dict=",connection_dict)
97 return ret
98 except KeyError:
99 print("Incorrect configuration file, missing some parameters. it should contain: "
100 "db_type, db_name, user, password, host, and port.")
101 return None

◆ construct_DB_API()

conditionsDatabase.factory.APIFactory.construct_DB_API (   self,
  path = None 
)

Returns an instance of the specified database API based on a configuration file.

Parameters
pathThe path to the configuration file. This field can be left empty/None, then the default path will be considered The default path is $FAIRSHIP/conditionsDatabase/config.yml
Exceptions
NotImplementedErrorIf the specified database is not supported
Returns
Instance of the specified database API

Definition at line 27 of file factory.py.

27 def construct_DB_API(self, path=None):
28
29 config = self.__read_config_file(path)
30
31 if config is None or len(config) <= 0:
32 raise ValueError("Error in reading or accessing the configuration file")

Member Data Documentation

◆ __supported_db_types

conditionsDatabase.factory.APIFactory.__supported_db_types
private

Definition at line 19 of file factory.py.

◆ connection_dict

conditionsDatabase.factory.APIFactory.connection_dict = config[db_type]
static

Definition at line 36 of file factory.py.

◆ db_type

str conditionsDatabase.factory.APIFactory.db_type = "mongo"
static

Definition at line 35 of file factory.py.


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