Пример #1
0
 def wrap(self, val):
     if is_atomic(val):
         return val
     elif is_listy(val):
         return DibListProxy(val)
     elif is_dicty(val):
         return DibWrap(val)
     else:
         return val
Пример #2
0
def _deep_setdefault(d, defaults):
    """
    >>> d = {'a': 1}
    >>> defaults = {'a': 0, 'b': 0}
    >>> _deep_setdefault(d, defaults)
    >>> d['a'] == 1
    True
    >>> d['b'] == 0
    True
    
    >>> d = {'a': {'b': 1}}
    >>> defaults = {'a': {'b': 0, 'c': 0}}
    >>> _deep_setdefault(d, defaults)
    >>> d['a']['b'] == 1
    True
    >>> d['a']['c'] == 0
    True
    
    >>> d = {'a': 'foo'}
    >>> defaults = {'a': {'b': 0}}
    >>> _deep_setdefault(d, defaults)
    >>> d['a'] == 'foo'
    True

    >>> d = {'a': {'b': 0}}
    >>> defaults = {'a': 'foo'}
    >>> _deep_setdefault(d, defaults)
    >>> d['a']['b'] == 0
    True

    """
    for k, v in defaults.items():
        if not k in d:
            d[k] = deepcopy(v)
        else:
            if is_dicty(d[k]) and is_dicty(defaults[k]):
                _deep_setdefault(d[k], defaults[k])
Пример #3
0
def _dibjectify(ob):
    """
    extracts whatever looks like a concoction of 
    dicts, lists and atomic types in ob and 
    recreates them as dicts, lists and atomic types

    dicts come back as the dict subclass Dibject
    which allows keys to be accessed as attributes
    like feedparser.
    """
    if ob is None:
        return None

    if is_atomic(ob):
        if isinstance(ob, UnicodeType):
            return UnicodeType(ob)
        elif isinstance(ob, StringType):
            return StringType(ob)
        elif isinstance(ob, IntType):
            return IntType(ob)
        elif isinstance(ob, FloatType):
            return FloatType(ob)
        elif isinstance(ob, LongType):
            return LongType(ob)
        else:
            log.debug('Skipping "%s" of atomic type %s' % (ob, ob.__class__))
            return None

    elif is_dicty(ob):
        d = Dibject()
        for (k, v) in ob.items():
            fk = dibjectify(k)
            fv = dibjectify(v)
            if fv is not None and fk is not None:
                d[fk] = fv
        return d

    elif is_listy(ob):
        l = []
        for x in ob:
            fv = dibjectify(x)
            if fv is not None:
                l.append(fv)
        return l

    else:
        log.debug('Skipping "%s" of unserializable type %s' % (ob, ob.__class__))
        return None
Пример #4
0
    def __call__(self, news_item):
        news_item = news_item.load_full_item(self.context.db)
        path = self.config.get('field', None)
        if path is None:
            return False

        path = path.split('.')
        val = news_item.details
        for node in path:
            if is_dicty(val):
                val = val.get(node, None)
            else:
                return False
            
            if val is None:
                break
        if val is None:
            return False
    
        return self._match(val)