-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
125 lines (101 loc) · 2.58 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
import datetime
import re
import yaml
def str_to_dt(date_str):
if 'T' in date_str:
date_str = date_str.split('T')[0]
return datetime.datetime.strptime(
date_str, '%Y-%m-%d'
).date()
def fix_stem_quotes(txt):
opn = 0
end = 0
cur = 0
for char in txt:
if char == '[':
opn += 1
elif char == ']':
opn -= 1
if opn == -1:
end = cur
break
cur += 1
if end > 0:
txt_array = list(txt)
txt_array = ['`'] + txt_array
txt_array[end+1] = '`'
txt = ''.join(txt_array)
return txt
def convert_stem(txt):
if 'stem:[' in txt:
result = ''
output = []
stems_marks = []
for m in re.finditer('stem\:\[', txt):
stems_marks.append((m.start(), m.end()))
i = 1
for cur in stems_marks:
if i == 1 and cur[0] > 0:
output.append(txt[0:cur[0]])
if i < len(stems_marks):
output.append(fix_stem_quotes(txt[cur[1]:stems_marks[i][0]]))
else:
output.append(fix_stem_quotes(txt[cur[1]:]))
i += 1
return ''.join(output)
else:
return txt
def set_str(path, d, fn):
v = d.get_str(path, False)
if v:
d[path] = fn(v)
return d
def set_lst(path, d, fn):
# get_str_list
v = d.get_list(path, False)
if v:
_l = []
for elm in v:
_l.append(fn(elm))
d[path] = _l
return d
def format_values(raw):
if isinstance(raw, str):
try:
obj = yaml.load(raw, yaml.Loader)
except Exception:
pass
else:
return format_values(obj)
return [{'content': raw}]
elif isinstance(raw, list):
output = []
for r in raw:
out = format_values(r)
if isinstance(out, list):
output.append(out[0])
else:
output.append(out)
return output
elif isinstance(raw, dict):
return [raw]
ITEM_STATUSES = [
'submitted',
'valid',
'superseded',
'retired',
'invalid',
]
RUS_STATUSES = {
"действующий": "valid",
"замененный": "superseded",
"окончательный": "final",
"изъятый": "retired",
}
def get_status(status):
if status not in ITEM_STATUSES:
if not RUS_STATUSES.get(status, False):
raise Exception(f"Matching status not found --> {status}")
else:
return RUS_STATUSES[status]
return status