Esempio n. 1
0
class Template(object):
    def __init__(self, root_dir, ops_config):
        loader = ChoiceLoader(
            [FileSystemLoader(root_dir),
             FileSystemLoader("/")])

        mode = ops_config.get('jinja2.undefined')
        undefined = Undefined
        if mode == 'StrictUndefined':
            undefined = StrictUndefined
        elif mode == 'DebugUndefined':
            undefined = DebugUndefined

        self.env = Environment(loader=loader, undefined=undefined)

        self.filter_plugin_loader = PluginLoader(
            'FilterModule', 'ansible.plugins.filter',
            ops_config.ansible_filter_plugins.split(':'), 'filter_plugins')

        for filter in self.filter_plugin_loader.all():
            self.env.filters.update(filter.filters())

    def render(self, source, vars):
        jinja_template = self.env.get_template(source)

        return jinja_template.render(**vars)
Esempio n. 2
0
    def test_all_no_duplicate_names(self, gp_mock, glob_mock):
        '''
        This test goes along with ``test__load_module_source_no_duplicate_names``
        and ensures that we ignore duplicate imports on multiple paths
        '''

        fixture_path = os.path.join(os.path.dirname(__file__), 'loader_fixtures')

        gp_mock.return_value = [
            fixture_path,
            '/path/to'
        ]

        glob_mock.glob.side_effect = [
            [os.path.join(fixture_path, 'import_fixture.py')],
            ['/path/to/import_fixture.py']
        ]

        pl = PluginLoader('test', '', 'test', 'test_plugin')
        # Aside from needing ``list()`` so we can do a len, ``PluginLoader.all`` returns a generator
        # so ``list()`` actually causes ``PluginLoader.all`` to run.
        plugins = list(pl.all())
        self.assertEqual(len(plugins), 1)

        self.assertIn(os.path.join(fixture_path, 'import_fixture.py'), pl._module_cache)
        self.assertNotIn('/path/to/import_fixture.py', pl._module_cache)
Esempio n. 3
0
    def test_all_no_duplicate_names(self, gp_mock, glob_mock):
        '''
        This test goes along with ``test__load_module_source_no_duplicate_names``
        and ensures that we ignore duplicate imports on multiple paths
        '''

        fixture_path = os.path.join(os.path.dirname(__file__), 'loader_fixtures')

        gp_mock.return_value = [
            fixture_path,
            '/path/to'
        ]

        glob_mock.glob.side_effect = [
            [os.path.join(fixture_path, 'import_fixture.py')],
            ['/path/to/import_fixture.py']
        ]

        pl = PluginLoader('test', '', 'test', 'test_plugin')
        # Aside from needing ``list()`` so we can do a len, ``PluginLoader.all`` returns a generator
        # so ``list()`` actually causes ``PluginLoader.all`` to run.
        plugins = list(pl.all())
        self.assertEqual(len(plugins), 1)

        self.assertIn(os.path.join(fixture_path, 'import_fixture.py'), pl._module_cache)
        self.assertNotIn('/path/to/import_fixture.py', pl._module_cache)