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 engine(self): """ template engine instance """ if not hasattr(self, "_engine"): self._engine = tmpl.get_engine( self.config.get("engine"))(tmpl_dir=self.source()) return self._engine
def __init__(self, model_version, model_type): """ Create a render object. subtype is typically vendor name version is the model version to use """ self.version = model_version self.type = model_type # FIXME use pkg resources search_path = os.path.join(os.getcwd(), "src/netom/templates/", self.version, self.type) #, "bgp") self.engine = tmpl.get_engine("jinja2")(search_path=search_path) self.engine.engine.filters["make_variable_name"] = make_variable_name self.engine.engine.filters["ip_version"] = ip_version
def test_get_engine_fail(): with pytest.raises(KeyError): tmpl.get_engine("none")
def test_get_engine(): for each in engines: assert getattr(tmpl, each) == tmpl.get_engine(each[:-6])
def test_get_engine_fail(): with pytest.raises(KeyError): tmpl.get_engine('none')
def set_search_path(self, search_path): self.engine = tmpl.get_engine("jinja2")(search_path=search_path) self.engine.engine.filters["make_variable_name"] = make_variable_name self.engine.engine.filters["ip_version"] = ip_version