diff --git a/napalm_base/helpers.py b/napalm_base/helpers.py index ed7d63f5..467c71d8 100644 --- a/napalm_base/helpers.py +++ b/napalm_base/helpers.py @@ -20,22 +20,31 @@ from napalm_base.utils.jinja_filters import CustomJinjaFilters from napalm_base.utils import py23_compat - # ---------------------------------------------------------------------------------------------------------------------- # helper classes -- will not be exported # ---------------------------------------------------------------------------------------------------------------------- + + class _MACFormat(mac_unix): pass _MACFormat.word_fmt = '%.2X' - # ---------------------------------------------------------------------------------------------------------------------- # callable helpers # ---------------------------------------------------------------------------------------------------------------------- + + def load_template(cls, template_name, template_source=None, template_path=None, openconfig=False, **template_vars): + used_config_format = None + if hasattr(cls, '_config_format'): + # let's hack this + # if the user set a specific configuration format, override it to `text` + # when applying the native NAPALM template + used_config_format = cls._config_format + cls._config_format = 'text' try: if isinstance(template_source, py23_compat.string_types): template = jinja2.Template(template_source) @@ -65,6 +74,8 @@ def load_template(cls, template_name, template_source=None, template_path=None, for filter_name, filter_function in CustomJinjaFilters.filters().items(): environment.filters[filter_name] = filter_function + # TODO: use the `used_config_format` variable to load the right file + template = environment.get_template('{template_name}.j2'.format( template_name=template_name )) @@ -83,7 +94,14 @@ def load_template(cls, template_name, template_source=None, template_path=None, error=jinjaerr.message ) ) - return cls.load_merge_candidate(config=configuration) + else: + # all good, let's merge + cls.load_merge_candidate(config=configuration) + finally: + # no matter what, let's revert config format to the previous state + if used_config_format: + cls._config_format = used_config_format + return True def textfsm_extractor(cls, template_name, raw_text):