class Config(object): """ This class has a shared state like a Borg. This is a Kaptan class that infers the file handler from the file extension using the `from_file` function. Parameters ---------- handler: str or kaptan.BaseHandler This parameter is to keep compatibility with Kaptan's instantiation protocol. See its documentation on how to use it: http://emre.github.io/kaptan/ I personally prefer to use the `from_file` function. """ __shared_state = {} def __init__(self, handler=None): self.__dict__ = self.__shared_state self._cpt = Kaptan(handler) @classmethod def from_file(cls, file_path): """ Returns a Config instance with the data from `file_path`. Parameters ---------- file_path: str Path to a configuration file. Its extension can be any from config.HANDLER_EXT, i.e., {'conf': 'ini', 'ini': 'ini', 'json': 'json', 'py': 'file', 'yaml': 'yaml', 'yml': 'yaml'} Returns ------- cfg: Config """ _check_file(file_path) cfg = Config() cfg._cpt = _load_config(file_path) return cfg def update_from_file(self, file_path): """ Updates the config parameters with the data from `file_path`. Parameters ---------- file_path: str Path to a configuration file. Its extension can be any from config.HANDLER_EXT, i.e., {'conf': 'ini', 'ini': 'ini', 'json': 'json', 'py': 'file', 'yaml': 'yaml', 'yml': 'yaml'} Returns ------- cfg: Config """ _check_file(file_path) cpt = _load_config(file_path) params = cpt.configuration_data _update_kaptan(self._cpt, params) def check_file(self, item): """ This is a __getitem__ operator for file path values, if the file does not exist an IOError is raised.""" try: fpath = self.__getitem__(item) except KeyError: raise else: if not op.exists(fpath): raise IOError( 'Could not find file for key {} in the {}.'.format( item, fpath)) return fpath def update(self, adict): for k, v in adict.items(): self._cpt.upsert(k, v) def keys(self): return self._cpt.configuration_data.keys() def items(self): return self._cpt.configuration_data.items() def get(self, item, default=None): return self._cpt.configuration_data.get(item, default) def __getitem__(self, item): if item not in self._cpt.configuration_data: raise KeyError( 'Could not find key {} in configuration content.'.format(item)) return self._cpt.configuration_data[item] def __setitem__(self, key, value): return self._cpt.upsert(key, value) def __repr__(self): return '<config.Config> ({})'.format('\n'.join( [str(i) for i in self.items()]))
class Config(object): """ This class has a shared state like a Borg. This is a Kaptan class that infers the file handler from the file extension using the `from_file` function. Parameters ---------- handler: str or kaptan.BaseHandler This parameter is to keep compatibility with Kaptan's instantiation protocol. See its documentation on how to use it: http://emre.github.io/kaptan/ I personally prefer to use the `from_file` function. """ __shared_state = {} def __init__(self, handler=None): self.__dict__ = self.__shared_state self._cpt = Kaptan(handler) @classmethod def from_file(cls, file_path): """ Returns a Config instance with the data from `file_path`. Parameters ---------- file_path: str Path to a configuration file. Its extension can be any from config.HANDLER_EXT, i.e., {'conf': 'ini', 'ini': 'ini', 'json': 'json', 'py': 'file', 'yaml': 'yaml', 'yml': 'yaml'} Returns ------- cfg: Config """ _check_file(file_path) cfg = Config() cfg._cpt = _load_config(file_path) return cfg def update_from_file(self, file_path): """ Updates the config parameters with the data from `file_path`. Parameters ---------- file_path: str Path to a configuration file. Its extension can be any from config.HANDLER_EXT, i.e., {'conf': 'ini', 'ini': 'ini', 'json': 'json', 'py': 'file', 'yaml': 'yaml', 'yml': 'yaml'} Returns ------- cfg: Config """ _check_file(file_path) cpt = _load_config(file_path) params = cpt.configuration_data _update_kaptan(self._cpt, params) def check_file(self, item): """ This is a __getitem__ operator for file path values, if the file does not exist an IOError is raised.""" try: fpath = self.__getitem__(item) except KeyError: raise else: if not op.exists(fpath): raise IOError('Could not find file for key {} in the {}.'.format(item, fpath)) return fpath def update(self, adict): for k, v in adict.items(): self._cpt.upsert(k, v) def keys(self): return self._cpt.configuration_data.keys() def items(self): return self._cpt.configuration_data.items() def get(self, item, default=None): return self._cpt.configuration_data.get(item, default) def __getitem__(self, item): if item not in self._cpt.configuration_data: raise KeyError('Could not find key {} in configuration content.'.format(item)) return self._cpt.configuration_data[item] def __setitem__(self, key, value): return self._cpt.upsert(key, value) def __repr__(self): return '<config.Config> ({})'.format('\n'.join([str(i) for i in self.items()]))