Skip to content

Commit

Permalink
Add Entity suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMyst committed Feb 15, 2022
1 parent 67bac5f commit 244ca5d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 62 deletions.
10 changes: 5 additions & 5 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from wikibaseintegrator import WikibaseIntegrator, datatypes, wbi_fastrun
from wikibaseintegrator.datatypes import BaseDataType
from wikibaseintegrator.entities import Item
from wikibaseintegrator.entities import ItemEntity
from wikibaseintegrator.wbi_enums import ActionIfExists
from wikibaseintegrator.wbi_fastrun import get_fastrun_container

Expand Down Expand Up @@ -172,8 +172,8 @@ def test_mediainfo():
def test_wikibaseintegrator():
nwbi = WikibaseIntegrator(is_bot=False)
assert nwbi.item.api.is_bot is False
assert Item(api=nwbi, is_bot=True).api.is_bot is True
assert Item(api=nwbi).api.is_bot is False
assert Item().api.is_bot is False
assert ItemEntity(api=nwbi, is_bot=True).api.is_bot is True
assert ItemEntity(api=nwbi).api.is_bot is False
assert ItemEntity().api.is_bot is False
assert nwbi.item.get('Q582').api.is_bot is False
assert Item(api=nwbi, is_bot=True).get('Q582').api.is_bot is True
assert ItemEntity(api=nwbi, is_bot=True).get('Q582').api.is_bot is True
22 changes: 11 additions & 11 deletions test/test_wbi_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from wikibaseintegrator.datatypes import (URL, CommonsMedia, ExternalID, Form, GeoShape, GlobeCoordinate, Lexeme, Math, MonolingualText, MusicalNotation, Property, Quantity,
Sense, String, TabularData, Time)
from wikibaseintegrator.datatypes.extra import EDTF, LocalMedia
from wikibaseintegrator.entities import Item
from wikibaseintegrator.entities import ItemEntity
from wikibaseintegrator.models import LanguageValues
from wikibaseintegrator.wbi_enums import ActionIfExists, WikibaseDatePrecision, WikibaseRank, WikibaseSnakType
from wikibaseintegrator.wbi_helpers import generate_entity_instances, search_entities
Expand All @@ -17,13 +17,13 @@ class TestWbiCore(unittest.TestCase):
common_item = wbi.item.new().get('Q2')

def test_item_engine(self):
Item(api=wbi)
ItemEntity(api=wbi)
wbi.item.new()
Item(api=wbi).add_claims(String(value='test', prop_nr='P1'))
Item(api=wbi).add_claims([String(value='test', prop_nr='P1')])
Item(api=wbi, id='Q2')
ItemEntity(api=wbi).add_claims(String(value='test', prop_nr='P1'))
ItemEntity(api=wbi).add_claims([String(value='test', prop_nr='P1')])
ItemEntity(api=wbi, id='Q2')
with self.assertRaises(TypeError):
Item(api=wbi).add_claims('test')
ItemEntity(api=wbi).add_claims('test')

def test_get(self):
item = wbi.item.new().get(entity_id='Q2')
Expand Down Expand Up @@ -149,16 +149,16 @@ def test_entity_generator(self):
entities = {
'Q408883': {
'etype': 'item',
'ctype': 'Item'
'ctype': 'ItemEntity'
}, 'P715': {
'etype': 'property',
'ctype': 'Property'
'ctype': 'PropertyEntity'
}, 'Q18046452': {
'etype': 'item',
'ctype': 'Item'
'ctype': 'ItemEntity'
}, 'L5': {
'etype': 'lexeme',
'ctype': 'Lexeme'
'ctype': 'LexemeEntity'
}
}

Expand All @@ -174,7 +174,7 @@ def test_entity_generator(self):
for qid, entity in entity_instances:
assert qid == 'Q408883'
assert entity.ETYPE == 'item'
assert type(entity).__name__ == 'Item'
assert type(entity).__name__ == 'ItemEntity'

def test_rank(self):
t1 = String(value='test1', prop_nr='P1', rank='preferred')
Expand Down
8 changes: 4 additions & 4 deletions wikibaseintegrator/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .baseentity import BaseEntity
from .item import Item
from .lexeme import Lexeme
from .mediainfo import MediaInfo
from .property import Property
from .item import ItemEntity
from .lexeme import LexemeEntity
from .mediainfo import MediaInfoEntity
from .property import PropertyEntity
24 changes: 15 additions & 9 deletions wikibaseintegrator/entities/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from wikibaseintegrator.models.sitelinks import Sitelinks


class Item(BaseEntity):
class ItemEntity(BaseEntity):
ETYPE = 'item'

def __init__(self, labels: Labels = None, descriptions: Descriptions = None, aliases: Aliases = None, sitelinks: Sitelinks = None, **kwargs: Any) -> None:
Expand All @@ -26,25 +26,31 @@ def __init__(self, labels: Labels = None, descriptions: Descriptions = None, ali
"""
super().__init__(**kwargs)

# Item and property specific
# Item, Property and MediaInfo specific
self.labels: LanguageValues = labels or Labels()
self.descriptions: LanguageValues = descriptions or Descriptions()
self.aliases = aliases or Aliases()

# Item specific
self.sitelinks = sitelinks or Sitelinks()

def new(self, **kwargs: Any) -> Item:
return Item(api=self.api, **kwargs)
def new(self, **kwargs: Any) -> ItemEntity:
return ItemEntity(api=self.api, **kwargs)

def get(self, entity_id: Union[str, int], **kwargs: Any) -> Item:
def get(self, entity_id: Union[str, int] = None, **kwargs: Any) -> ItemEntity:
"""
Request the Mediawiki API to get data for the entity specified in argument.
:param entity_id: The entity_id of the Item entity you want. Must start with a 'Q'.
:param kwargs:
:return: an Item instance
:return: an ItemEntity instance
"""

if entity_id is None and self.id is not None:
entity_id = self.id
elif entity_id is None:
raise ValueError("You must provide an entity_id")

if isinstance(entity_id, str):
pattern = re.compile(r'^Q?([0-9]+)$')
matches = pattern.match(entity_id)
Expand All @@ -59,7 +65,7 @@ def get(self, entity_id: Union[str, int], **kwargs: Any) -> Item:

entity_id = f'Q{entity_id}'
json_data = super()._get(entity_id=entity_id, **kwargs)
return Item(api=self.api).from_json(json_data=json_data['entities'][entity_id])
return ItemEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id])

def get_json(self) -> Dict[str, Union[str, Dict]]:
"""
Expand All @@ -74,7 +80,7 @@ def get_json(self) -> Dict[str, Union[str, Dict]]:
**super().get_json()
}

def from_json(self, json_data: Dict[str, Any]) -> Item:
def from_json(self, json_data: Dict[str, Any]) -> ItemEntity:
super().from_json(json_data=json_data)

self.labels = Labels().from_json(json_data['labels'])
Expand All @@ -84,6 +90,6 @@ def from_json(self, json_data: Dict[str, Any]) -> Item:

return self

def write(self, **kwargs: Any) -> Item:
def write(self, **kwargs: Any) -> ItemEntity:
json_data = super()._write(data=self.get_json(), **kwargs)
return self.from_json(json_data=json_data)
14 changes: 7 additions & 7 deletions wikibaseintegrator/entities/lexeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from wikibaseintegrator.wbi_config import config


class Lexeme(BaseEntity):
class LexemeEntity(BaseEntity):
ETYPE = 'lexeme'

def __init__(self, lemmas: Lemmas = None, lexical_category: str = None, language: str = None, forms: Forms = None, senses: Senses = None, **kwargs: Any):
Expand All @@ -23,10 +23,10 @@ def __init__(self, lemmas: Lemmas = None, lexical_category: str = None, language
self.forms = forms or Forms()
self.senses = senses or Senses()

def new(self, **kwargs: Any) -> Lexeme:
return Lexeme(api=self.api, **kwargs)
def new(self, **kwargs: Any) -> LexemeEntity:
return LexemeEntity(api=self.api, **kwargs)

def get(self, entity_id: Union[str, int], **kwargs: Any) -> Lexeme:
def get(self, entity_id: Union[str, int], **kwargs: Any) -> LexemeEntity:
if isinstance(entity_id, str):
pattern = re.compile(r'^L?([0-9]+)$')
matches = pattern.match(entity_id)
Expand All @@ -41,7 +41,7 @@ def get(self, entity_id: Union[str, int], **kwargs: Any) -> Lexeme:

entity_id = f'L{entity_id}'
json_data = super()._get(entity_id=entity_id, **kwargs)
return Lexeme(api=self.api).from_json(json_data=json_data['entities'][entity_id])
return LexemeEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id])

def get_json(self) -> Dict[str, Union[str, Dict]]:
json_data: Dict = {
Expand All @@ -57,7 +57,7 @@ def get_json(self) -> Dict[str, Union[str, Dict]]:

return json_data

def from_json(self, json_data: Dict[str, Any]) -> Lexeme:
def from_json(self, json_data: Dict[str, Any]) -> LexemeEntity:
super().from_json(json_data=json_data)

self.lemmas = Lemmas().from_json(json_data['lemmas'])
Expand All @@ -68,6 +68,6 @@ def from_json(self, json_data: Dict[str, Any]) -> Lexeme:

return self

def write(self, **kwargs: Any) -> Lexeme:
def write(self, **kwargs: Any) -> LexemeEntity:
json_data = super()._write(data=self.get_json(), **kwargs)
return self.from_json(json_data=json_data)
20 changes: 10 additions & 10 deletions wikibaseintegrator/entities/mediainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from wikibaseintegrator.wbi_helpers import mediawiki_api_call_helper


class MediaInfo(BaseEntity):
class MediaInfoEntity(BaseEntity):
ETYPE = 'mediainfo'

def __init__(self, labels: Labels = None, descriptions: Descriptions = None, aliases: Aliases = None, **kwargs: Any) -> None:
Expand All @@ -26,15 +26,15 @@ def __init__(self, labels: Labels = None, descriptions: Descriptions = None, ali
"""
super().__init__(**kwargs)

# Item and property specific
# Item, Property and MediaInfo specific
self.labels: LanguageValues = labels or Labels()
self.descriptions: LanguageValues = descriptions or Descriptions()
self.aliases = aliases or Aliases()

def new(self, **kwargs: Any) -> MediaInfo:
return MediaInfo(api=self.api, **kwargs)
def new(self, **kwargs: Any) -> MediaInfoEntity:
return MediaInfoEntity(api=self.api, **kwargs)

def get(self, entity_id: Union[str, int], **kwargs: Any) -> MediaInfo:
def get(self, entity_id: Union[str, int], **kwargs: Any) -> MediaInfoEntity:
if isinstance(entity_id, str):
pattern = re.compile(r'^M?([0-9]+)$')
matches = pattern.match(entity_id)
Expand All @@ -49,9 +49,9 @@ def get(self, entity_id: Union[str, int], **kwargs: Any) -> MediaInfo:

entity_id = f'M{entity_id}'
json_data = super()._get(entity_id=entity_id, **kwargs)
return MediaInfo(api=self.api).from_json(json_data=json_data['entities'][entity_id])
return MediaInfoEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id])

def get_by_title(self, titles: Union[List[str], str], sites: str = 'commonswiki', **kwargs: Any) -> MediaInfo:
def get_by_title(self, titles: Union[List[str], str], sites: str = 'commonswiki', **kwargs: Any) -> MediaInfoEntity:
if isinstance(titles, list):
titles = '|'.join(titles)

Expand All @@ -69,7 +69,7 @@ def get_by_title(self, titles: Union[List[str], str], sites: str = 'commonswiki'
if len(json_data['entities'].keys()) > 1:
raise Exception('More than one element for this title')

return MediaInfo(api=self.api).from_json(json_data=json_data['entities'][list(json_data['entities'].keys())[0]])
return MediaInfoEntity(api=self.api).from_json(json_data=json_data['entities'][list(json_data['entities'].keys())[0]])

def get_json(self) -> Dict[str, Union[str, Dict]]:
return {
Expand All @@ -79,14 +79,14 @@ def get_json(self) -> Dict[str, Union[str, Dict]]:
**super().get_json()
}

def from_json(self, json_data: Dict[str, Any]) -> MediaInfo:
def from_json(self, json_data: Dict[str, Any]) -> MediaInfoEntity:
super().from_json(json_data=json_data)

self.labels = Labels().from_json(json_data['labels'])
self.descriptions = Descriptions().from_json(json_data['descriptions'])

return self

def write(self, **kwargs: Any) -> MediaInfo:
def write(self, **kwargs: Any) -> MediaInfoEntity:
json_data = super()._write(data=self.get_json(), **kwargs)
return self.from_json(json_data=json_data)
16 changes: 8 additions & 8 deletions wikibaseintegrator/entities/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from wikibaseintegrator.models.labels import Labels


class Property(BaseEntity):
class PropertyEntity(BaseEntity):
ETYPE = 'property'

def __init__(self, datatype: str = None, labels: Labels = None, descriptions: Descriptions = None, aliases: Aliases = None, **kwargs: Any):
Expand All @@ -19,15 +19,15 @@ def __init__(self, datatype: str = None, labels: Labels = None, descriptions: De
# Property specific
self.datatype = datatype

# Items and property specific
# Item, Property and MediaInfo specific
self.labels: LanguageValues = labels or Labels()
self.descriptions: LanguageValues = descriptions or Descriptions()
self.aliases = aliases or Aliases()

def new(self, **kwargs: Any) -> Property:
return Property(api=self.api, **kwargs)
def new(self, **kwargs: Any) -> PropertyEntity:
return PropertyEntity(api=self.api, **kwargs)

def get(self, entity_id: Union[str, int], **kwargs: Any) -> Property:
def get(self, entity_id: Union[str, int], **kwargs: Any) -> PropertyEntity:
if isinstance(entity_id, str):
pattern = re.compile(r'^P?([0-9]+)$')
matches = pattern.match(entity_id)
Expand All @@ -42,7 +42,7 @@ def get(self, entity_id: Union[str, int], **kwargs: Any) -> Property:

entity_id = f'P{entity_id}'
json_data = super()._get(entity_id=entity_id, **kwargs)
return Property(api=self.api).from_json(json_data=json_data['entities'][entity_id])
return PropertyEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id])

def get_json(self) -> Dict[str, Union[str, Dict]]:
return {
Expand All @@ -53,7 +53,7 @@ def get_json(self) -> Dict[str, Union[str, Dict]]:
**super().get_json()
}

def from_json(self, json_data: Dict[str, Any]) -> Property:
def from_json(self, json_data: Dict[str, Any]) -> PropertyEntity:
super().from_json(json_data=json_data)

self.datatype = json_data['datatype']
Expand All @@ -63,6 +63,6 @@ def from_json(self, json_data: Dict[str, Any]) -> Property:

return self

def write(self, **kwargs: Any) -> Property:
def write(self, **kwargs: Any) -> PropertyEntity:
json_data = super()._write(data=self.get_json(), **kwargs)
return self.from_json(json_data=json_data)
16 changes: 8 additions & 8 deletions wikibaseintegrator/wikibaseintegrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from typing import TYPE_CHECKING

from wikibaseintegrator.entities.item import Item
from wikibaseintegrator.entities.lexeme import Lexeme
from wikibaseintegrator.entities.mediainfo import MediaInfo
from wikibaseintegrator.entities.property import Property
from wikibaseintegrator.entities.item import ItemEntity
from wikibaseintegrator.entities.lexeme import LexemeEntity
from wikibaseintegrator.entities.mediainfo import MediaInfoEntity
from wikibaseintegrator.entities.property import PropertyEntity

if TYPE_CHECKING:
from wikibaseintegrator.wbi_login import _Login
Expand All @@ -28,7 +28,7 @@ def __init__(self, is_bot: bool = False, login: _Login = None):
self.login = login

# Quick access to entities
self.item = Item(api=self)
self.property = Property(api=self)
self.lexeme = Lexeme(api=self)
self.mediainfo = MediaInfo(api=self)
self.item = ItemEntity(api=self)
self.property = PropertyEntity(api=self)
self.lexeme = LexemeEntity(api=self)
self.mediainfo = MediaInfoEntity(api=self)

0 comments on commit 244ca5d

Please sign in to comment.