def test_load_datafile(codec, dataset): obj = codec.cls() with pytest.raises(IOError): munge.load_datafile('nonexistant', data_dir) # default value assert None == munge.load_datafile('nonexistant', data_dir, default=None) assert 'DEFAULT' == munge.load_datafile('nonexistant', data_dir, default='DEFAULT') data = munge.load_datafile(dataset.filename, this_dir) assert data # test hardset extension assert obj.extensions for ext in obj.extensions: fq_path = "%s.%s" % (dataset.filename, ext) data = munge.load_datafile(fq_path, this_dir, default=None) if data: break data = None assert data # test default search path '.' #files = munge.find_datafile('set0', data_dir) files = munge.find_datafile(fq_path, this_dir) assert 1 == len(files) relpath = os.path.relpath(files[0][1]) data = munge.load_datafile(relpath) assert data with pytest.raises(IOError): munge.load_datafile(dataset.filename, this_dir, codecs={})
def read_config(schema, config_dir, config_name="config", ctx=None): """ read a config file from config_dir """ conf_path = os.path.expanduser(config_dir) if not os.path.exists(conf_path): raise OSError(f"config dir not found at {conf_path}") for codec, filename in munge.find_datafile(config_name, conf_path): if tmpl: # if twentyc.tmpl was imported, render config before # loading it. engine = tmpl.get_engine("jinja2")(search_path=os.path.dirname(filename)) engine.engine.undefined = IgnoreUndefined # TODO need a streamlined way to load in template filters engine.engine.filters["escape_regex"] = filter_escape_regex data = codec().loads( engine._render(src=os.path.basename(filename), env=ctx.tmpl["env"]) ) ctx.tmpl.update(engine=engine) else: with open(filename) as fobj: data = codec().load(fobj) meta = dict(config_dir=config_dir, config_file=filename) return confu.config.Config(schema, data, meta) raise OSError(f"config dir not found at {conf_path}")
def test_find_datafile(codec, dataset): obj = codec.cls() print(dataset.filename) print(data_dir) files = munge.find_datafile('set0', data_dir) # should == number of codec tests assert files assert not munge.find_datafile('nonexistant', []) # test codec override files = munge.find_datafile(dataset.filename, this_dir, {('json',): munge.get_codecs()[('json',)]}) assert 1 == len(files) load = files[0] assert os.path.join(this_dir, dataset.filename + '.json') == load[1] assert hasattr(load[0], 'load') assert dataset.expected == load[0]().load(open(load[1]))