| Total Complexity | 11 |
| Total Lines | 70 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | 1 | import os |
|
| 2 | |||
| 3 | 1 | import yorm |
|
|
|
|||
| 4 | 1 | from yorm.types import String, List, SortedList |
|
| 5 | |||
| 6 | 1 | from ..domain import Template |
|
| 7 | |||
| 8 | |||
| 9 | 1 | class UpperString(String): |
|
| 10 | |||
| 11 | 1 | @classmethod |
|
| 12 | def to_data(cls, obj): |
||
| 13 | 1 | value = super().to_data(obj) |
|
| 14 | 1 | value = value.upper() |
|
| 15 | 1 | return value |
|
| 16 | |||
| 17 | |||
| 18 | 1 | @yorm.attr(name=String) |
|
| 19 | 1 | @yorm.attr(link=String) |
|
| 20 | 1 | @yorm.attr(default=List.of_type(UpperString)) |
|
| 21 | 1 | @yorm.attr(aliases=SortedList.of_type(String)) |
|
| 22 | 1 | @yorm.sync("{self.root}/{self.key}/config.yml") |
|
| 23 | class TemplateModel: |
||
| 24 | """Persistence model for templates.""" |
||
| 25 | |||
| 26 | 1 | def __init__(self, key, root): |
|
| 27 | 1 | self.key = key |
|
| 28 | 1 | self.root = root |
|
| 29 | 1 | self.name = "" |
|
| 30 | 1 | self.default = [] |
|
| 31 | 1 | self.link = "" |
|
| 32 | 1 | self.aliases = [] |
|
| 33 | |||
| 34 | 1 | @property |
|
| 35 | def domain(self): |
||
| 36 | 1 | return Template( |
|
| 37 | key=self.key, |
||
| 38 | name=self.name, |
||
| 39 | lines=self.default, |
||
| 40 | aliases=self.aliases, |
||
| 41 | link=self.link, |
||
| 42 | root=self.root, |
||
| 43 | ) |
||
| 44 | |||
| 45 | |||
| 46 | 1 | class TemplateStore: |
|
| 47 | |||
| 48 | 1 | def __init__(self, root): |
|
| 49 | 1 | self.root = root |
|
| 50 | 1 | self._items = {} |
|
| 51 | 1 | for key in os.listdir(self.root): |
|
| 52 | 1 | if key[0] not in ('.', '_'): |
|
| 53 | 1 | model = TemplateModel(key, self.root) |
|
| 54 | 1 | yorm.save(model) |
|
| 55 | 1 | self._items[key] = model |
|
| 56 | |||
| 57 | 1 | def read(self, key): |
|
| 58 | 1 | try: |
|
| 59 | 1 | model = self._items[key] |
|
| 60 | 1 | except KeyError: |
|
| 61 | 1 | return None |
|
| 62 | else: |
||
| 63 | 1 | return model.domain |
|
| 64 | |||
| 65 | 1 | def filter(self, **_): |
|
| 66 | 1 | templates = [] |
|
| 67 | 1 | for model in self._items.values(): |
|
| 68 | 1 | templates.append(model.domain) |
|
| 69 | return templates |
||
| 70 |