def from_file(cls, config_path=None, *args, **kwargs): """Create a Cihai instance from a JSON or YAML config. :param config_path: path to custom config file :type config_path: str :rtype: :class:`Cihai` """ config_reader = kaptan.Kaptan() config = {} if config_path: if not os.path.exists(config_path): raise exc.CihaiException( '{0} does not exist.'.format(os.path.abspath(config_path))) if not any( config_path.endswith(ext) for ext in ('json', 'yml', 'yaml', 'ini') ): raise exc.CihaiException( '{0} does not have a yaml,yml,json,ini extension.' .format(os.path.abspath(config_path)) ) else: custom_config = config_reader.import_config(config_path).get() config = merge_dict(config, custom_config) return cls(config)
def test_merge_dict(): dict1 = {'hi world': 1, 'innerdict': {'hey': 1}} dict2 = {'innerdict': {'welcome': 2}} expected = {'hi world': 1, 'innerdict': {'hey': 1, 'welcome': 2}} assert util.merge_dict(dict1, dict2) == expected
def __init__(self, config={}): # Merge custom configuration settings on top of defaults self.config = merge_dict(self.default_config, config) #: Expand template variables expand_config(self.config) if not os.path.exists(dirs.user_data_dir): os.makedirs(dirs.user_data_dir) self.engine = create_engine(self.config['database']['url']) self.metadata = MetaData() self.metadata.bind = self.engine self.reflect_db() self.session = Session(self.engine)