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