|
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
|
@@ -306,6 +310,23 @@ def download_entity_ttl(self, **kwargs) -> str:
|
306 | 310 |
|
307 | 311 | raise ValueError('entity ID is null')
|
308 | 312 |
|
| 313 | + def schema_validator(self, entity_schema_id: str, language: str | None = None) -> Result: |
| 314 | + if isinstance(entity_schema_id, str): |
| 315 | + pattern = re.compile(r'^(?:[a-zA-Z]+:)?E?([0-9]+)$') |
| 316 | + matches = pattern.match(entity_schema_id) |
| 317 | + |
| 318 | + if not matches: |
| 319 | + raise ValueError(f"Invalid EntitySchema ID ({entity_schema_id}), format must be 'E[0-9]+'") |
| 320 | + |
| 321 | + entity_schema_id = f'E{matches.group(1)}' |
| 322 | + elif isinstance(entity_schema_id, int): |
| 323 | + entity_schema_id = f'E{entity_schema_id}' |
| 324 | + else: |
| 325 | + raise ValueError(f"Invalid EntitySchema ID ({entity_schema_id}), format must be 'E[0-9]+'") |
| 326 | + |
| 327 | + language = str(language or config['DEFAULT_LANGUAGE']) |
| 328 | + return EntityShape(qid=self.id, eid=entity_schema_id, lang=language).validate_and_get_result() |
| 329 | + |
309 | 330 | def __repr__(self):
|
310 | 331 | """A mixin implementing a simple __repr__."""
|
311 | 332 | return "<{klass} @{id:x} {attrs}>".format( # pylint: disable=consider-using-f-string
|
|
0 commit comments