text stringlengths 0 93.6k |
|---|
self.entity_types = frozenset( |
key for key, val in Property.value_types.items() |
if val == 'wikibase-entityid') |
self.attr_mapping = { |
'L': {'key': 'labels'}, |
'D': {'key': 'descriptions'}, |
'A': { |
'key': 'aliases', |
'callback': lambda data, key, value: data.setdefault( |
key, []).append(value) |
}, |
'S': {'key': 'sitelinks'}, |
} |
self.last = None # the last created item (using CREATE) |
self._current = None # the last treated entity |
@property |
def current(self): |
return self._current |
@current.setter |
def current(self, new): |
self._current = new |
@staticmethod |
def valid_text_literal(text, *, allow_empty=False): |
bound = 3 - allow_empty |
if text.startswith('"') and text.endswith('"') and len(text) >= bound: |
return text[1:-1] |
else: |
return None |
def parse_entity(self, value): |
if value == 'LAST': |
return self.last |
else: |
return self.repo.get_entity_for_entity_id(value) |
def _set_target(self, snak, value): |
if value in ('somevalue', 'novalue'): |
snak.setSnakType(value) |
return True |
def invalid_report(): |
pywikibot.warning( |
f'Invalid value "{value}" for {snak.type} datatype') |
if snak.type in self.entity_types: |
target = self.parse_entity(value) |
if target is None: |
pywikibot.warning('"LAST" magic word used without "CREATE"') |
else: |
snak.setTarget(target) |
return True |
elif snak.type == 'quantity': |
match = self.quantity_errR.fullmatch(value) |
if match: |
amount, error, unit = match.groups() |
else: |
match = self.quantity_boundsR.fullmatch(value) |
if match: |
groups = list(match.groups()) |
unit = groups.pop() |
amount, lower, upper = map(Decimal, groups) |
if lower > upper: |
error = amount - lower, upper - amount |
else: |
error = upper - amount, amount - lower |
if match: |
if unit: |
unit = pywikibot.ItemPage(self.repo, 'Q' + unit) |
quantity = WbQuantity(amount, unit, error, site=self.repo) |
snak.setTarget(quantity) |
return True |
else: |
invalid_report() |
elif snak.type == 'time': |
iso, _, prec = value.rpartition('/') |
if iso: |
time = WbTime.fromTimestr( |
iso, precision=int(prec), site=self.repo) |
snak.setTarget(time) |
return True |
else: |
invalid_report() |
elif snak.type in ('string', 'external-id', 'url', 'math'): |
literal = self.valid_text_literal(value) |
if literal: |
snak.setTarget(literal) |
return True |
else: |
invalid_report() |
elif snak.type == 'commonsMedia': |
literal = self.valid_text_literal(value) |
if literal: |
image_repo = self.repo.image_repository() |
snak.setTarget(pywikibot.FilePage(image_repo, literal)) |
return True |
else: |
invalid_report() |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.