-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_mapping_tables.py
110 lines (85 loc) · 3.57 KB
/
create_mapping_tables.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
import argparse
import wlmhelpers
import pywikibot
def getTableHeaders(connection, tablename):
headerList = []
cursor = connection.cursor()
query = "DESCRIBE `" + tablename + "`"
cursor.execute(query)
result = cursor.fetchall()
for header in result:
headerList.append(header[0])
return headerList
def tableHeadersToWikitable(headersWithContentTuple):
wikitableTop = "{| class='wikitable'\n! heritage field\n! example value\n! Wikidata property\n! Conversion\n! comment\n"
wikitableBottom = "|}\n"
wikitext = ""
for item in headersWithContentTuple:
wikitext += "|- \n| {}\n| {}\n| \n| \n|\n".format(item[0], item[1])
return wikitableTop + wikitext + wikitableBottom
def addToFile(filename, content):
with open(filename, "a", encoding="utf-8") as out:
out.write(content + "\n")
def fileToString(filename):
return open(filename, 'r', encoding="utf-8").read()
def addTablenameToList(tablename, filename):
tablenameShort = wlmhelpers.shortenTablename(tablename)
template = "* [[Wikidata:WikiProject_WLM/Mapping_tables/{}|{}]]".format(
tablenameShort, tablenameShort)
addToFile(filename, template)
print("Added table to list: {}".format(tablenameShort))
def getExampleValueFromColumn(connection, column, table):
cursor = connection.cursor()
query = "SELECT `" + column + "` FROM `" + \
table + "` WHERE trim(`" + column + "`) > ''"
cursor.execute(query)
result = cursor.fetchall()
try:
content = result[1][0]
if isinstance(content, str):
if "[" in content or "{{" in content:
content = "<nowiki>" + content + "</nowiki>"
except IndexError:
content = ""
return content
def insertWikitableIntoTemplate(tabletitle, wikitable, templateFile):
template = fileToString(templateFile)
return template % (tabletitle, wikitable)
def createTables(connection):
open(TABLE_NAMES, 'w', encoding="utf-8").close()
countryTables = wlmhelpers.getNonEmptyCountryTables(connection)
for tablename in countryTables:
tablename = tablename[0]
addTablenameToList(tablename, TABLE_NAMES)
headerList = getTableHeaders(connection, tablename)
headersWithContent = []
for header in headerList:
content = getExampleValueFromColumn(
connection, header, tablename)
headersWithContent.append((header, content))
wikiTable = tableHeadersToWikitable(headersWithContent)
wikiPage = insertWikitableIntoTemplate(
wlmhelpers.shortenTablename(tablename), wikiTable, TEMPLATE)
wlmhelpers.saveToFile("{}.txt".format(tablename), wikiPage)
TABLE_NAMES = "_tablenames.txt"
TEMPLATE = "_template.txt"
def main(arguments):
connection = wlmhelpers.create_connection(arguments)
createTables(connection)
def handle_args(*args):
"""
Parse and handle command line arguments to get data from the database.
Also supports any pywikibot arguments, these are prefixed by a single "-"
and the full list can be gotten through "-help".
"""
parser = argparse.ArgumentParser()
if not wlmhelpers.on_forge():
parser.add_argument("--host", default="localhost")
parser.add_argument("--db", default="wlm")
parser.add_argument("--user", default="root")
parser.add_argument("--password", default="")
# first parse args with pywikibot, send remaining args to local handler
return parser.parse_args(pywikibot.handle_args(args))
if __name__ == "__main__":
parsed_args = handle_args()
main(parsed_args)