Loading ccnode/tags.py +23 −11 Original line number Diff line number Diff line import inspect import logging import time import weakref from functools import partial logger = logging.getLogger(__name__) Loading @@ -8,19 +10,32 @@ logger = logging.getLogger(__name__) class Tag(object): """``class`` that abstract tags and act as a simple container.""" def __init__(self, name, valuable, ttl=-1): def __init__(self, name, valuable, ttl=-1, parent=None): """ :param string name: tag name :param valuable: something that gives tag value, string or function :param valuable: something that gives tag value, string or callable :param None,int ttl: Time to live for caching the tags, possible values are: * None (means no ttl) * -1 (means infinite ttl) * positive integer in seconds :param object obj: parent object the tag is attached to, it may be needed for the tag callable to process the tag """ self.name = name self._value = valuable self.ttl = ttl self.parent = parent if parent is None else weakref.proxy(parent) if inspect.isfunction(valuable): args_count = len(inspect.getargspec(valuable).args) if args_count > 1: raise Exception('Invalid number of argument for tag function') elif args_count == 1: self._value = partial(valuable, self.parent) else: self._value = valuable else: self._value = lambda: valuable @property def value(self): Loading @@ -28,15 +43,12 @@ class Tag(object): call the function. """ if inspect.isfunction(self._value): try: return self._value() except Exception: logger.exception(u'Calculating tag %s failed:' % self.name) logger.exception(u'Calculating tag %s failed:', self.name) return None return self._value class DynamicTags(object): """Set tags dynamicaly. Loading Loading
ccnode/tags.py +23 −11 Original line number Diff line number Diff line import inspect import logging import time import weakref from functools import partial logger = logging.getLogger(__name__) Loading @@ -8,19 +10,32 @@ logger = logging.getLogger(__name__) class Tag(object): """``class`` that abstract tags and act as a simple container.""" def __init__(self, name, valuable, ttl=-1): def __init__(self, name, valuable, ttl=-1, parent=None): """ :param string name: tag name :param valuable: something that gives tag value, string or function :param valuable: something that gives tag value, string or callable :param None,int ttl: Time to live for caching the tags, possible values are: * None (means no ttl) * -1 (means infinite ttl) * positive integer in seconds :param object obj: parent object the tag is attached to, it may be needed for the tag callable to process the tag """ self.name = name self._value = valuable self.ttl = ttl self.parent = parent if parent is None else weakref.proxy(parent) if inspect.isfunction(valuable): args_count = len(inspect.getargspec(valuable).args) if args_count > 1: raise Exception('Invalid number of argument for tag function') elif args_count == 1: self._value = partial(valuable, self.parent) else: self._value = valuable else: self._value = lambda: valuable @property def value(self): Loading @@ -28,15 +43,12 @@ class Tag(object): call the function. """ if inspect.isfunction(self._value): try: return self._value() except Exception: logger.exception(u'Calculating tag %s failed:' % self.name) logger.exception(u'Calculating tag %s failed:', self.name) return None return self._value class DynamicTags(object): """Set tags dynamicaly. Loading