forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_wbtypes.py
61 lines (46 loc) · 1.72 KB
/
_wbtypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Wikibase data type classes."""
#
# (C) Pywikibot team, 2013-2020
#
# Distributed under the terms of the MIT license.
#
import abc
import json
from typing import Any
from pywikibot.backports import Dict
# TODO: replace these after T286867
OPT_SITE = Any # Optional['pywikibot.site.DataSite']
class WbRepresentation(abc.ABC):
"""Abstract class for Wikibase representations."""
@abc.abstractmethod
def __init__(self):
"""Constructor."""
raise NotImplementedError
@abc.abstractmethod
def toWikibase(self):
"""Convert representation to JSON for the Wikibase API."""
raise NotImplementedError
@classmethod
@abc.abstractmethod
def fromWikibase(cls, data: Dict[str, Any], site: OPT_SITE = None
) -> 'WbRepresentation':
"""Create a representation object based on JSON from Wikibase API."""
raise NotImplementedError
def __str__(self):
return json.dumps(self.toWikibase(), indent=4, sort_keys=True,
separators=(',', ': '))
def __repr__(self):
assert isinstance(self._items, tuple)
assert all(isinstance(item, str) for item in self._items)
values = ((attr, getattr(self, attr)) for attr in self._items)
attrs = ', '.join('{}={}'.format(attr, value)
for attr, value in values)
return '{}({})'.format(self.__class__.__name__, attrs)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.toWikibase() == other.toWikibase()
return NotImplemented
def __hash__(self):
return hash(frozenset(self.toWikibase().items()))
def __ne__(self, other):
return not self.__eq__(other)