Completed
Pull Request — master (#177)
by Jace
08:56
created

load_before()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
cc 3
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
1 1
import os
2
3 1
import yorm
0 ignored issues
show
Configuration introduced by
The import yorm could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
from yorm.types import String, List
0 ignored issues
show
Configuration introduced by
The import yorm.types could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5 1
6
from ..domain import Template
7
8 1
9 1
@yorm.attr(name=String)
10 1
@yorm.attr(link=String)
11
@yorm.attr(default=List.of_type(String))
12
@yorm.attr(aliases=List.of_type(String))
13 1
@yorm.attr(regexes=List.of_type(String))
14 1
@yorm.sync("{self.root}/{self.key}/config.yml")
15 1
class TemplateModel:
16 1
    """Persistence model for templates."""
17 1
18 1
    def __init__(self, key, root):
19 1
        self.key = key
20
        self.root = root
21
        self.name = ""
22
        self.default = []
23 1
        self.link = ""
24 1
        self.aliases = []
25 1
        self.regexes = []
26
27 1
    @property
28
    def domain(self):
29
        return Template(
30 1
            key=self.key,
31
            name=self.name,
32
            lines=self.default if any(self.default) else [],
33
            aliases=self.aliases,
34
            patterns=self.regexes,
35
            link=self.link,
36
            root=self.root,
37
        )
38
39
40
class TemplateStore:
41 1
42 1
    def __init__(self, root):
43
        self.root = root
44 1
        self._items = {}
45 1
        for key in os.listdir(self.root):
46 1
            if key[0] not in ('.', '_'):
47 1
                model = TemplateModel(key, self.root)
48
                yorm.update_file(model)
49
                self._items[key] = model
50 1
51
    def read(self, key):
52 1
        try:
53 1
            model = self._items[key]
54 1
        except KeyError:
55
            return None
56 1
        else:
57
            return model.domain
58 1
59 1
    def filter(self, **_):
60 1
        templates = []
61 1
        for model in self._items.values():
62
            templates.append(model.domain)
63
        return templates
64