Ejemplo n.º 1
0
def get_field_dump_as_list(meta_tree, *key_trail):
    metadata = get_metadata(meta_tree, *key_trail)

    ret = None
    for key, value in metadata.items():
        add_val = listify(value)
        if add_val:
            if ret is None:
                ret = []
            ret += add_val

    return listify(ret)
Ejemplo n.º 2
0
 def _helper(node, field_name):
     meta_data = node.metadata
     if field_name in meta_data:
         return listify(meta_data[field_name])
     else:
         child_agg = None
         for key in node.keys():
             child_ret = _helper(node[key], field_name)
             if child_ret is not None:
                 if child_agg is None:
                     child_agg = []
                 child_agg += child_ret
         return listify(child_agg)
 def withClaimTest(self, hitItem, P, Q, descr, orNone=True):
     """
     Base test that an item contains a certain claim
     parm hitItem: item to check
     param P: the property to look for
     param Q: (list) of Q claim to look for
     param descr: a descriptive text
     param orNone: if no Claim is also ok
     return bool
     """
     P = u'P%s' % P.lstrip('P')
     Q = helpers.listify(Q)
     testItems = []
     for q in Q:
         testItems.append(self.wd.QtoItemPage(q))
     # check claims
     if P in hitItem.claims.keys():
         for testItem in testItems:
             if self.wd.hasClaim(P, testItem, hitItem):
                 return True
         else:
             pywikibot.output(u'%s is identified as something other '
                              u'than a %s. Check!' %
                              (hitItem.title(), descr))
             return False
     elif orNone:  # no P claim
         return True
Ejemplo n.º 4
0
def index_ancestors(meta_tree, field_name, *key_trail):
    ancestor_metadata = get_ancestor_metadata_trail(meta_tree, *key_trail)
    for am in reversed(ancestor_metadata):
        if field_name in am:
            return helpers.listify(am[field_name])

    return None
Ejemplo n.º 5
0
    def ret(meta_tree, field_name, *key_trail):
        for count, key in enumerate(key_trail, start=1):
            lbl = get_label_from_key(key)
            if lbl == label:
                target_key_trail = key_trail[:count]
                return index(meta_tree, field_name, *target_key_trail)

        return listify(None)
Ejemplo n.º 6
0
def lookup(meta_tree, field_name, *key_trail, indexers=None):
    if indexers is None:
        indexers = (index, index_ancestors, index_children)
    # elif not isinstance(indexers, (list, tuple)):
    #     indexers = (indexers,)

    for i in indexers:
        ret = i(meta_tree, field_name, *key_trail)
        if ret is not None:
            return ret
    return listify(None)
 def __init__(self, keys, values, target, viaId=None):
     """
     keys: list|string|None of keys which must be present
           (in addition to value/target)
     values: a dict|None of key-value pairs which must be present
     target: the key for which the value is wanted
     viaId: if not None then the value of target should be matched to
            an @id entry where this key should be used
     """
     self.keys = []
     if keys is not None:
         self.keys += helpers.listify(keys)
     self.values = values
     if values is not None:
         self.keys += values.keys()
     self.target = target
     self.keys.append(target)
     self.viaId = viaId
    def get_nationality(bot, values):
        """Get the nationality/nationalities.

        @param bot: the instance of the bot calling upon the template
        @param bot: KulturnavBot
        @param values: the values extracted using the rules
        @type values: dict
        @return: nationalities
        @rtype: list of WD.Statement
        """
        if values.get(u'person.nationality'):
            # there can be multiple values
            values[u'person.nationality'] = helpers.listify(
                values[u'person.nationality'])
            claim = []
            for pn in values[u'person.nationality']:
                claim.append(WD.Statement(bot.location2Wikidata(pn)))
            if claim:
                return claim
Ejemplo n.º 9
0
def index_children(meta_tree, field_name, *key_trail):
    def _helper(node, field_name):
        meta_data = node.metadata
        if field_name in meta_data:
            return listify(meta_data[field_name])
        else:
            child_agg = None
            for key in node.keys():
                child_ret = _helper(node[key], field_name)
                if child_ret is not None:
                    if child_agg is None:
                        child_agg = []
                    child_agg += child_ret
            return listify(child_agg)

    node = get_node(meta_tree, *key_trail)
    ret = None
    for key in node.keys():
        child_agg = _helper(node[key], field_name)
        if child_agg is not None:
            if ret is None:
                ret = []
            ret += child_agg
    return listify(ret)
Ejemplo n.º 10
0
def get_field_as_list(meta_tree, field_name, *key_trail):
    return listify(get_field(meta_tree, field_name, *key_trail))
Ejemplo n.º 11
0
def index(meta_tree, field_name, *key_trail):
    val = get_metadata(meta_tree, *key_trail).get(field_name)
    return listify(val)
 def listify(value):
     """
     Deprecated in favour of helpers.listify
     """
     print 'call to deprecated KulturnavBot.listify()'
     return helpers.listify(value)
Ejemplo n.º 13
0
def index(meta_tree, field_name, *key_trail):
    meta_data = get_metadata(meta_tree, *key_trail)
    val = meta_data.get(field_name)
    return helpers.listify(val)