-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·198 lines (159 loc) · 4.72 KB
/
main.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re, json
import os, sys
from ModelInterface import *
################
# Model Interface
# ~S.
################
path = os.path.abspath(os.path.dirname(sys.argv[0]))
def metal(value, model, dicts, default = None):
"""Trivial metal dictionary fixer
value: fixed values list
model: validation model of appropriate type
dicts: word token dictionary
default: default value for not-found keys
"""
data = []
for item in value:
instance = {}
for d in dicts:
for v in model.model:
for w in dicts[d]:
if w in str(item[v]).lower():
instance[d] = dicts[d][w]
break
if instance.get(d):
break
if not instance.get(d):
instance[d] = default
data.append(instance)
return data
def as_is(value):
"""Dummy function"""
return value
def split(value, key, delimiter, model):
"""Split value by delimiter
value: parsed list value
key: field to split by
delimiter: regex pattern
model: validation model of appropriate type
"""
instances = []
for v in value:
for s in [x.strip() for x in delimiter.split(v[key]) if x.strip()]:
i = model.parse(v)
i[key] = s
instances.append(i)
return instances
def match(value, field, token):
"""Get fuzzy values identified by word tokens
value: parsed list value
field: destination value[field]
token: regex pattern
"""
for v in value:
s = token.search(v["insertion"])
try:
v[field] = s.group(0)
v["insertion"] = v["insertion"].replace(s.group(0), '')
except AttributeError:
pass
return value
# Модель блока металлов
mm = model({
'metal': parse_str,
'sample': parse_int,
'color': parse_str
})
# Словарь по металлам
dictpath = os.path.join(path, "metaldict.json")
print()
print("Loading dictionaries from %r" % (dictpath,))
try:
with open(dictpath) as f:
mdict = json.load(f)
print("Done")
except OSError as ex:
mdict = {}
print(ex)
# Модель блока размеров
dm = model({
'size': parse_float,
'weight': parse_float,
'length': parse_float,
'height': parse_float,
'currency': parse_str,
'first_price': parse_float,
'last_price': parse_float,
'width': parse_float
})
# Модель блока вставок
im = model({
'carat': parse_float,
'color': parse_int,
'count': parse_int,
'cut': parse_str,
'cut_count': parse_int,
'form': parse_str,
'insertion': parse_str,
'purity': parse_int,
'type': parse_str
})
# Интерфейс основной модели
i = interface("provider_name", {
"nebo": {},
"adamas": {
"insertion_group_list": (
(split, {"key": "insertion", "delimiter": re.compile('\s*;\s*'), "model": im}),
(match, {"field": "count", "token": re.compile(r'([0-9]+)\s*шт(?:ук)?[.]*', re.IGNORECASE)}),
(match, {"field": "carat", "token": re.compile(r'((?:[0-9]*[,.])?[0-9]+)\s*кар(?:ат)?[.]*', re.IGNORECASE)}),
)
}
})
# Основная модель
bm = model({
"stuff_list": (blocklist, {"model": model({
'brand': parse_str,
'crucifixion': parse_bool,
'dimensions': (blocklist, {"model": dm}),
'insertion_group_list': (blocklist, {"model": im}),
'insertion_count': parse_int,
'is_child': parse_bool,
'lock': parse_list,
'metal': (trigger, {"tlist": [(metal, {"model": mm, "dicts": mdict}), (blocklist, {"model": mm})]}),
'metal_count': parse_int,
'name': parse_str,
'product_type': parse_str,
'provider': parse_str,
'provider_images': parse_list,
'provider_name': parse_str,
'provider_url': parse_str,
'religion': parse_bool,
'sex': parse_str,
'soroka_images': parse_list,
'style': parse_list,
'vendor_code': parse_str,
'weaving': parse_str
}, i)})
})
# Чтение
srcpath = os.path.join(path, 'sample-data.json')
print("Reading data from file %r" % (srcpath,))
try:
with open(srcpath) as f:
data = json.load(f)
print("Done")
except OSError as ex:
exit(ex)
# Запись
respath = os.path.join(path, 'result.json')
print("Writing data to file %r" % (respath,))
try:
with open(respath, 'w') as f:
json.dump(bm.parse(data), f, ensure_ascii = False, indent = 2)
print("Done")
print()
except OSError as ex:
exit(ex)