-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveDictToFile.py
227 lines (192 loc) · 8.73 KB
/
SaveDictToFile.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
import codecs
import copy
import datetime
import os
import pathlib
import re
import unittest
import openpyxl
from openpyxl.styles import Alignment
import GlobalFunctions
from GlobalFunctions import print # Todo use logging
def __init(data, filename, fieldnames, optimize):
if fieldnames is None: fieldnames = []
if not isinstance(data, (dict, list)): raise ValueError('Wrong data')
if not isinstance(filename, str): raise ValueError('Wrong filename')
if not isinstance(fieldnames, list): raise ValueError('Wrong fieldnames')
for key in fieldnames:
if not isinstance(key, str): raise ValueError('Wrong fieldnames')
data = copy.deepcopy(data)
fieldnames = __generate_fieldnames_optimized(data, fieldnames) if optimize else __generate_fieldnames_all(data)
return data, fieldnames
def __generate_fieldnames_optimized(data, fieldnames):
new_fieldnames = []
for each in data.values() if isinstance(data, dict) else data:
if not isinstance(each, dict): raise ValueError('Wrong data')
for key, value in each.items():
if value != '' and key not in new_fieldnames: new_fieldnames.append(str(key))
additional_fields = [x for x in new_fieldnames if x not in fieldnames]
cleared_fields = [x for x in fieldnames if x not in new_fieldnames]
if cleared_fields: print('deleted columns: ' + ', '.join(cleared_fields))
return [x for x in fieldnames if x in new_fieldnames] + additional_fields
def __generate_fieldnames_all(data):
all_fieldnames = []
for each in data.values() if isinstance(data, dict) else data:
if not isinstance(each, dict): raise ValueError('Wrong data')
for key, value in each.items():
if key not in all_fieldnames: all_fieldnames.append(str(key))
return all_fieldnames
def __view_enhancement(ws):
def auto_column_width():
dims = {}
for row in ws.iter_rows():
for cell in row:
cell.alignment = Alignment(shrinkToFit=True)
dims[cell.column_letter] = max((dims.get(cell.column_letter, 1), len(str(cell.value))))
for col, value in dims.items():
ws.column_dimensions[col].width = min(value + 2, 40)
ws.auto_filter.ref = f"A1:{openpyxl.utils.get_column_letter(ws.max_column)}{ws.max_row}"
ws.freeze_panes = ws['A2']
auto_column_width()
@GlobalFunctions.print_run_time
def save_to_xlsx(data, filename='', fieldnames=None, optimize=False, open=False, date_insert=True):
if not __check_data(data, filename): return None
data, fieldnames = __init(data, filename, fieldnames, optimize)
newfilename = _get_new_file_name_with_datetime('.xlsx', filename, date_insert)
wb = openpyxl.Workbook()
ws = wb.active
ws.append(fieldnames)
i = -1
for i, each in enumerate(data.values() if isinstance(data, dict) else data):
line = []
for key in fieldnames:
value = each.get(key, '')
if not isinstance(value, (int, float)) or len(str(value)) > 10:
value = str(value) if not optimize else re.sub(r'\s+', ' ', str(value)).strip()
line.append(value)
try:
ws.append(line)
except openpyxl.utils.exceptions.IllegalCharacterError:
print(f'save_to_xlsx: IllegalCharacterError: {line}') # Todo only for debug
ws.append([x.encode('unicode_escape').decode('utf-8') for x in line])
__view_enhancement(ws)
wb.save(newfilename)
print(f"{newfilename} / {i + 1} lines saved / ", end='')
if open: os.startfile(newfilename)
return newfilename
def __check_data(data, filename):
if data: return True
print(f'{filename} / nothing to save / ', end='')
return False
def _get_new_file_name_with_datetime(filetype, filename, date_insert):
if not date_insert:
if '\\' in filename: pathlib.Path(filename[:filename.rfind('\\')]).mkdir(parents=True, exist_ok=True)
return f'{filename}{filetype}'
date = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d_%H-%M")
if '\\' not in filename: return f'{date}_{filename}{filetype}'
last_pos = filename.rfind('\\')
pathlib.Path(filename[:last_pos]).mkdir(parents=True, exist_ok=True)
return f'{filename[:last_pos]}\\{date}_{filename[last_pos + 1:]}{filetype}'
@GlobalFunctions.print_run_time
def save_to_csv(data, filename='', fieldnames=None, optimize=False, open=False, date_insert=True, SEP=','):
if not __check_data(data, filename): return None
QC, NL = '"', '\r\n' # separator, quote char, new line
data, fieldnames = __init(data, filename, fieldnames, optimize)
if SEP == ',': file_extension = '.csv'
if SEP == '\t': file_extension = '.tsv'
newfilename = _get_new_file_name_with_datetime(file_extension, filename, date_insert)
with codecs.open(newfilename, 'w', encoding='utf-8') as file:
file.write(SEP.join([f'{QC}{x}{QC}' for x in fieldnames]) + NL)
i = -1
for i, each in enumerate(data.values() if isinstance(data, dict) else data):
line = []
for key in fieldnames:
value = each.get(key, '')
if isinstance(value, float): value = str(value)
value = str(value) if not optimize else re.sub(r'\s+', ' ', str(value)).strip()
line.append(value.replace('"', '""'))
file.write(SEP.join([f'{QC}{x}{QC}' for x in line]) + NL)
print(f"{newfilename} / {i + 1} lines saved / ", end='')
if open: os.startfile(newfilename)
return newfilename
def save_to_files(data, filename='', fieldnames=None, optimize=False, open=False, date_insert=True):
return (save_to_xlsx(data, filename, fieldnames, optimize, open, date_insert),
save_to_csv(data, filename, fieldnames, optimize, open, date_insert))
def _param_list_extend():
return [
# main
'url',
'sku',
'manufacturer', # may be ident
'herstellernummer',
'category_ids',
'name',
'short_description',
'description',
# images
'image',
'small_image',
'thumbnail',
'media_gallery',
'logo',
# dop
'weight',
'ean',
# same_for_all
'url_key',
'status', # ident
'qty', # ident
'is_in_stock', # ident
'tax_class_id', # ident
'attribute_set_name', # ident
# specific
'websites', # ident
'lieferant', # ident
'am_shipping_type', # ident
'country_of_manufacture', # may be ident
'delivery_time',
'relation', # через ||
# price
'price',
'special_price'
# 'original_price',
]
class SaveDictToFileTests(unittest.TestCase):
__data = {'2': {'first': '1\r\n1', 'second': '22.2', 'third': ''},
'3': {'first': '', 'second': '12345678901234567890', 'third': '"4""4'}}
def test_save_to_xlsx(self):
result = [['first', 'second', 'third'], ['1\r\n1', '22.2', 'None'],
['None', '12345678901234567890', '"4""4']]
xlsx = []
file_name = save_to_xlsx(self.__data)
sheet = openpyxl.load_workbook(file_name).active
for row in range(1, sheet.max_row + 1):
row_data = []
for column in range(1, sheet.max_column + 1):
row_data.append(str(sheet.cell(row=row, column=column).value))
xlsx.append(row_data)
self.assertEqual(result, xlsx)
import LoadDictFromFile
self.assertEqual(self.__data, LoadDictFromFile.load(file_name))
os.remove(file_name)
def test_save_to_csv(self):
result = '"first","second","third"\r\n"1\r\n1","22.2",""\r\n"","12345678901234567890","""4""""4"\r\n'
file_name = save_to_csv(self.__data)
with codecs.open(file_name, 'r', encoding='utf-8') as file:
csv = file.read()
self.assertEqual(result, csv)
import LoadDictFromFile
self.assertEqual(self.__data, LoadDictFromFile.load(file_name))
os.remove(file_name)
def test_get_new_file_name(self):
date = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d_%H-%M")
self.assertEqual('filename.xlsx',
_get_new_file_name_with_datetime('.xlsx', 'filename', date_insert=False))
self.assertEqual(f'{date}_filename.xlsx',
_get_new_file_name_with_datetime('.xlsx', 'filename', date_insert=True))
self.assertEqual('C:\\tmp\\filename.xlsx',
_get_new_file_name_with_datetime('.xlsx', 'C:\\tmp\\filename', date_insert=False))
self.assertEqual(f'C:\\tmp\\{date}_filename.xlsx',
_get_new_file_name_with_datetime('.xlsx', 'C:\\tmp\\filename', date_insert=True))
if __name__ == '__main__':
unittest.main()