pgtoolkit/config.py
changeset 17 f768a3529ee7
parent 10 f3a1b9792cc9
equal deleted inserted replaced
16:cb7e13711a99 17:f768a3529ee7
     7         self.registered_args = {}
     7         self.registered_args = {}
     8         self.log = logging.getLogger('config')
     8         self.log = logging.getLogger('config')
     9 
     9 
    10     def add_argument(self, name, type=str, default=None):
    10     def add_argument(self, name, type=str, default=None):
    11         self.registered_args[name] = {'type':type, 'default':default}
    11         self.registered_args[name] = {'type':type, 'default':default}
       
    12         self.args[name] = default
    12 
    13 
    13     def load(self, fname):
    14     def load(self, fname):
    14         # parse config file
    15         # parse config file
    15         with open(fname) as f:
    16         with open(fname) as f:
    16             exec(f.read(), self.args)
    17             exec(f.read(), self.args)
    23             if key == '__builtins__':
    24             if key == '__builtins__':
    24                 continue
    25                 continue
    25             if key in self.registered_args:
    26             if key in self.registered_args:
    26                 # arg registered, check type
    27                 # arg registered, check type
    27                 type = self.registered_args[key]['type']
    28                 type = self.registered_args[key]['type']
    28                 if not isinstance(self.args[key], type):
    29                 if not isinstance(self.args[key], type) and not self.args[key] is None:
    29                     ok = False
    30                     ok = False
    30                     self.log.error("Bad type of config parameter '%s': is %s but should be %s",
    31                     self.log.error("Bad type of config parameter '%s': is %s but should be %s",
    31                         key, type(self.args[key]), type)
    32                         key, type(self.args[key]), type)
    32             else:
    33             else:
    33                 # arg not registered
    34                 # arg not registered
    34                     ok = False
    35                 ok = False
    35                     self.log.error("Unknown config parameter '%s'.", key)
    36                 self.log.error("Unknown config parameter '%s'.", key)
    36         return ok
    37         return ok
    37 
    38 
    38     def __getattr__(self, name):
    39     def __getattr__(self, name):
    39         if name in self.args:
    40         if name in self.args:
    40             return self.args[name]
    41             return self.args[name]