-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.py
executable file
·206 lines (181 loc) · 5.78 KB
/
cli.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
from elastic_wikidata import dump_to_es, sparql_to_es
from elastic_wikidata.config import runtime_config
import os
import click
from configparser import ConfigParser
@click.command()
@click.argument("source", nargs=1)
@click.option("--path", "-p", type=click.Path(exists=True))
@click.option(
"--cluster", envvar="ELASTICSEARCH_CLUSTER", help="Elasticsearch cluster URL"
)
@click.option("--user", envvar="ELASTICSEARCH_USER", help="Elasticsearch username")
@click.option(
"--password", envvar="ELASTICSEARCH_PASSWORD", help="Elasticsearch password"
)
@click.option(
"--agent_contact",
"-contact",
envvar="WIKIMEDIA_AGENT_CONTACT",
help="(optional) Contact details to add to the User Agent header for Wikidata requests",
default=None,
)
@click.option(
"--config",
"-c",
type=click.Path(exists=True),
help="Path to .ini file containing Elasticsearch credentials",
)
@click.option(
"--index",
"-i",
prompt="Elasticsearch index",
help="Name of Elasticsearch index to load into",
)
@click.option(
"--limit", "-l", type=int, help="(optional) Limit the number of entities loaded in"
)
@click.option("--page_size", type=int, help="Page size for SPARQL query.", default=100)
@click.option(
"--language", "-lang", type=str, help="Language (Wikimedia language code)"
)
@click.option(
"--properties",
"-prop",
type=str,
help="One or more Wikidata property e.g. 'p31' or 'p31 p21'. A path to a file containing newline-separated properties can also be passed. Not case-sensitive",
)
@click.option(
"--timeout",
"-t",
type=int,
help="Timeout for Wikidata requests (seconds)",
default=6,
)
@click.option(
"--disable_refresh/--no_disable_refresh",
"-dr/-ndr",
help="Whether to disable Elasticsearch's (CPU-intensive) refresh during data load. Defaults to True. Recommended to leave this on for low-resource machines or large datasets.",
default=True,
)
def main(
source,
path,
cluster,
user,
password,
agent_contact,
config,
index,
limit,
page_size,
language,
properties,
timeout,
disable_refresh,
):
# get elasticsearch credentials
if config:
# read .ini file
parser = ConfigParser()
parser.optionxform = str # make option names case sensitive
parser.read(config)
es_credentials = parser._sections["ELASTIC"]
check_es_credentials(es_credentials)
runtime_config.add_item(
{
"user_agent_contact": parser._sections["HTTP"].get(
"CONTACT_DETAILS", None
)
}
)
else:
# check environment variables/flags
es_credentials = {}
if cluster:
es_credentials["ELASTICSEARCH_CLUSTER"] = cluster
if user:
es_credentials["ELASTICSEARCH_USER"] = user
if password:
es_credentials["ELASTICSEARCH_PASSWORD"] = password
check_es_credentials(es_credentials)
runtime_config.add_item({"user_agent_contact": agent_contact})
runtime_config.add_item({"http_timeout": timeout})
# global flag for all functions that the module is being run through the CLI
runtime_config.add_item({"cli": True})
# set kwargs
kwargs = {}
if language:
kwargs["lang"] = language
if properties:
if os.path.exists(properties):
with open(properties, "r") as f:
kwargs["properties"] = f.read().splitlines()
else:
kwargs["properties"] = properties.split()
if disable_refresh:
kwargs["disable_refresh_on_index"] = disable_refresh
# run job
if source == "dump":
load_from_dump(path, es_credentials, index, limit, **kwargs)
elif source == "query":
load_from_sparql(path, es_credentials, index, limit, page_size, **kwargs)
else:
raise ValueError(f"Argument {source} must be either dump or sparql")
def load_from_dump(path, es_credentials, index, limit, **kwargs):
if not kwargs:
kwargs = {}
if limit:
kwargs["doc_limit"] = limit
# limit is used when dumping JSON to Elasticsearch
d = dump_to_es.processDump(
dump=path, es_credentials=es_credentials, index_name=index, **kwargs
)
d.start_elasticsearch()
d.dump_to_es()
def load_from_sparql(path, es_credentials, index, limit, page_size=100, **kwargs):
if not kwargs:
kwargs = {}
with open(path, "r") as f:
query = f.read()
# limit is used when getting list of entities
print("Getting entities from SPARQL query")
entity_list = sparql_to_es.get_entities_from_query(
query, page_size=100, limit=limit
)
print(
f"Retrieving information from wbgetentities API and pushing to ES index {index}"
)
d = dump_to_es.processDump(
dump=entity_list, es_credentials=es_credentials, index_name=index, **kwargs
)
d.start_elasticsearch()
d.dump_to_es()
def check_es_credentials(credentials: dict):
credentials_present = set(credentials.keys())
credentials_required = {
"ELASTICSEARCH_CLUSTER",
"ELASTICSEARCH_USER",
"ELASTICSEARCH_PASSWORD",
}
missing_credentials = credentials_required - credentials_present
if len(missing_credentials) > 0:
raise ValueError(f"Missing Elasticsearch credentials: {missing_credentials}")
if __name__ == "__main__":
# main(
# source='dump',
# path="../wikidata/all_no_articles.ndjson",
# properties="p31,p279",
# config="./config.ini",
# index='wikidump2',
# cluster=None,
# user=None,
# password=None,
# agent_contact=False,
# limit=None,
# page_size=100,
# language='en',
# timeout=6,
# disable_refresh=True
# )
main()