diff options
author | bnewbold <bnewbold@robocracy.org> | 2016-01-25 14:13:14 -0800 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2016-01-25 14:13:14 -0800 |
commit | beccf5514c05370732a83438f9bbe9e216367fb7 (patch) | |
tree | c781ef4251e9bb3da78fd1202710a35b1c476531 | |
parent | 161ad38959e7bea6967760c0418f90850f77a9c9 (diff) | |
download | basebasebase-master.tar.gz basebasebase-master.zip |
-rw-r--r-- | bbb/bbb.py | 42 |
1 files changed, 38 insertions, 4 deletions
@@ -170,29 +170,36 @@ class WikibaseServer: return entities def get_items(self, query, **kwargs): + if len(query) == 0: + return [] as_titles = False if type(query[0]) is int: # Convert list of ints to QIDs query = ["Q%d" % x for x in query] + elif type(query[0]) is str and query[0][0] in "P" and query[0][1:].isdigit(): + raise ValueError("query must be a list of QIDs, not PIDs") elif not (type(query[0]) is str and query[0][0] in "Q" and query[0][1:].isdigit()): # Must be list of titles as_titles = True try: entities = self._get_entities(query, as_titles=as_titles, expected='item', **kwargs) - except MissingEntityError as wee: + except MissingItemError as wee: # Case entity error to item error raise MissingItemError(id=wee.id, title=wee.title) items = [WikibaseItem.from_dict(e) for e in entities] return items def get_item(self, query, **kwargs): - return self.get_items((query, ), **kwargs) + return self.get_items((query, ), **kwargs)[0] def get_properties(self, query, **kwargs): + if len(query) == 0: + return [] + if type(query[0]) is int: # Convert list of ints to PIDs - query = map(lambda x: "P%d" % x, query) + query = ["P%d" % x for x in query] elif not (type(query[0]) is str and query[0][0] in "P" and query[0][1:].isdigit()): raise ValueError("query must be a list of PIDs") @@ -405,7 +412,6 @@ class WikibaseStatement: @classmethod def from_dict(cls, d): - print(d.keys()) if not d['type'] == 'statement': raise WikibaseException("Tried to parse a non-statement claim?") quals = [] @@ -427,6 +433,21 @@ class WikibaseStatement: qualifiers=quals, references=refs, ) + return ws + + def to_dict(self): + d = { + "type": "statement", + "references": [[snak.to_dict() for snak in snaklist] for snaklist in self.references], + "qualifiers": [snak.to_dict() for snak in self.qualifiers], + } + if self.mainsnak is not None: + d['mainsnak'] = self.mainsnak.to_dict() + if self.rank is not None: + d['rank'] = self.rank + if self.guid is not None: + d['guid'] = self.guid + return d class WikibaseSnak: @@ -461,4 +482,17 @@ class WikibaseSnak: ws.value = "P%d" % int(d['datavalue']['value']['numeric-id']) else: ws.value = d['datavalue']['value'] + return ws + + def to_dict(self): + d = {'snaktype': self.snaktype} + if self.hash: + d['hash'] = self.hash + if self.value is not None: + d['value'] = self.value + if self.datatype is not None: + d['datatype'] = self.datatype + if self.property is not None: + d['property'] = self.property + return {} |