-
Notifications
You must be signed in to change notification settings - Fork 0
/
bettercsv.py
110 lines (85 loc) · 3.07 KB
/
bettercsv.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
import os
import csv
from collections import OrderedDict
class Parser(object):
def __init__(self, path):
self.path = path
self.raw_csv = []
self.unparsed_files = []
# lists
self.vehicles = []
self.engines = []
self.ecus = []
# related data
self.vehicle_engines = []
self.engine_ecus = []
def validate_csv(self, file_path):
try:
file = open(file_path)
except PermissionError:
print("access denied in creating file!")
return False
except IOError:
print("could not read file!")
return False
reader = csv.reader(file)
for row in reader:
if len(row) != 8:
file.close()
return False
file.close()
return True
def read_csv(self, file_path):
valid = self.validate_csv(file_path)
if valid:
try:
file = open(file_path)
except PermissionError:
print("access denied in creating file!")
return False
except IOError:
print("could not read file!")
return False
reader = csv.reader(file)
for row in reader:
self.raw_csv.append(row)
file.close()
else:
self.unparsed_files.append(file_path)
def add_vehicle(self, brand, model, year):
if brand != '':
brand = brand.lower()
brand = brand.title()
if year == '':
year = None
vehicle = {'brand': brand, 'model': model, 'year': year}
if vehicle not in self.vehicles:
self.vehicles.append(vehicle)
return self.vehicles.index(vehicle)
def add_engine(self, engine, fuel):
engine = {'engine': engine, 'fuel': fuel}
if engine not in self.engines:
self.engines.append(engine)
return self.engines.index(engine)
def add_ecu(self, ecu_type, model, version):
if ecu_type == 'ECU':
ecu = {'model': model, 'version': version}
if ecu not in self.ecus:
self.ecus.append(ecu)
return self.ecus.index(ecu)
def parse_rows(self):
raw_data = self.raw_csv
for (brand, model, engine, fuel, year, ecu, ecu_model, ecu_version) in raw_data:
vehicle = self.add_vehicle(brand, model, year)
engine = self.add_engine(engine, fuel)
ecu = self.add_ecu(ecu, ecu_model, ecu_version)
ve = OrderedDict([('vehicle_id', vehicle), ('engine_id', engine)])
self.vehicle_engines.append(ve)
# sometimes engines don't have the ecu
if(ecu):
ee = OrderedDict([('engine_id', engine), ('ecu_id', ecu)])
self.engine_ecus.append(ee)
def read(self, file):
file_path = self.path + os.sep + file
self.read_csv(file_path)
self.parse_rows()