-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_csv_file.py
More file actions
193 lines (162 loc) · 10.5 KB
/
update_csv_file.py
File metadata and controls
193 lines (162 loc) · 10.5 KB
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
import encode_decode_map_schema
import tag_utils
def update_csv(map_schema, area_map, loc_equip, loc_item, loc_tagname, loc_cluster, loc_iodev, loc_project_name,
file_name, new_equip_list, equipment_map_dict, equipment_type_map_dict, item_type_map_dict,
area_prefix_map_dict):
scratch_file = file_name.replace('.csv', '-working.csv')
with open(scratch_file, mode='wt', encoding='utf-8') as sf:
existing_cluster_equip_item_list = list()
with open(file_name, mode='rt', encoding='utf-8') as rf:
read_in_record_count = 0
for read_line in rf:
read_in_record_count += 1
if read_in_record_count > 1:
existing_csv_equipment = tag_utils.read_in_data(loc_equip, read_line.strip())
if not existing_csv_equipment == '':
exiting_csv_tag = tag_utils.read_in_data(loc_tagname, read_line.strip())
existing_csv_cluster = tag_utils.read_in_data(loc_cluster, read_line.strip())
existing_csv_item = tag_utils.read_in_data(loc_item, read_line.strip())
if existing_csv_item == '': # citect will use tag if no item is defined so record tag for item
existing_csv_item = exiting_csv_tag
equip_key = existing_csv_cluster + ':' + existing_csv_equipment
if equip_key.lower() not in existing_cluster_equip_item_list:
existing_cluster_equip_item_list.append(equip_key.lower())
with open(file_name, mode='rt', encoding='utf-8') as rf:
read_in_record_count = 0
for read_line in rf:
mod_line = read_line
read_in_record_count += 1
if read_in_record_count > 1:
exiting_csv_tag = tag_utils.read_in_data(loc_tagname, read_line.strip())
existing_csv_equipment = tag_utils.read_in_data(loc_equip, read_line.strip())
matrix0, mode, generalised_schema = encode_decode_map_schema.decode_mapping_schema(map_schema)
current_schema, g_tag = tag_utils.get_schema(exiting_csv_tag)
generalised_current_schema = encode_decode_map_schema.generalise_schema(matrix0, mode,
current_schema)
if generalised_current_schema.find(generalised_schema) == 0 and existing_csv_equipment == '':
existing_csv_cluster = tag_utils.read_in_data(loc_cluster, read_line.strip())
existing_csv_iodev = tag_utils.read_in_data(loc_iodev, read_line.strip())
exiting_csv_project_name = tag_utils.read_in_data(loc_project_name, read_line.strip())
# construct equipment
equip_level_tree = matrix0[0]
first_level_tree = matrix0[1]
second_level_tree = matrix0[2]
third_level_tree = matrix0[3]
fourth_level_tree = matrix0[4]
last_digit = matrix0[5]
equip_num_start = matrix0[6]
equip_num_end = matrix0[7]
prefix_area_key = ''
new_equip = '.'
area_key = ''
for chars in area_map.rsplit('.'):
if chars.find('Prefix') == 1:
# get prefix + Area
char = chars[len('xPrefix'):]
prefix = chars[0:len('xPrefix')]
if char == 'A' and first_level_tree >= 0:
area_key += tag_utils.add_equip_part(g_tag, first_level_tree, mode)
prefix_area_key += area_prefix_map_dict[prefix] + \
tag_utils.add_equip_part(g_tag, first_level_tree, mode)
elif char == 'B' and second_level_tree >= 0:
area_key += '.' + tag_utils.add_equip_part(g_tag, second_level_tree, mode)
prefix_area_key += '.' + area_prefix_map_dict[prefix] + \
tag_utils.add_equip_part(g_tag, second_level_tree, mode)
elif char == 'C' and third_level_tree >= 0:
area_key += '.' + tag_utils.add_equip_part(g_tag, third_level_tree, mode)
prefix_area_key += '.' + area_prefix_map_dict[prefix] + \
tag_utils.add_equip_part(g_tag, third_level_tree, mode)
elif char == 'D' and fourth_level_tree >= 0:
area_key += '.' + tag_utils.add_equip_part(g_tag, fourth_level_tree, mode)
prefix_area_key += '.' + area_prefix_map_dict[prefix] + \
tag_utils.add_equip_part(g_tag, fourth_level_tree, mode)
else:
# add equip type + equip numbers
for char in chars:
if char == 'E':
new_equip += equipment_type_map_dict[g_tag[equip_level_tree]]
if char == 'X':
for index in range(len(current_schema)):
if index >= equip_num_start and index <= equip_num_end:
new_equip += g_tag[index]
# construct item
item = ''
for index in range(len(current_schema)):
if index >= last_digit:
item += g_tag[index]
if item in item_type_map_dict:
new_item = item_type_map_dict[item]
else:
new_item = item
# record all new equipment + iodevice and project name
new_area_key = existing_csv_cluster + ':' + area_key
if new_area_key in equipment_map_dict:
# if mapped area is not the same as default then add prefix
if not new_area_key == existing_csv_cluster + ':' + equipment_map_dict[new_area_key]:
prefix_area_key = equipment_map_dict[new_area_key]
prefix_area_key += new_equip
cluster_area_key = existing_csv_cluster + ':' + prefix_area_key
# check if area parts and equip part is valid
is_key_valid = 0
for area in cluster_area_key[cluster_area_key.find(':')+1:].rsplit('.'):
if len(area) == 0:
is_key_valid = 0
break
else:
is_key_valid = 1
if is_key_valid:
# add to new equipment to list if not used anywhere
is_key_exists = 0
if cluster_area_key in new_equip_list:
is_key_exists = 1
if not is_key_exists and not cluster_area_key.lower() in existing_cluster_equip_item_list:
new_equip_list[cluster_area_key] = [existing_csv_iodev, exiting_csv_project_name]
# insert equip and item into mod_line if equipment generated is valid and cell empty
record_list = mod_line.rsplit(',"')
if record_list[loc_equip] == '""':
for index in range(len(record_list)):
record_list[index] = '\"' + record_list[index] # add leading "
record_list[loc_item] = '\"' + new_item + '\"'
record_list[loc_equip] = '\"' + prefix_area_key + '\"'
mod_line = ','.join(record_list)
sf.write(mod_line)
return new_equip_list
def update_equipment_csv(loc_equip, loc_cluster, loc_iodev, loc_project_name, file_name, new_equip_list):
scratch_file = file_name.replace('.csv', '-working.csv')
with open(scratch_file, mode='wt', encoding='utf-8') as sf:
# read existing equipment and delete from new equipment list if match
with open(file_name, mode='rt', encoding='utf-8') as rf:
read_in_record_count = 0
for read_line in rf:
read_in_record_count += 1
sf.write(read_line) # copy existing lines
if read_in_record_count > 1:
cluster = tag_utils.read_in_data(loc_cluster, read_line.strip()).strip('"')
equipment = tag_utils.read_in_data(loc_equip, read_line.strip()).strip('"')
equip_key = cluster + ':' + equipment
is_duplicate = 0
for key in new_equip_list:
if equip_key.lower() == key.lower():
is_duplicate = 1
if is_duplicate:
del new_equip_list[key] # remove equipment entry as it already exists
else:
header = read_line
# clear line so it can be used as a template
new_list = header.rsplit(',')
for item in range(len(new_list)):
new_list[item] = '""'
for equipment in new_equip_list:
cluster = equipment[0:equipment.find(':')]
equip = equipment[equipment.find(':') + 1:]
new_list[loc_cluster] = '\"' + cluster + '\"'
new_list[loc_equip] = '\"' + equip + '\"'
iodev = new_equip_list[equipment][0]
project_name = new_equip_list[equipment][1]
if len(iodev) > 0:
new_list[loc_iodev] = '\"' + iodev + '\"'
if len(project_name) > 0:
new_list[loc_project_name] = '\"' + project_name + '\"'
mod_line = '\n' + ','.join(new_list)
sf.write(mod_line) # write new line
read_in_record_count += 1