Repository navigation
Expand file tree
/
Copy pathconfigure_settings.py
More file actions
135 lines (117 loc) · 5.51 KB
/
Copy pathconfigure_settings.py
File metadata and controls
135 lines (117 loc) · 5.51 KB
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
# coding=<utf-8>
from settings import settings
import argparse
import re
import os
def usable_path(path):
"""Quickly checkinf if the path can be used."""
if path == '' or type(path) is not str:
return False
if not path.endswith('.sqlite3'):
path += '.sqlite3'
if len(path) < 9:
return False
elif os.path.exists(path):
print(f'File: {path} already exists')
return False
else:
try:
with open(path, 'w') as f:
f.write('')
pass
os.remove(path)
return path
except FileNotFoundError:
print(f'Could not find path: {path}')
return False
if __name__ == '__main__':
# later: (10) add a flag for wikipedia, 0: do not use, 1+ the number of Qs to resolve before commiting to db.
parser = argparse.ArgumentParser(
description='This script will help you set up the toponym database.'
'All these settings will be stored in .yaml format, '
'which is in a plain text format and can be edited with'
'just about any text editor. \n'
'Since your server token will be stored in this file'
', anyone with access to the file can control your'
'application. So share it at your own risk.')
# TODO : Add some examples here. -- also add these to the README.md
parser.add_argument('-d', '--db_path', metavar='database_path', type=str,
const='s', nargs='?',
help='Path to where the database should be.')
parser.add_argument('-t', '--token', metavar='server_token', type=str,
const='', nargs='?',
help='Token for connecting to Anvil.Works GUI')
parser.add_argument('-l', '--languages', metavar='languages', type=str,
nargs='*', default=set(),
help='The ISO 639 code(s) for relevant language(s).\n'
'If left blank, all languages will be imported.')
parser.add_argument('-c', '--countries', metavar='countries', type=str,
nargs='+',
help='The ISO 3166 code(s) for countrie(s) of interest'
)
parser.add_argument('-a', '--adjacents', metavar='adjacents', type=str,
nargs='*', default=set(),
help='Add adjacent countries with locations that you'
'suspect may be in your input dataset.'
'These names will be mapped to the same single point'
'to help filter out them from.')
parser.add_argument('-n', '--num-rows', metavar='num_rows', type=int,
nargs='?', default=1e100,
help='The number of rows of toponyms to batch add to '
'database, may be necessary for machines with less '
'memory.')
parser.add_argument('-w', '--wiki-rows', metavar='wiki_rows', type=int,
nargs='?', default=500,
help='Set the size of the query to wikidata for more '
'topnym variants. Defaults to 500, 0 turns this off.')
args = parser.parse_args()
settings.num_rows = args.num_rows
db_path = args.db_path
token = args.token
# using sets to make sure that no code is entered twice.
countries = set(_.upper() for _ in args.countries)
adjacents = set(_.upper() for _ in args.adjacents) - countries
languages = set(args.languages)
if countries is None:
raise ValueError('No country has been selected.')
else:
settings.countries = countries
lang_pattern = re.compile(r'[a-z]+')
if languages is None:
print('No languages have been entered.')
languages = input('Please enter ISO-code for relevant languages, or '
'just press enter to proceed with all languages: ')
if languages == '':
languages = None
else:
languages = lang_pattern.findall(languages)
else:
settings.languages = languages
if adjacents is None:
print('No adjacents have been entered.')
adjacents = input('Please enter ISO-code for relevant adjacents, or '
'just press enter to proceed with all adjacents: ')
if adjacents == '':
adjacents = None
else:
adjacents = lang_pattern.findall(adjacents)
else:
settings.adjacents = adjacents
if not usable_path(db_path):
print(f'Database path is invalid: {db_path}')
while not (db_path := usable_path(input('Please enter a valid path to '
'continue: '))):
pass
db_path = usable_path(db_path)
settings.database_path = db_path
if token is None or token == '':
print('Token has not been set, and will be required to access the GUI.'
'\n Enter the token below to save it now or manually enter it '
'into "toponym_settings.yaml" later')
token = input('Enter new token:')
if len(token) < 10:
token = 'Replace this text with your server token'
settings.server_token = token
settings.wiki_rows = min(0, abs(args.wiki_rows))
settings.save()
print('Settings saved.')