|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import logging |
| 4 | +import re |
4 | 5 | from copy import copy |
5 | 6 | from typing import TYPE_CHECKING, Any |
6 | 7 |
|
| 8 | +from entityshape import EntityShape, Result |
| 9 | + |
7 | 10 | from wikibaseintegrator import wbi_fastrun |
8 | 11 | from wikibaseintegrator.datatypes import BaseDataType |
9 | 12 | from wikibaseintegrator.models.claims import Claim, Claims |
| 13 | +from wikibaseintegrator.wbi_config import config |
10 | 14 | from wikibaseintegrator.wbi_enums import ActionIfExists |
11 | 15 | from wikibaseintegrator.wbi_exceptions import MissingEntityException |
12 | 16 | from wikibaseintegrator.wbi_helpers import delete_page, edit_entity, mediawiki_api_call_helper |
@@ -299,6 +303,23 @@ def get_entity_url(self, wikibase_url: str | None = None) -> str: |
299 | 303 |
|
300 | 304 | raise ValueError('wikibase_url or entity ID is null.') |
301 | 305 |
|
| 306 | + def schema_validator(self, entity_schema_id: str, language: str | None = None) -> Result: |
| 307 | + if isinstance(entity_schema_id, str): |
| 308 | + pattern = re.compile(r'^(?:[a-zA-Z]+:)?E?([0-9]+)$') |
| 309 | + matches = pattern.match(entity_schema_id) |
| 310 | + |
| 311 | + if not matches: |
| 312 | + raise ValueError(f"Invalid EntitySchema ID ({entity_schema_id}), format must be 'E[0-9]+'") |
| 313 | + |
| 314 | + entity_schema_id = f'E{matches.group(1)}' |
| 315 | + elif isinstance(entity_schema_id, int): |
| 316 | + entity_schema_id = f'E{entity_schema_id}' |
| 317 | + else: |
| 318 | + raise ValueError(f"Invalid EntitySchema ID ({entity_schema_id}), format must be 'E[0-9]+'") |
| 319 | + |
| 320 | + language = str(language or config['DEFAULT_LANGUAGE']) |
| 321 | + return EntityShape(qid=self.id, eid=entity_schema_id, lang=language).validate_and_get_result() |
| 322 | + |
302 | 323 | def __repr__(self): |
303 | 324 | """A mixin implementing a simple __repr__.""" |
304 | 325 | return "<{klass} @{id:x} {attrs}>".format( # pylint: disable=consider-using-f-string |
|
0 commit comments