generated from greenelab/lab-website-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
236 lines (185 loc) · 5.86 KB
/
util.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
"""
utility functions for cite process and plugins
"""
import subprocess
import json
import yaml
from yaml.loader import SafeLoader
from pathlib import Path
from datetime import date, datetime
from rich import print
from diskcache import Cache
# cache for time-consuming network requests
cache = Cache("./_cite/.cache")
# clear expired items from cache
cache.expire()
def log_cache(func):
"""
decorator to use around memoized function to log if cached or or not
"""
def wrap(*args):
key = func.__cache_key__(*args)
if key in cache:
log(" (from cache)", level="INFO", newline=False)
return func(*args)
return wrap
def log(message="\n--------------------\n", indent=0, level="", newline=True):
"""
log to terminal, color determined by indent and level
"""
palette = {
0: "[orange1]",
1: "[salmon1]",
2: "[violet]",
3: "[sky_blue1]",
"ERROR": "[white on #F43F5E]",
"WARNING": "[black on #EAB308]",
"SUCCESS": "[black on #10B981]",
"INFO": "[grey70]",
}
color = get_safe(palette, level, "") or get_safe(palette, indent, "") or "[white]"
if newline:
print()
print(indent * " " + color + str(message) + "[/]", end="", flush=True)
def label(entry):
"""
get "label" of dict entry (for logging purposes)
"""
return str(list(entry.keys())[0]) + ": " + str(list(entry.values())[0])
def get_safe(item, path, default=None):
"""
safely access value in nested lists/dicts
"""
for part in str(path).split("."):
try:
part = int(part)
except ValueError:
part = part
try:
item = item[part]
except (KeyError, IndexError, AttributeError, TypeError):
return default
return item
def list_of_dicts(data):
"""
check if data is list of dicts
"""
return isinstance(data, list) and all(isinstance(entry, dict) for entry in data)
def format_date(_date):
"""
format date as YYYY-MM-DD, or no date if malformed
"""
if isinstance(_date, int):
return datetime.fromtimestamp(_date // 1000.0).strftime("%Y-%m-%d")
if isinstance(_date, (date, datetime)):
return _date.strftime("%Y-%m-%d")
try:
return datetime.strptime(_date, "%Y-%m-%d").strftime("%Y-%m-%d")
except Exception:
return ""
def load_data(path):
"""
read data from yaml or json file
"""
# convert to path object
path = Path(path)
# check if file exists
if not path.is_file():
raise Exception("Can't find file")
# try to open file
try:
file = open(path, encoding="utf8")
except Exception as e:
raise Exception(e or "Can't open file")
# try to parse as yaml
try:
with file:
data = yaml.load(file, Loader=SafeLoader)
except Exception:
raise Exception("Can't parse file. Make sure it's valid YAML.")
# if no errors, return data
return data
def save_data(path, data):
"""
write data to yaml file
"""
# convert to path object
path = Path(path)
# try to open file
try:
file = open(path, mode="w")
except Exception:
raise Exception("Can't open file for writing")
# prevent yaml anchors/aliases (pointers)
yaml.Dumper.ignore_aliases = lambda *args: True
# try to save data as yaml
try:
with file:
yaml.dump(data, file, default_flow_style=False, sort_keys=False)
except Exception:
raise Exception("Can't save YAML to file")
# write warning note to top of file
note = "# DO NOT EDIT, GENERATED AUTOMATICALLY"
try:
with open(path, "r") as file:
data = file.read()
with open(path, "w") as file:
file.write(f"{note}\n\n{data}")
except Exception:
raise Exception("Can't write to file")
@log_cache
@cache.memoize(name="manubot", expire=90 * (60 * 60 * 24))
def cite_with_manubot(_id):
"""
generate citation data for source id with Manubot
"""
# run Manubot
try:
commands = ["manubot", "cite", _id, "--log-level=WARNING"]
output = subprocess.Popen(commands, stdout=subprocess.PIPE).communicate()
except Exception as e:
log(e, 3)
raise Exception("Manubot could not generate citation")
# parse results as json
try:
manubot = json.loads(output[0])[0]
except Exception:
raise Exception("Couldn't parse Manubot response")
# new citation with only needed info
citation = {}
# original id
citation["id"] = _id
# title
citation["title"] = get_safe(manubot, "title", "").strip()
# authors
citation["authors"] = []
for author in get_safe(manubot, "author", {}):
given = get_safe(author, "given", "").strip()
family = get_safe(author, "family", "").strip()
if given or family:
citation["authors"].append(" ".join([given, family]))
# publisher
container = get_safe(manubot, "container-title", "").strip()
collection = get_safe(manubot, "collection-title", "").strip()
publisher = get_safe(manubot, "publisher", "").strip()
citation["publisher"] = container or publisher or collection or ""
# extract date part
def date_part(citation, index):
try:
return citation["issued"]["date-parts"][0][index]
except (KeyError, IndexError, TypeError):
return ""
# date
year = date_part(manubot, 0)
if year:
# fallbacks for month and day
month = date_part(manubot, 1) or "1"
day = date_part(manubot, 2) or "1"
citation["date"] = format_date(f"{year}-{month}-{day}")
else:
# if no year, consider date missing data
citation["date"] = ""
# link
citation["link"] = get_safe(manubot, "URL", "").strip()
# return citation data
return citation