Passed
Pull Request — master (#3188)
by Alexander
02:25
created

ssg.jinja   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 91
rs 10
c 0
b 0
f 0
ccs 40
cts 45
cp 0.8889
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A AbsolutePathFileSystemLoader.__init__() 0 2 1
A AbsolutePathFileSystemLoader.get_source() 0 20 4

3 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_jinja_environment() 0 21 3
A extract_substitutions_dict_from_template() 0 14 3
A process_file() 0 4 1
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
6
7 1
from .utils import required_key
8
9
10 1
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 1
    def __init__(self, encoding='utf-8'):
21 1
        self.encoding = encoding
22
23 1
    def get_source(self, environment, template):
24 1
        if not os.path.isabs(template):
25
            raise jinja2.TemplateNotFound(template)
26
27 1
        template_file = jinja2.utils.open_if_exists(template)
28 1
        if template_file is None:
29
            raise jinja2.TemplateNotFound(template)
30 1
        try:
31 1
            contents = template_file.read().decode(self.encoding)
32
        finally:
33 1
            template_file.close()
34
35 1
        mtime = os.path.getmtime(template)
36
37 1
        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 1
            try:
39 1
                return os.path.getmtime(template) == mtime
40
            except OSError:
41
                return False
42 1
        return contents, template, uptodate
43
44
45 1
def _get_jinja_environment(substitutions_dict):
46 1
    if _get_jinja_environment.env is None:
47 1
        bytecode_cache = None
48 1
        if substitutions_dict.get("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 1
        _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 1
    return _get_jinja_environment.env
66
67
68 1
_get_jinja_environment.env = None
69
70
71 1
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 1
    template = _get_jinja_environment(substitutions_dict).get_template(filename)
78 1
    all_symbols = template.make_module(substitutions_dict).__dict__
79 1
    symbols_to_export = dict()
80 1
    for name, symbol in all_symbols.items():
81 1
        if name.startswith("_"):
82 1
            continue
83 1
        symbols_to_export[name] = symbol
84 1
    return symbols_to_export
85
86
87 1
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 1
    filepath = os.path.abspath(filepath)
89 1
    template = _get_jinja_environment(substitutions_dict).get_template(filepath)
90
    return template.render(substitutions_dict)
91