Test Failed
Pull Request — master (#3170)
by Matěj
02:13
created

extract_substitutions_dict_from_template()   A

Complexity

Conditions 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nop 2
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 12
rs 9.95
c 0
b 0
f 0
1 1
from __future__ import absolute_import
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2 1
from __future__ import print_function
3
4 1
import os.path
5 1
import jinja2
0 ignored issues
show
introduced by
Unable to import 'jinja2'
Loading history...
6
7
from .utils import required_key
8
9
10
class AbsolutePathFileSystemLoader(jinja2.BaseLoader):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
11
    """Loads templates from the file system. This loader insists on absolute
12
    paths and fails if a relative path is provided.
13
14
    >>> loader = AbsolutePathFileSystemLoader()
15
16
    Per default the template encoding is ``'utf-8'`` which can be changed
17
    by setting the `encoding` parameter to something else.
18
    """
19
20
    def __init__(self, encoding='utf-8'):
21
        self.encoding = encoding
22
23
    def get_source(self, environment, template):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The argument environment seems to be unused.
Loading history...
24
        if not os.path.isabs(template):
25
            raise jinja2.TemplateNotFound(template)
26
27
        template_file = jinja2.utils.open_if_exists(template)
28
        if template_file is None:
29
            raise jinja2.TemplateNotFound(template)
30
        try:
31
            contents = template_file.read().decode(self.encoding)
32
        finally:
33
            template_file.close()
34
35
        mtime = os.path.getmtime(template)
36
37
        def uptodate():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
38
            try:
39
                return os.path.getmtime(template) == mtime
40
            except OSError:
41
                return False
42
        return contents, template, uptodate
43
44
45
def _get_jinja_environment(substitutions_dict):
46
    if _get_jinja_environment.env is None:
47
        bytecode_cache = None
48
        if required_key(substitutions_dict, "jinja2_cache_enabled") == "true":
49
            bytecode_cache = jinja2.FileSystemBytecodeCache(
50
                required_key(substitutions_dict, "jinja2_cache_dir")
51
            )
52
53
        # TODO: Choose better syntax?
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
54
        _get_jinja_environment.env = jinja2.Environment(
55
            block_start_string="{{%",
56
            block_end_string="%}}",
57
            variable_start_string="{{{",
58
            variable_end_string="}}}",
59
            comment_start_string="{{#",
60
            comment_end_string="#}}",
61
            loader=AbsolutePathFileSystemLoader(),
62
            bytecode_cache=bytecode_cache
63
        )
64
65
    return _get_jinja_environment.env
66
67
68
_get_jinja_environment.env = None
69
70
71
def extract_substitutions_dict_from_template(filename, substitutions_dict):
0 ignored issues
show
Coding Style Naming introduced by
The name extract_substitutions_dict_from_template does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
72
    """
73
    Treat the given filename as a jinja2 file containing macro definitions,
74
    and export definitions that don't start with _ as a name->macro dictionary.
75
    During macro compilation, symbols from substitutions_dict may be used in those definitions.
76
    """
77
    template = _get_jinja_environment(substitutions_dict).get_template(filename)
78
    all_symbols = template.make_module(substitutions_dict).__dict__
79
    symbols_to_export = dict()
80
    for name, symbol in all_symbols.items():
81
        if name.startswith("_"):
82
            continue
83
        symbols_to_export[name] = symbol
84
    return symbols_to_export
85
86
87
def process_file(filepath, substitutions_dict):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
88
    filepath = os.path.abspath(filepath)
89
    template = _get_jinja_environment(substitutions_dict).get_template(filepath)
90
    return template.render(substitutions_dict)
91