Skip to content

Commit

Permalink
Use WikibaseTimePrecision instead of WikibaseDatePrecision (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMyst authored Nov 11, 2024
1 parent a1d21ea commit 08ff6b8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ or modification of items.
from wikibaseintegrator import WikibaseIntegrator, wbi_login
from wikibaseintegrator.datatypes import ExternalID, Item, String, Time
from wikibaseintegrator.wbi_config import config as wbi_config
from wikibaseintegrator.wbi_enums import WikibaseDatePrecision
from wikibaseintegrator.wbi_enums import WikibaseTimePrecision

wbi_config['USER_AGENT'] = 'MyWikibaseBot/1.0 (https://www.wikidata.org/wiki/User:MyUsername)'

Expand All @@ -697,7 +697,7 @@ for entrez_id, ensembl in raw_data.items():
references = [
[
Item(value='Q20641742', prop_nr='P248'),
Time(time='+2020-02-08T00:00:00Z', prop_nr='P813', precision=WikibaseDatePrecision.DAY),
Time(time='+2020-02-08T00:00:00Z', prop_nr='P813', precision=WikibaseTimePrecision.DAY),
ExternalID(value='1017', prop_nr='P351')
]
]
Expand Down Expand Up @@ -744,7 +744,7 @@ The full example:
```python
from wikibaseintegrator import WikibaseIntegrator, wbi_login
from wikibaseintegrator.datatypes import ExternalID, Item, String, Time
from wikibaseintegrator.wbi_enums import WikibaseDatePrecision
from wikibaseintegrator.wbi_enums import WikibaseTimePrecision

# login object
login = wbi_login.OAuth2(consumer_token='<consumer_token>', consumer_secret='<consumer_secret>')
Expand All @@ -766,7 +766,7 @@ for entrez_id, ensembl in raw_data.items():
Item(value='Q20641742', prop_nr='P248')
],
[
Time(time='+2020-02-08T00:00:00Z', prop_nr='P813', precision=WikibaseDatePrecision.DAY),
Time(time='+2020-02-08T00:00:00Z', prop_nr='P813', precision=WikibaseTimePrecision.DAY),
ExternalID(value='1017', prop_nr='P351')
]
]
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#
import os
import sys
from datetime import datetime
from collections.abc import MutableMapping
from datetime import datetime

sys.path.insert(0, os.path.abspath('../..'))

Expand Down
14 changes: 7 additions & 7 deletions test/test_wbi_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from wikibaseintegrator.entities import ItemEntity
from wikibaseintegrator.models import Descriptions
from wikibaseintegrator.wbi_config import config as wbi_config
from wikibaseintegrator.wbi_enums import ActionIfExists, WikibaseDatePrecision, WikibaseRank, WikibaseSnakType
from wikibaseintegrator.wbi_enums import ActionIfExists, WikibaseRank, WikibaseSnakType, WikibaseTimePrecision
from wikibaseintegrator.wbi_helpers import generate_entity_instances, search_entities

wbi_config['USER_AGENT'] = 'WikibaseIntegrator-pytest/1.0 (test_wbi_core.py)'
Expand Down Expand Up @@ -225,13 +225,13 @@ def test_new_item_creation(self):
Item(value=123, prop_nr='P4'),
Item(value='Item:Q123', prop_nr='P4'),
Item(value='http://www.wikidata.org/entity/Q123', prop_nr='P4'),
Time(time='-0458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
Time(time='+458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
Time(time='-0458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseTimePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
Time(time='+458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseTimePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
Time(time='+2021-01-01T00:00:00Z', before=1, after=2, precision=3, timezone=4, prop_nr='P5'),
Time(time='now', before=1, after=2, precision=WikibaseDatePrecision.MONTH, timezone=4, prop_nr='P5'),
Time(time='+2021-01-00T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MONTH, timezone=4, prop_nr='P5'),
Time(time='+2021-00-00T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.YEAR, timezone=4, prop_nr='P5'),
Time(time='+2021-00-00T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.DECADE, timezone=4, prop_nr='P5'),
Time(time='now', before=1, after=2, precision=WikibaseTimePrecision.MONTH, timezone=4, prop_nr='P5'),
Time(time='+2021-01-00T00:00:00Z', before=1, after=2, precision=WikibaseTimePrecision.MONTH, timezone=4, prop_nr='P5'),
Time(time='+2021-00-00T00:00:00Z', before=1, after=2, precision=WikibaseTimePrecision.YEAR, timezone=4, prop_nr='P5'),
Time(time='+2021-00-00T00:00:00Z', before=1, after=2, precision=WikibaseTimePrecision.DECADE, timezone=4, prop_nr='P5'),
URL(value="http://www.wikidata.org", prop_nr='P6'),
URL(value="https://www.wikidata.org", prop_nr='P6'),
URL(value="ftp://example.com", prop_nr='P6'),
Expand Down
20 changes: 10 additions & 10 deletions wikibaseintegrator/datatypes/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from wikibaseintegrator.datatypes.basedatatype import BaseDataType
from wikibaseintegrator.wbi_config import config
from wikibaseintegrator.wbi_enums import WikibaseDatePrecision
from wikibaseintegrator.wbi_enums import WikibaseTimePrecision


@total_ordering
Expand All @@ -21,7 +21,7 @@ class Time(BaseDataType):
}}
'''

def __init__(self, time: Optional[str] = None, before: int = 0, after: int = 0, precision: Union[int, WikibaseDatePrecision, None] = None, timezone: int = 0,
def __init__(self, time: Optional[str] = None, before: int = 0, after: int = 0, precision: Union[int, WikibaseTimePrecision, None] = None, timezone: int = 0,
calendarmodel: Optional[str] = None, wikibase_url: Optional[str] = None, **kwargs: Any):
"""
Constructor, calls the superclass BaseDataType
Expand All @@ -41,7 +41,7 @@ def __init__(self, time: Optional[str] = None, before: int = 0, after: int = 0,
super().__init__(**kwargs)
self.set_value(time=time, before=before, after=after, precision=precision, timezone=timezone, calendarmodel=calendarmodel, wikibase_url=wikibase_url)

def set_value(self, time: Optional[str] = None, before: int = 0, after: int = 0, precision: Union[int, WikibaseDatePrecision, None] = None, timezone: int = 0,
def set_value(self, time: Optional[str] = None, before: int = 0, after: int = 0, precision: Union[int, WikibaseTimePrecision, None] = None, timezone: int = 0,
calendarmodel: Optional[str] = None, wikibase_url: Optional[str] = None):
calendarmodel = calendarmodel or str(config['CALENDAR_MODEL_QID'])
wikibase_url = wikibase_url or str(config['WIKIBASE_URL'])
Expand All @@ -66,23 +66,23 @@ def set_value(self, time: Optional[str] = None, before: int = 0, after: int = 0,
pattern_year = re.compile(r'^[+-][0-9]{1,16}-(?:1[0-2]|0[0-9])-(?:3[01]|0[0-9]|[12][0-9])T00:00:00Z$')
if not precision:
if pattern_day.match(time):
precision = WikibaseDatePrecision.DAY
precision = WikibaseTimePrecision.DAY
elif pattern_month.match(time):
precision = WikibaseDatePrecision.MONTH
precision = WikibaseTimePrecision.MONTH
elif pattern_year.match(time):
precision = WikibaseDatePrecision.YEAR
precision = WikibaseTimePrecision.YEAR
else:
raise ValueError(f"Time value ({time}) must be a string in the following format: '+%Y-%m-%dT00:00:00Z'.")
else:
if isinstance(precision, int):
precision = WikibaseDatePrecision(precision)
precision = WikibaseTimePrecision(precision)

if precision not in WikibaseDatePrecision:
if precision not in WikibaseTimePrecision:
raise ValueError("Invalid value for time precision, see https://www.mediawiki.org/wiki/Wikibase/DataModel/JSON#time")

if precision == WikibaseDatePrecision.DAY:
if precision == WikibaseTimePrecision.DAY:
pattern = pattern_day
elif precision == WikibaseDatePrecision.MONTH:
elif precision == WikibaseTimePrecision.MONTH:
pattern = pattern_month
else:
pattern = pattern_year
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/wbi_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class WikibaseSnakType(Enum):
UNKNOWN_VALUE = "somevalue"


class WikibaseDatePrecision(Enum):
class WikibaseTimePrecision(Enum):
# SECOND = 14 # UNSUPPORTED TO DATE (REL1_37)
# MINUTE = 13 # UNSUPPORTED TO DATE (REL1_37)
# HOUR = 12 # UNSUPPORTED TO DATE (REL1_37)
Expand Down

0 comments on commit 08ff6b8

Please sign in to comment.