Beispiel #1
0
 def try_access(data, **context):
     try:
         result = access(key)(data)
     except Exception:
         return False
     else:  # make sure validation errors not caught
         return tests(result, **context)
Beispiel #2
0
def retrieve(data, form, *args):
    """
    Using accessors specified in form,
    retrieves all values from dictionary 'data'.
    form = {
        "value": accessor_function,
    }

    If accessor is a dict, recurses.
    If accessor is a str/unicode, splits on "/"

    Returns dictionary if succeeded. If accessor raises Exception,
    will return None (should be filtered out)
    and prints and logs a message about that entry.
    """
    entry = {}
    for tupl in form.items():
        key, accessor = tuple(tupl)  # just in case it's a list

        #
        # if accessor is a dict, recurse
        #
        if isinstance(accessor, c.DICTIONARIES):
            entry[key] = retrieve(data, accessor, *args)
            if entry[key] is None:  # retrieve failed
                return None  # filter it out
        #
        # else, access the value
        #
        else:
            #
            # if value is a string, use standard accessor with / as lister
            #
            if isinstance(accessor, c.STRINGS):
                accessor = access(accessor.split("/"))

            #
            # try to access the value from the data
            #
            try:
                entry[key] = accessor(data, *args)
            except Exception as e:
                #
                # accessing failed. Log message
                # and return None so it gets filtered out.
                #
                msg = "({}, {}): {}\nSkipping {}.".format(*(key,
                                                            accessor.__name__,
                                                            str(e), data))
                # logger.debug(msg)
                print msg
                return None
    return entry
Beispiel #3
0
 def refer(data, entries):
     finds = 0
     for item in entries:
         acc = access(key, flatten=flatten)(item)
         if isinstance(acc, c.ITERABLES):
             result = data in acc
         else:
             result = data == acc
         if result:
             finds += 1
             if max_finds and finds > max_finds:
                 return False
     return finds >= min_finds
Beispiel #4
0
 def __getitem__(self, name):
     result = access(name, split=self.__splitter)(self.__data)
     if (isinstance(result, c.ITERABLES)
             or isinstance(result, c.DICTIONARIES)):
         return Wrapper(result, splitter=self.__splitter)
     return result
Beispiel #5
0
 def get_ter(**kwargs):
     keypath = name[4:].split(self.__splitter)
     try:
         return access(keypath, **kwargs)(self.__data)
     except KeyError:
         raise AttributeError("Cannot access {}".format(keypath))