def update(self): dct = Metadata() dct.update({'hello.world': 1}) assert 'hello' in dct assert dct.hello.world == 1
def redirects(self): dct = Metadata() alist = [1, 2, 3] dct['foo'] = alist dct.redirect('foo', 'baz') assert 'foo' not in dct assert 'baz' in dct assert dct['baz'] == alist
def __init__(self, conf, meta): self.props = Metadata((k, v) for k, v in iteritems(conf) if k in ['author', 'lang', 'email', 'date_format', 'entry_permalink', 'page_permalink']) self.props.update(meta) self.type = meta.get('type', 'entry') # redirect singular -> plural for key, to in [('tag', 'tags'), ('filter', 'filters'), ('template', 'layout')]: if key in self.props: self.props.redirect(key, to) self.filters = self.props.get('filters', []) self.hashvalue = hash(self.filename, self.title, self.date.ctime())
def works(self): dct = Metadata() dct['hello.world'] = 1 assert dct['hello']['world'] == 1 assert dct.hello.world == 1 try: dct.foo dct.foo.bar except KeyError: assert True else: assert False dct['hello.foreigner'] = 2 assert dct['hello']['world'] == 1 assert dct.hello.world == 1 assert dct.hello.foreigner == 2
class Reader(compat.metaclass(abc.ABCMeta, object)): """This class represents a single entry. Every property from this class is available during templating including custom key-value pairs from the header. The formal structure is first a YAML with some key/value pairs and then the actual content. For example:: --- title: My Title date: 12.04.2012, 14:12 tags: [some, values] custom: key example image: /path/to/my/image.png --- Here we start! Where you can access the image path via ``entry.image``. For convenience Acrylamid maps "filter" and "tag" automatically to "filters" and "tags" and also converts a single string into an array containing only one string. :param filename: valid path to an entry :param conf: acrylamid configuration .. attribute:: lang Language used in this article. This is important for the hyphenation pattern.""" def __init__(self, conf, meta): self.props = Metadata((k, v) for k, v in iteritems(conf) if k in ['author', 'lang', 'email', 'date_format', 'entry_permalink', 'page_permalink']) self.props.update(meta) self.type = meta.get('type', 'entry') # redirect singular -> plural for key, to in [('tag', 'tags'), ('filter', 'filters'), ('template', 'layout')]: if key in self.props: self.props.redirect(key, to) self.filters = self.props.get('filters', []) self.hashvalue = hash(self.filename, self.title, self.date.ctime()) @abc.abstractmethod def __hash__(self): return @abc.abstractproperty def source(self): return @abc.abstractproperty def modified(self): return @abc.abstractproperty def lastmodified(self): return def getfilters(self): return self._filters def setfilters(self, filters): if isinstance(filters, string_types): filters = [filters] self._filters = FilterTree(filters) filters = property(getfilters, setfilters) def gettype(self): """="Type of this entry. Can be either ``'entry'`` or ``'page'``""" return self._type def settype(self, value): if value not in ('entry', 'page'): raise ValueError("item type must be 'entry' or 'page'") self._type = value type = property(gettype, settype, doc=gettype.__doc__) def hasproperty(self, prop): """Test whether BaseEntry has prop in `self.props`.""" return prop in self.props @property def date(self): return datetime.now() def __iter__(self): for key in self.props: yield key for key in (attr for attr in dir(self) if not attr.startswith('_')): yield key def __contains__(self, other): return other in self.props or other in self.__dict__ def __getattr__(self, attr): try: return self.props[attr] except KeyError: raise AttributeError(attr) __getitem__ = lambda self, attr: getattr(self, attr)
class Reader(compat.metaclass(abc.ABCMeta, object)): """This class represents a single entry. Every property from this class is available during templating including custom key-value pairs from the header. The formal structure is first a YAML with some key/value pairs and then the actual content. For example:: --- title: My Title date: 12.04.2012, 14:12 tags: [some, values] custom: key example image: /path/to/my/image.png --- Here we start! Where you can access the image path via ``entry.image``. For convenience Acrylamid maps "filter" and "tag" automatically to "filters" and "tags" and also converts a single string into an array containing only one string. :param filename: valid path to an entry :param conf: acrylamid configuration .. attribute:: lang Language used in this article. This is important for the hyphenation pattern.""" def __init__(self, conf, meta): self.props = Metadata((k, v) for k, v in iteritems(conf) if k in [ 'author', 'lang', 'email', 'date_format', 'entry_permalink', 'page_permalink' ]) self.props.update(meta) self.type = meta.get('type', 'entry') # redirect singular -> plural for key, to in [('tag', 'tags'), ('filter', 'filters'), ('template', 'layout')]: if key in self.props: self.props.redirect(key, to) self.filters = self.props.get('filters', []) self.hashvalue = hash(self.filename, self.title, self.date.ctime()) @abc.abstractmethod def __hash__(self): return @abc.abstractproperty def source(self): return @abc.abstractproperty def modified(self): return @abc.abstractproperty def lastmodified(self): return def getfilters(self): return self._filters def setfilters(self, filters): if isinstance(filters, string_types): filters = [filters] self._filters = FilterTree(filters) filters = property(getfilters, setfilters) def gettype(self): """="Type of this entry. Can be either ``'entry'`` or ``'page'``""" return self._type def settype(self, value): if value not in ('entry', 'page'): raise ValueError("item type must be 'entry' or 'page'") self._type = value type = property(gettype, settype, doc=gettype.__doc__) def hasproperty(self, prop): """Test whether BaseEntry has prop in `self.props`.""" return prop in self.props @property def date(self): return datetime.now() def __iter__(self): for key in self.props: yield key for key in (attr for attr in dir(self) if not attr.startswith('_')): yield key def __contains__(self, other): return other in self.props or other in self.__dict__ def __getattr__(self, attr): try: return self.props[attr] except KeyError: raise AttributeError(attr) __getitem__ = lambda self, attr: getattr(self, attr)
def init(self): assert Metadata({'hello.world': 1}).hello.world == 1
class Reader(object): __metaclass__ = abc.ABCMeta def __init__(self, conf, meta): self.props = Metadata((k, v) for k, v in conf.iteritems() if k in ['author', 'lang', 'encoding', 'email', 'date_format', 'entry_permalink', 'page_permalink']) self.props.update(meta) self.type = meta.get('type', 'entry') # redirect singular -> plural for key, to in {'tag': 'tags', 'filter': 'filters'}.iteritems(): if key in self.props: self.props.redirect(key, to) self.filters = self.props.get('filters', []) self.hashvalue = hash(self.filename, self.title, self.date.ctime()) @abc.abstractmethod def __hash__(self): return @abc.abstractproperty def source(self): return @abc.abstractproperty def modified(self): return @abc.abstractproperty def lastmodified(self): return def getfilters(self): return self._filters def setfilters(self, filters): if isinstance(filters, basestring): filters = [filters] self._filters = FilterTree(filters) filters = property(getfilters, setfilters) def gettype(self): """="Type of this entry. Can be either ``'entry'`` or ``'page'``""" return self._type def settype(self, value): if value not in ('entry', 'page'): raise ValueError("item type must be 'entry' or 'page'") self._type = value type = property(gettype, settype, doc=gettype.__doc__) def hasproperty(self, prop): """Test whether BaseEntry has prop in `self.props`.""" return prop in self.props @property def date(self): return datetime.now() def __iter__(self): for key in self.props: yield key for key in (attr for attr in dir(self) if not attr.startswith('_')): yield key def __contains__(self, other): return other in self.props or other in self.__dict__ def __getattr__(self, attr): try: return self.props[attr] except KeyError: raise AttributeError(attr) __getitem__ = lambda self, attr: getattr(self, attr)