-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchup.py
100 lines (79 loc) · 4.67 KB
/
touchup.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
import csv
import os
import json
with open("config.json") as config_file:
CONFIG = json.load(config_file)
def debug_print(debug_lvl, req_lvl, print_str):
if debug_lvl >= req_lvl:
print(print_str)
def recurcive_file_list(dir_name):
list_of_file = os.listdir(dir_name)
all_files = []
for file in list_of_file:
if file in ["venv",".idea"]: # directories no ordinary person would be messing with
continue
full_path = os.path.join(dir_name, file)
if os.path.isdir(full_path):
all_files = all_files + recurcive_file_list(full_path)
else:
all_files.append(full_path)
return all_files
def touch_up_csvs(bugging=0):
debug_print(bugging, 1, "loading directory path: %s" % os.getcwd())
array_to_save = []
for filename in recurcive_file_list(os.getcwd()):
debug_print(bugging, 1, "checking file: %s" % filename)
if "ShopifyImportFile" in filename and ".csv" in filename:
debug_print(bugging, 1, " shopify file found, beginning edit...")
with open(filename, encoding="utf8") as raw_file:
reader = csv.reader(raw_file, delimiter=",")
line = 1 # counting from 1 because excel and openoffice count lines from 1
for i in reader:
if line != 1:
debug_print(bugging, 1, " editing line %s: %s" % (line, i))
debug_print(bugging, 1, " editing title: %s" % i[1])
i[1] = i[1].title()
for j in CONFIG["title_rep_dict"]:
if j in i[1]:
i[1] = i[1].replace(j, CONFIG["title_rep_dict"][j])
while '(' in i[1]:
starter = i[1].find('(')
ender = i[1][starter:].find(')')
if ender != -1:
i[1] = i[1].replace(i[1][starter:ender+starter+1], '')
else:
i[1] = i[1].replace(i[1][starter:], '')
i[1] = i[1].strip()
debug_print(bugging, 1, " title after editing: %s" % i[1])
debug_print(bugging, 1, " editing vendor: %s" % i[3])
i[3] = i[3].title()
i[3] = i[3].replace('Dc', 'DC')
i[3] = i[3].strip()
debug_print(bugging, 1, " vendor after editing: %s" % i[3])
debug_print(bugging, 1, " adding vendor to tags if tags is empty")
if i[5] == "":
i[5] = "\"" + i[3] + "\""
debug_print(bugging, 1, " tags is now: %s" % i[5])
array_to_save.append(i)
line += 1
debug_print(bugging, 1, "saving touched up contents in a final .csv")
with open("touched_up_and_combined.csv", "w", encoding="utf8") as output:
output_writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
debug_print(bugging, 1, " writing header row")
output_writer.writerow(
["Handle", "Title", "Body (HTML)", "Vendor", "Type", "Tags", "Published", "Option1 Name", "Option1 Value",
"Option2 Name", "Option2 Value", "Option3 Name", "Option3 Value", "Variant SKU", "Variant Grams",
"Variant Inventory Tracker", "Variant Inventory Qty", "Variant Inventory Policy",
"Variant Fulfillment Service", "Variant Price", "Variant Compare At Price", "Variant Requires Shipping",
"Variant Taxable", "Variant Barcode", "Image Src", "Image Position", "Image Alt Text", "Gift Card",
"SEO Title", "SEO Description", "Google Shopping / Google Product Category", "Google Shopping / Gender",
"Google Shopping / Age Group", "Google Shopping / MPN", "Google Shopping / AdWords Grouping",
"Google Shopping / AdWords Labels", "Google Shopping / Condition", "Google Shopping / Custom Product",
"Google Shopping / Custom Label 0", "Google Shopping / Custom Label 1", "Google Shopping / Custom Label 2",
"Google Shopping / Custom Label 3", "Google Shopping / Custom Label 4", "Variant Image",
"Variant Weight Unit", "Variant Tax Code", "Cost per item"])
for i in array_to_save:
debug_print(bugging, 1, " writing row for %s" % i[1])
output_writer.writerow(i)
if __name__ == "__main__":
touch_up_csvs(1)