-
Notifications
You must be signed in to change notification settings - Fork 1
/
importer.py
372 lines (326 loc) · 14.5 KB
/
importer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
from CmFr import CmFr
from CzCs import CzCs
from AtDe import AtDe
from DkBygningDa import DkBygningDa
from DkFortidsDa import DkFortidsDa
from EeEt import EeEt
from HuHu import HuHu
from IeEn import IeEn
from NoNo import NoNo
from PlPl import PlPl
from PtPt import PtPt
from RoRo import RoRo
from SeArbetslSv import SeArbetslSv
from SeBbrSv import SeBbrSv
from SeFornminSv import SeFornminSv
from SeShipSv import SeShipSv
from XkSq import XkSq
from ZaEn import ZaEn
from Uploader import *
from Logger import *
import LookupTable as Lt
from os import path
import argparse
import pymysql
import wikidataStuff.wdqsLookup as lookup
import importer_utils as utils
DEFAULT_SHORT = 10
MAPPING_DIR = "mappings"
MONUMENTS_ALL = "monuments_all"
class Mapping(object):
def load_mapping_file(self, countryname, languagename):
filename = path.join(
MAPPING_DIR, "{}_({}).json".format(countryname, languagename))
return utils.load_json(filename)
def get_unique_prop(self):
if "unique" in self.file_content and self.file_content["unique"]["property"] != "":
return self.file_content["unique"]["property"]
def __init__(self, countryname, languagename):
self.file_content = self.load_mapping_file(countryname, languagename)
def make_query(specific_table, offset):
"""
Generate a query to retrieve data from database.
:param specific_table: Name of table to retrieve data from.
:param offset: Optional offset to start from.
"""
query = 'select DISTINCT * from `{}`'.format(specific_table)
if isinstance(offset, int):
unlimited = "18446744073709551615"
# workaround for MySQL requiring a limit when using offset
query += " LIMIT {}, {};".format(str(offset), unlimited)
return query
def make_count_query(specific_table):
return ("SELECT COUNT(*) FROM `{}`").format(specific_table)
def create_connection(arguments):
"""Create a connection to the SQL database using arguments."""
return pymysql.connect(
host=arguments["host"],
user=arguments["user"],
password=arguments["password"],
db=arguments["db"],
charset="utf8")
"""
There must be a better way to do this.....
"""
SPECIFIC_TABLES = {"monuments_se-ship_(sv)": {"class": SeShipSv,
"data_files":
{"functions":
"se-ship_(sv)_functions.json"}},
"monuments_cz_(cs)": {"class": CzCs, "data_files": {}},
"monuments_hu_(hu)": {"class": HuHu, "data_files": {}},
"monuments_pt_(pt)": {"class": PtPt, "data_files": {}},
"monuments_ro_(ro)": {"class": RoRo, "data_files": {}},
"monuments_xk_(sq)": {"class": XkSq, "data_files": {}},
"monuments_ie_(en)": {"class": IeEn, "data_files":{"counties": "ireland_counties.json"}},
"monuments_za_(en)": {"class": ZaEn, "data_files": {}},
"monuments_cm_(fr)": {"class": CmFr, "data_files": {}},
"monuments_at_(de)": {"class": AtDe, "data_files": {"municipalities":
"austria_municipalities.json",
"types": "at_(de)_types.json"}},
"monuments_dk-bygninger_(da)": {"class": DkBygningDa,
"data_files": {},
"subclass_downloads": {"settlement": "Q486972"}},
"monuments_pl_(pl)": {"class": PlPl,
"data_files": {"settlements": "poland_settlements.json"}},
"monuments_dk-fortidsminder_(da)": {"class": DkFortidsDa,
"data_files": {
"types": "dk-fortidsminder_(da)_types.json",
"municipalities": "denmark_municipalities.json"
}},
"monuments_no_(no)": {"class": NoNo,
"data_files": {}},
"monuments_se-bbr_(sv)": {"class": SeBbrSv,
"data_files": {"functions": "se-bbr_(sv)_functions.json",
"settlements": "sweden_settlements.json"}},
"monuments_ee_(et)": {"class": EeEt,
"data_files": {"counties": "estonia_counties.json"}},
"monuments_se-fornmin_(sv)":
{"class": SeFornminSv,
"data_files": {
"municipalities": "sweden_municipalities.json",
"socken": "sweden_socken.json"
},
"lookup_downloads": {
"types": "se-fornmin_(sv)/types"},
},
"monuments_se-arbetsl_(sv)":
{"class":
SeArbetslSv,
"data_files":
{"municipalities": "sweden_municipalities.json",
"types": "se-arbetsl_(sv)_types.json",
"settlements": "sweden_settlements.json"}}
}
def get_row_count(tablename, connection):
count_query = make_count_query(tablename)
return select_query(count_query, connection)[0]['COUNT(*)']
def print_row_count(tablename, connection):
rowcount = get_row_count(tablename, connection)
print(("TABLE {} HAS {} ROWS.").format(tablename, rowcount))
def select_query(query, connection):
cursor = connection.cursor(pymysql.cursors.DictCursor)
cursor.execute(query)
result = cursor.fetchall()
return result
def get_wd_items_using_prop(prop):
items = {}
print("WILL NOW DOWNLOAD WD ITEMS THAT USE " + prop)
query = "SELECT DISTINCT ?item ?value WHERE {?item p:" + \
prop + "?statement. OPTIONAL { ?item wdt:" + prop + " ?value. }}"
data = lookup.make_simple_wdqs_query(query, verbose=False)
for x in data:
key = lookup.sanitize_wdqs_result(x['item'])
value = x['value']
items[value] = key
print("FOUND {} WD ITEMS WITH PROP {}".format(len(items), prop))
return items
def get_subclasses(q_item):
"""
Get Q-ids of all the items that are subclasses of the specified one.
The query fetches both items where P279 = q_item
and indirect ones (subclass of subclass...).
"""
results = []
query = "SELECT DISTINCT ?item WHERE {?item wdt:P279* wd:" + q_item + ".}"
data = lookup.make_simple_wdqs_query(query, verbose=False)
for x in data:
results.append(lookup.sanitize_wdqs_result(x['item']))
return results
def save_reports(problem_reports, tablename, timestamp):
"""Save the problem reports of the batch to a file."""
filename = "report_{}_{}.json".format(tablename, timestamp)
utils.json_to_file(filename, problem_reports)
def load_data_files(tablename):
"""Load offline data files as specified in SPECIFIC_TABLES."""
file_dict = tablename["data_files"]
for key in file_dict.keys():
json_path = path.join(MAPPING_DIR, file_dict[key])
file_dict[key] = utils.load_json(json_path)
print("Loaded offline data file: {}".format(json_path))
return file_dict
def load_data(tablename):
"""
Get data files necessary for mappings.
Loads both offline files and online ones
specified in SPECIFIC TABLES.
"""
data_files = load_data_files(tablename)
if tablename.get("subclass_downloads"):
for sub_title, sub_item in tablename.get("subclass_downloads").items():
data_files[sub_title] = get_subclasses(sub_item)
print("Loaded online data: {}".format(sub_title))
if tablename.get("lookup_downloads"):
for l_title, l_path in tablename.get("lookup_downloads").items():
lookup_table = Lt.LookupTable(l_path)
lookup_json = lookup_table.convert_page_to_json_table()
data_files[l_title] = lookup_json
print("Loaded online data: {}".format(l_path))
return data_files
def get_items(connection,
country,
language,
upload,
short=False,
offset=None,
table=False):
"""
Retrieve data from database and process it.
:param connection: Connection used to access the database.
:param country: Country of the dataset.
:param language: Language code of the dataset.
:param upload: Whether to upload the processed items.
:param short: Optional number of randomly selected rows to process.
:param offset: Optional offset to retrieve rows.
:param table: Whether to save the results as a wikitable.
"""
started_at = utils.get_current_timestamp()
if upload:
logger = Logger()
country_language = {"country": country, "language": language}
specific_table_name = utils.get_specific_table_name(country, language)
if not utils.table_exists(connection, specific_table_name):
print("Table does not exist.")
return
mapping = Mapping(country, language)
unique_prop = mapping.get_unique_prop()
if unique_prop is not None:
existing = get_wd_items_using_prop(unique_prop)
else:
existing = None
query = make_query(specific_table_name, offset)
if specific_table_name in SPECIFIC_TABLES:
specific_table = SPECIFIC_TABLES[specific_table_name]
class_to_use = specific_table["class"]
data_files = load_data(specific_table)
else:
print("No class defined for " + specific_table_name)
return
print_row_count(specific_table_name, connection)
database_rows = select_query(query, connection)
if short:
database_rows = utils.get_random_list_sample(database_rows, short)
print("USING RANDOM SAMPLE OF " + str(short))
filename = specific_table_name + "_" + utils.get_current_timestamp()
problem_reports = []
wikidata_site = utils.create_site_instance("wikidata", "wikidata")
for row in database_rows:
monument = class_to_use(row, mapping, data_files,
existing, wikidata_site)
problem_report = monument.get_report()
if table:
raw_data = "<pre>" + str(row) + "</pre>\n"
monument_table = monument.print_wd_to_table()
utils.append_line_to_file(raw_data, filename)
utils.append_line_to_file(monument_table, filename)
if upload:
live = True if upload == "live" else False
uploader = Uploader(
monument,
repo=wikidata_site,
log=logger,
tablename=country,
live=live)
if "Q" in problem_report and problem_report["Q"] == "":
"""
If the Monument didn't have an associated Qid,
this means the Uploader has now created a new Item
for it -- insert that id into the problem report.
"""
problem_report["Q"] = uploader.wd_item_q
uploader.upload()
print("--------------------------------------------------")
if problem_report: # dictionary is not empty
problem_reports.append(problem_report)
save_reports(problem_reports, specific_table_name, started_at)
if table:
print("SAVED TEST RESULTS TO " + filename)
def main(arguments):
"""Process the arguments and fetch data according to them"""
arguments = vars(arguments)
if on_labs():
arguments["host"] = "tools-db"
arguments["db"] = "s51138__heritage_p"
credentials = get_db_credentials()
arguments["user"] = credentials["user"]
arguments["password"] = credentials["password"]
connection = create_connection(arguments)
country = arguments["country"]
language = arguments["language"]
short = arguments["short"]
offset = arguments["offset"]
upload = arguments["upload"]
table = arguments["table"]
get_items(connection, country, language, upload, short, offset, table)
def get_db_credentials():
"""Get credentials to access the SQL db on Tolllabs."""
credentials = {}
credentials_path = path.expanduser("~") + "/replica.my.cnf"
with open(credentials_path) as f:
lines = f.readlines()
for line in lines:
if line.startswith("user"):
credentials["user"] = line.partition("=")[-1].strip()
elif line.startswith("password"):
credentials["password"] = line.partition("=")[-1].strip()
return credentials
def on_labs():
"""Check if running in the Toollabs environment."""
return path.isfile(path.expanduser("~") + "/replica.my.cnf")
if __name__ == "__main__":
"""
Get command line arguments to get data from the database.
Options:
--host Name of the database host.
--db Name of the database to connect to.
--user Username to connect to the database.
--password Password to connect to the database.
--language Language code of the table to fetch, eg. "sv".
--country Country of the table to fetch, eg. "se-fornmin".
--upload Whether to upload the processed items. Two options:
--upload sandbox Upload to the Wikidata sandbox item.
--upload live Live upload to real Wikidata items.
--short <int> Only fetch a random sample of <int> items.
--table Save results of the processing to file as a wikitable.
"""
arguments = {}
parser = argparse.ArgumentParser()
if not on_labs():
parser.add_argument("--host", default="localhost")
parser.add_argument("--db", default="wlm")
parser.add_argument("--user", default="root")
parser.add_argument("--password", default="")
parser.add_argument("--language", default="sv")
parser.add_argument("--country", default="se-ship")
parser.add_argument("--upload", action='store')
parser.add_argument("--short",
const=DEFAULT_SHORT,
nargs='?',
type=int,
action='store',)
parser.add_argument("--offset",
nargs='?',
type=int,
action='store',)
parser.add_argument("--table", action='store_true')
args = parser.parse_args()
main(args)