-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
139 lines (108 loc) · 3.92 KB
/
utils.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
import re
from typing import Iterable, Optional, Dict, Sequence
import jinja2
import requests
from filters import datetimeformat, extendlinks, titlefilter
TEXT_REPLACEMENTS = {
"‘": "'",
"’": "'",
"“": '"',
"”": '"',
"–": "--",
}
TITLE_REPLACEMENTS = {
"&": "and"
}
LINE_ENDINGS = re.compile(r"\s+\n")
def load_allow_list(name: str, allow_list: Sequence) -> Sequence:
for elem in allow_list:
if isinstance(elem, dict):
first_key = list(elem.keys())[0]
if name == first_key:
return elem[name]
return []
def traverse_into(name, **namespace):
head = namespace[name[0]]
for path in name[1:]:
if isinstance(head, dict):
head = head[path]
else:
head = getattr(head, path)
return head
def create_template(template_file):
templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
templateEnv = jinja2.Environment(loader=templateLoader)
templateEnv.filters['extendlinks'] = extendlinks
templateEnv.filters['datetimeformat'] = datetimeformat
templateEnv.filters['titlefilter'] = titlefilter
template = templateEnv.get_template(template_file)
return template
def to_float(value: str, default: float = 0.0) -> float:
if value is None:
return default
return float(value)
def to_str(value: str, default: Optional[str] = None) -> Optional[str]:
if value is None or "none" == value.lower():
return default
return value
def to_text(value: str) -> Optional[str]:
"""Function ensures proper encoding of entities"""
if value:
value = replace_text(value, TEXT_REPLACEMENTS)
# also ensure proper line endings
value = LINE_ENDINGS.sub("\n\n", value)
return value
def to_title(value: str) -> Optional[str]:
"""Function ensures proper encoding of titles"""
return replace_text(value, TITLE_REPLACEMENTS)
def replace_text(text: str, replacements: Dict[str, str]) -> Optional[str]:
if text:
for old, replacement in replacements.items():
text = text.replace(old, replacement)
return text
def to_bool(value: str, default: bool = False) -> bool:
if isinstance(value, bool):
return value
if value is None:
return default
value = value.lower()
if "yes" in value or "true" in value:
return True
return False
def find_custom_fields_key(keys: Sequence[str], name: str) -> Optional[str]:
for index, key in enumerate(keys):
if name in key:
return keys[index]
return None
def flatten_custom_fields(elements: Sequence[Dict]) -> Dict:
if len(elements) == 0:
return {}
assert "name" in elements[0] and "value" in elements[0]
return {element["name"]: element["value"] for element in elements}
def load_orcid_information(orcids: Iterable[str]) -> Dict[str, Dict]:
result = {}
for orcid in orcids:
response = requests.get(f"https://orcid.org/{orcid}/person.json")
if response.status_code == 200:
orcid_data = response.json()
result[orcid] = {
"displayName": orcid_data["displayName"],
"publicGroupedOtherNames": orcid_data["publicGroupedOtherNames"]
}
return result
def check_name(first, last, to_check):
if first in to_check and last in to_check:
return True
return False
def search_matching_orcid(
first_name: str, last_name: str, orcids: Dict[str, Dict]) -> Optional[str]:
first_name = first_name.lower()
last_name = last_name.lower()
for orcid_id, orcid_information in orcids.items():
if check_name(first_name, last_name, orcid_information["displayName"].lower()):
return orcid_id
# also check for other names
for key in orcid_information["publicGroupedOtherNames"]:
if check_name(first_name, last_name, key.lower()):
return orcid_id
return None