def test_shortened_syntax(self): cls = List.of_type(Integer) expect(cls.__name__) == "IntegerList" expect(common.attrs[cls]) == {'all': Integer}
# pylint: disable=redefined-outer-name,expression-not-assigned,attribute-defined-outside-init,no-member from expecter import expect import yorm from yorm.types import List, Float from . import strip @yorm.attr(things=List.of_type(Float)) @yorm.sync("tmp/example.yml") class Example: """An example class mapping a list using the shortened syntax.""" def test_list_mapping_using_shortened_syntax(): obj = Example() obj.things = [1, 2.0, "3"] expect(obj.__mapper__.text) == strip(""" things: - 1.0 - 2.0 - 3.0 """)
import yorm from yorm.types import List, Object from profanityfilter import ProfanityFilter import log profanity_filter = ProfanityFilter() profanity_filter.remove_word("damn") @yorm.attr(items=List.of_type(Object)) @yorm.sync("data/cache/{self.name}.yml", auto_resolve=True) class Cache: SIZE = 100 def __init__(self, filtered=True): self.items = [] self.disabled = False self.filtered = filtered @property def name(self): return 'filtered' if self.filtered else 'unfiltered' def add(self, **kwargs): if self._skip_cache(kwargs): return log.info("Caching: %s", kwargs) self.items.insert(0, kwargs)
def test_not_implemented(self): """Verify `List` cannot be used directly.""" with pytest.raises(NotImplementedError): List() with pytest.raises(NotImplementedError): UnknownList()
import os import yorm from yorm.types import String, List from ..domain import Template @yorm.attr(name=String) @yorm.attr(link=String) @yorm.attr(default=List.of_type(String)) @yorm.attr(aliases=List.of_type(String)) @yorm.attr(regexes=List.of_type(String)) @yorm.sync("{self.root}/{self.key}/config.yml") class TemplateModel: """Persistence model for templates.""" def __init__(self, key, root): self.key = key self.root = root self.name = "" self.default = [] self.link = "" self.aliases = [] self.regexes = [] @property def domain(self): return Template( key=self.key, name=self.name,
import logging import yorm from yorm.types import List, Object import profanityfilter log = logging.getLogger(__name__) @yorm.attr(items=List.of_type(Object)) @yorm.sync("data/images/cache.yml") class Cache: SIZE = 9 def __init__(self): self.items = [] def add(self, **kwargs): if kwargs in self.items: log.debug("Already cached: %s", kwargs) return if kwargs['key'] == 'custom': log.debug("Skipped caching of custom: %s", kwargs) return if profanityfilter.is_profane(kwargs['path']): log.debug("Skipped caching of profanity: %s", kwargs) return log.info("Caching: %s", kwargs)
self.command = command self.variables = variables or [] def __str__(self): return f"Environment: {self.name}" def fetch(self): self.variables.clear() result = delegator.run(self.command) for line in result.out.splitlines(): variable = Variable.from_env(line) if variable: self.variables.append(variable) @yorm.attr(sourcefiles=List.of_type(SourceFile)) @yorm.attr(environments=List.of_type(Environment)) @yorm.sync("{self.root}/{self.filename}", auto_create=False, auto_save=False) class Config(yorm.ModelMixin): def __init__(self, filename="env-diff.yml", root=None, sourcefiles=None, environments=None): self.root = root or Path.cwd() self.filename = filename self.sourcefiles = sourcefiles or [] self.environments = environments or [] def __str__(self): return str(self.path)
import os import yorm from yorm.types import String, List from ..domain import Template @yorm.attr(name=String) @yorm.attr(link=String) @yorm.attr(default=List.of_type(String)) @yorm.attr(aliases=List.of_type(String)) @yorm.attr(regexes=List.of_type(String)) @yorm.sync("{self.root}/{self.key}/config.yml") class TemplateModel: """Persistence model for templates.""" def __init__(self, key, root): self.key = key self.root = root self.name = "" self.default = [] self.link = "" self.aliases = [] self.regexes = [] @property def domain(self): return Template( key=self.key, name=self.name, lines=self.default,
from yorm.types import String, List, SortedList from ..domain import Template class UpperString(String): @classmethod def to_data(cls, obj): value = super().to_data(obj) value = value.upper() return value @yorm.attr(name=String) @yorm.attr(link=String) @yorm.attr(default=List.of_type(UpperString)) @yorm.attr(aliases=SortedList.of_type(String)) @yorm.sync("{self.root}/{self.key}/config.yml") class TemplateModel: """Persistence model for templates.""" def __init__(self, key, root): self.key = key self.root = root self.name = "" self.default = [] self.link = "" self.aliases = [] @property def domain(self): return Template(
from yorm.types import String, List from ..domain import Template class UpperString(String): @classmethod def to_data(cls, obj): value = super().to_data(obj) value = value.upper() return value @yorm.attr(name=String) @yorm.attr(link=String) @yorm.attr(default=List.of_type(UpperString)) @yorm.attr(aliases=List.of_type(String)) @yorm.sync("{self.root}/{self.key}/config.yml") class TemplateModel: """Persistence model for templates.""" def __init__(self, key, root): self.key = key self.root = root self.name = "" self.default = [] self.link = "" self.aliases = [] @property def domain(self): return Template(
import logging import yorm from yorm.types import AttributeDictionary, List, String from .. import exceptions log = logging.getLogger(__name__) @yorm.attr(name=String) @yorm.attr(members=List.of_type(String)) class Group(AttributeDictionary): """A group with sources.""" def __init__(self, name, members): super().__init__() self.name = name self.members = members or [] for key in ['name', 'members']: if not self[key]: msg = "'{}' required for {}".format(key, repr(self)) raise exceptions.InvalidConfig(msg) def __repr__(self): return "<group {}>".format(self) def __str__(self):
import logging import warnings import yorm from yorm.types import String, NullableString, List, AttributeDictionary from .. import common, exceptions, shell, git log = logging.getLogger(__name__) @yorm.attr(name=String) @yorm.attr(repo=String) @yorm.attr(rev=String) @yorm.attr(link=NullableString) @yorm.attr(scripts=List.of_type(String)) class Source(AttributeDictionary): """A dictionary of `git` and `ln` arguments.""" DIRTY = '<dirty>' UNKNOWN = '<unknown>' def __init__(self, repo, name=None, rev='master', link=None, scripts=None): super().__init__() self.repo = repo self.name = self._infer_name(repo) if name is None else name self.rev = rev self.link = link self.scripts = scripts or [] for key in ['name', 'repo', 'rev']: