-
Notifications
You must be signed in to change notification settings - Fork 7
/
oparl_cache.py
executable file
·292 lines (235 loc) · 10.6 KB
/
oparl_cache.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
#!/usr/bin/env python3
"""
OParl file cache
Downloads the contents of an OParl API into a file based cache, allowing easy retieval and incremental cache updates
# Usage
Create an `OParlCache` object with the url of the OParl entrypoint. Use the `load_to_cache()` method to download the
contents of the API. You can the retrieve objects using `get_from_cache(id)`. Update by calling `load_to_cache()`.
Note that embedded objects are stripped out and replaced by their id, by which you can retrieve them.
You can also use this script as a command line tool. Use "--help" for usage information.
# Implementation
The cache folder contains a file called "cache_status.json" with an entry for each known OParl server. An entry
contains the external lists of the server with the date of last update of that list. All OParl entities are stored in a
file in the cache folder whose path is a reformatted version of the url. For external lists only the ids of the elements
are stored.
"""
import argparse
import datetime
import json
import os
import requests
from collections import OrderedDict, defaultdict
from urllib.parse import urlparse, urlencode, parse_qs, urlunparse
from dateutil.tz import tzlocal
from threading import Lock
from concurrent.futures.thread import ThreadPoolExecutor
from itertools import islice
try:
from validate import validate_object
except ImportError:
print("Warning: validate.py not found")
class OParlCache:
def __init__(self, entrypoint, schemadir, cachedir, validate, external_list_limit=None, max_workes=None):
self.schema = {}
self.cachedir = cachedir
self.entrypoint = entrypoint
self.cache_status_file = os.path.join(self.cachedir, "cache_status.json")
self.external_lists = []
self.object_pairs_hook = OrderedDict
self.validate = validate
self.thread_pool = ThreadPoolExecutor(max_workers=max_workes)
self.futures = []
self.external_lists_lock = Lock()
self.external_list_limit = external_list_limit
for schemafile in os.listdir(schemadir):
with open(os.path.join(schemadir, schemafile)) as file:
self.schema[os.path.splitext(schemafile)[0]] = self.json_load_hooked(file)
if os.path.isfile(self.cache_status_file):
with open(self.cache_status_file) as f:
cache_status = self.json_load_hooked(f)
for i in cache_status:
if i["entrypoint"] == self.entrypoint:
self.external_lists = i["external_lists"]
break
else:
os.makedirs(os.path.dirname(self.cache_status_file), exist_ok=True)
with open(self.cache_status_file, "w") as f:
json.dump(self.json_loads_hooked("[]"), f)
@staticmethod
def iso8601_now():
return datetime.datetime.now().replace(microsecond=0, tzinfo=tzlocal()).isoformat()
def json_loads_hooked(self, data):
return json.loads(data, object_pairs_hook=self.object_pairs_hook)
def json_load_hooked(self, data):
return json.load(data, object_pairs_hook=self.object_pairs_hook)
def add_external_list(self, element):
self.external_lists_lock.acquire()
print("Scheduled: " + element["url"])
self.external_lists.append(element)
self.futures.append(self.thread_pool.submit(self.parse_external_list, element["url"], element["last_update"]))
self.external_lists_lock.release()
def url_to_path(self, url_raw):
"""
Takes an url as string and returns ap path in the format <cachedir>/<scheme>:<host>[:<port>][/<path>].json
:param url_raw:
:return: the path to where the corresponding url is cached
"""
url = urlparse(url_raw)
url_options = url.params
query = parse_qs(url.query)
query.pop("modified_since", None)
query.pop("modified_until", None)
query.pop("created_since", None)
query.pop("created_until", None)
if query != {}:
url_options += "?" + urlencode(query)
if url.fragment != "":
url_options += "#" + url.fragment
return os.path.join(self.cachedir, url.scheme + ":" + url.netloc, url.path[1:] + url_options + ".json")
def download_external_list(self, url):
"""
Yields all the objects from all pages of an external list
:param url:
:return:
"""
while True:
print("- " + url)
response = requests.get(url)
response.raise_for_status()
contents = response.json(object_pairs_hook=self.object_pairs_hook)
if "next" in contents["links"]:
url = contents["links"]["next"]
for i in contents["data"]:
yield i
if "next" not in contents["links"]:
return
def write_to_cache(self, url, cacheable):
filepath = self.url_to_path(url)
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w') as f:
json.dump(cacheable, f, indent=4)
def parse_entry(self, key, entry, entry_def):
if entry_def["type"] == "array":
i = 0
while i < len(entry):
entry[i] = self.parse_entry(key + "[" + str(i) + "]", entry[i], entry_def["items"])
i += 1
elif entry_def["type"] == "object":
if entry["type"] == "Feature":
return entry
self.parse_object(entry)
return entry["id"]
elif "references" in entry_def and entry_def["references"] == "externalList":
if entry not in [i["url"] for i in self.external_lists]:
self.add_external_list({"url": entry, "last_update": None})
return entry
def parse_object(self, target):
oparl_type = target["type"].split("/")[-1]
definition = self.schema[oparl_type]["properties"]
for key in set(target.keys()) & set(definition.keys()):
target[key] = self.parse_entry(key, target[key], definition[key])
self.write_to_cache(target["id"], target)
def parse_external_list(self, url_raw, last_update):
"""
:param url_raw: The url of the external list
:param last_update: The datetime in iso 8601 format when the lisdt was last synced or None
:return: the new last_update date
"""
this_sync = self.iso8601_now()
# Add the filter to the url
url_parts = list(urlparse(url_raw))
query = parse_qs(url_parts[4])
if last_update:
query.update({"modified_since": last_update})
url_parts[4] = urlencode(query)
url = urlunparse(url_parts)
collected_messages = defaultdict(int)
urls = []
if not self.external_list_limit:
external_list_iterator = self.download_external_list(url)
else:
external_list_iterator = islice(self.download_external_list(url), self.external_list_limit)
for i in external_list_iterator:
if self.validate:
valid, messages = validate_object(i, schema=self.schema)
if not valid:
for j in messages:
collected_messages[j] += 1
urls.append(i["id"])
self.parse_object(i)
if last_update:
with open(self.url_to_path(url)) as f:
old_urls = self.json_load_hooked(f)
else:
old_urls = []
self.write_to_cache(url, old_urls + urls)
self.external_lists_lock.acquire()
for i in self.external_lists:
if i["url"] == url_raw:
i["last_update"] = this_sync
break
self.save()
self.external_lists_lock.release()
return collected_messages
def load_to_cache(self):
response = requests.get(self.entrypoint)
response.raise_for_status()
entryobject = response.json(object_pairs_hook=self.object_pairs_hook)
self.parse_object(entryobject)
for i in self.external_lists:
print("Scheduled: " + i["url"])
self.futures.append(self.thread_pool.submit(self.parse_external_list, i["url"], i["last_update"]))
for i in self.futures:
collected_messages = i.result() # keep i.result() to have the future be executed
for key, value in collected_messages.items():
print("{:>5}x: {}".format(value, key))
self.thread_pool.shutdown()
def save(self):
with open(self.cache_status_file) as f:
cache_status = self.json_load_hooked(f)
for i in cache_status:
if i["entrypoint"] == self.entrypoint:
i["external_lists"] = self.external_lists
break
else:
cache_status.append({
"entrypoint": self.entrypoint,
"external_lists": self.external_lists
})
with open(self.cache_status_file, "w") as f:
json.dump(cache_status, f, indent=4)
def get_from_cache(self, url):
"""
Returnes cached API results for all cached objects
:param url:
:return:
"""
if not os.path.isfile(self.url_to_path(url)):
print(self.url_to_path(url))
return None
with open(self.url_to_path(url)) as f:
loaded = self.json_load_hooked(f)
if type(loaded) == list:
for i, j in enumerate(loaded):
with open(self.url_to_path(j)) as f:
loaded[i] = self.json_load_hooked(f)
return loaded
def main():
parser = argparse.ArgumentParser(description="CLI of the python OParl cache ")
parser.add_argument("--entrypoint", default="http://localhost:8080/oparl/v1.0")
parser.add_argument("--schemadir", default="~/oparl/schema/")
parser.add_argument("--cachedir", default="~/cache/")
parser.add_argument("--max-workers", default=None, type=int)
parser.add_argument("--external-list-limit", default=None, type=int,
help="Limits the number of objects retrieved from the external lists. \
This is for the validation and benchmarking purposes")
# Why do boolean flags no works?
parser.add_argument('--validate', dest='validate', action='store_true')
parser.add_argument('--no-validate', dest='validate', action='store_false')
parser.set_defaults(feature=True)
args = parser.parse_args()
oparl_cache = OParlCache(args.entrypoint, os.path.expanduser(args.schemadir), os.path.expanduser(args.cachedir),
args.validate, args.external_list_limit)
oparl_cache.load_to_cache()
if __name__ == '__main__':
main()