Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param prop:
:param value:
:return:
"""
equiv_class_pid = self.URI_PID['http://www.w3.org/2002/07/owl#equivalentClass']
query = """
PREFIX wwdt:
SELECT ?wditem ?localitem ?id WHERE {{
SERVICE {{
?wditem wwdt:{prop} "{value}"
}}
?localitem wdt:{equiv_class_pid} ?wditem
}}"""
query = query.format(prop=prop, value=value, equiv_class_pid=equiv_class_pid)
results = wdi_core.WDItemEngine.execute_sparql_query(query, endpoint=self.sparql_endpoint_url)
result = results['results']['bindings']
if len(result) == 0:
return None
elif len(result) > 1:
raise ValueError("More than one wikidata ID found for {} {}: {}".format(prop, value, result))
else:
return result[0]['localitem']['value'].split("/")[-1]
msg = "SKIP"
try:
if write:
wd_item.write(login=login, edit_summary=edit_summary)
wdi_core.WDItemEngine.log("INFO", format_msg(record_id, record_prop, wd_item.wd_item_id, msg) + ";" + str(
wd_item.lastrevid))
except wdi_core.WDApiError as e:
print(e)
wdi_core.WDItemEngine.log("ERROR",
format_msg(record_id, record_prop, wd_item.wd_item_id, json.dumps(e.wd_error_msg),
type(e)))
return e
except Exception as e:
print(e)
wdi_core.WDItemEngine.log("ERROR", format_msg(record_id, record_prop, wd_item.wd_item_id, str(e), type(e)))
return e
return True
if not all([k in prop_value_map for k, v in rblock.items()]):
found_good = False
if not all([v in prop_value_map[k] for k, v in rblock.items() if v]):
found_good = False
if found_good:
return True
return False
# stated in, title, retrieved
ref_properties = ['P248', 'P1476', 'P813'] # 'P407' language of work,
for v in values:
if prop_nrs[values.index(v)] == 'P248' and v in WDItemEngine.pmids:
return True
elif v == 'P698':
return True
for p in ref_properties:
if p not in prop_nrs:
return False
for ref in ref_block:
pn = ref.get_prop_nr()
value = ref.get_value()
if pn == 'P248' and value not in WDItemEngine.databases and 'P854' not in prop_nrs:
return False
elif pn == 'P248' and value in WDItemEngine.databases:
db_props = WDItemEngine.databases[value]
def get_prop_datatype(prop_nr):
item = wdi_core.WDItemEngine(wd_item_id=prop_nr)
return item.entity_metadata['datatype']
def create(self, login=None):
"""
Attempt to create new item. Returns `None` if creation fails.
"""
if login is None:
raise ValueError("login required to create item")
try:
self.parse_metadata()
self.make_statements()
item = wdi_core.WDItemEngine(item_name=self.meta['title'], data=self.statements,
domain="scientific_article")
except Exception as e:
msg = format_msg(self.ext_id, self.id_type, None, str(e), type(e))
print(msg)
wdi_core.WDItemEngine.log("ERROR", msg)
return None
item.set_label(self.meta['title'])
description = ', '.join(self.descriptions[x] for x in self.meta['pubtype_qid'])
item.set_description(description, lang='en')
write_success = try_write(item, self.ext_id, self.PROPS[self.id_types[self.id_type]], login)
if write_success:
self._cache[(self.ext_id, self.id_type)] = item.wd_item_id
return item.wd_item_id
else:
return None
if not isinstance(claims['P50'], list):
claims['P50'] = [claims['P50']]
for claim in claims['P50']:
assert len(claim['qualifiers']['P1545']) == 1
ordinals.append(int(claim['qualifiers']['P1545'][0]['datavalue']['value']))
try:
current_label = dc['entities'][qid]['labels']['en']['value']
except KeyError:
current_label = ''
if difflib.SequenceMatcher(None, current_label, self.meta['title']).ratio() > 0.90:
self.meta['title'] = current_label
# make statements without the existing authors
self.make_statements(ordinals)
item = wdi_core.WDItemEngine(wd_item_id=qid, data=self.statements, domain="scientific_article",
global_ref_mode='CUSTOM', ref_handler=update_retrieved_if_new)
item.set_label(self.meta['title'])
description = ', '.join(self.descriptions[x] for x in self.meta['pubtype_qid'])
if item.get_description() == '':
item.set_description(description, lang='en')
write_success = try_write(item, self.ext_id, self.PROPS[self.id_types[self.id_type]], login)
if write_success:
self._cache[(self.ext_id, self.id_type)] = item.wd_item_id
return item.wd_item_id
else:
return None
self.original_statments = []
self.entity_metadata = {}
self.fast_run = fast_run
self.fast_run_base_filter = fast_run_base_filter
self.fast_run_container = None
self.require_write = True
self.global_ref_mode = global_ref_mode
self.good_refs = good_refs
self.keep_good_ref_statements = keep_good_ref_statements
self.search_only = search_only
if len(WDItemEngine.databases) == 0 or len(WDItemEngine.pmids) == 0:
WDItemEngine._init_ref_system()
if data is None:
self.data = []
else:
self.data = data
if append_value is None:
self.append_value = []
else:
self.append_value = append_value
if self.fast_run:
self.init_fastrun()
if not __debug__:
def get_last_modified_header(entity="http://www.wikidata.org", endpoint='https://query.wikidata.org/sparql'):
# this will work on wikidata or any particular entity
query = "select ?d where {{<{}> schema:dateModified ?d}}".format(entity)
results = WDItemEngine.execute_sparql_query(query, endpoint=endpoint)['results']['bindings']
results = [{k: v['value'] for k, v in x.items()} for x in results]
t = results[0]['d']
try:
# wikidata format
dt = datetime.datetime.strptime(t, '%Y-%m-%dT%H:%M:%SZ')
except ValueError:
# wikibase format
dt = datetime.datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%fZ')
return dt