Ejemplo n.º 1
0
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}")
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
def test_get_engine_fail():
    with pytest.raises(KeyError):
        tmpl.get_engine("none")
Ejemplo n.º 5
0
def test_get_engine():
    for each in engines:
        assert getattr(tmpl, each) == tmpl.get_engine(each[:-6])
Ejemplo n.º 6
0
def test_get_engine_fail():
    with pytest.raises(KeyError):
        tmpl.get_engine('none')
Ejemplo n.º 7
0
def test_get_engine():
    for each in engines:
        assert getattr(tmpl, each) == tmpl.get_engine(each[:-6])
Ejemplo n.º 8
0
 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