-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_file.py
65 lines (59 loc) · 2.4 KB
/
edit_file.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
from zipfile import ZipFile as zip
import os
import json
def unzip(file_path):
with zip(file_path, 'r') as f:
file = [file for file in f.namelist() if file.endswith(".tds") or file.endswith(".twb")].pop()
f.extract(file)
return file
def filter_change(tds_file, tdsx_path, filter_list):
with open(tds_file, "r") as t:
temp_file = t.read().splitlines()
x = 0
index = temp_file.index([line for line in temp_file if "filter class='categorical'" in line].pop())
line = temp_file[index]
while "/filter" not in line:
if "member=" in line:
if x == len(filter_list):
print(line)
temp_file.remove(line)
index -= 1
print(line)
else:
filter_name = line.split("member=")[1].split(""")[1]
temp_file[index] = line.replace(filter_name, filter_list[x])
x += 1
if "/groupfilter" in temp_file[index + 1] and x != len(filter_list):
new_line = line.replace(filter_name, filter_list[x])
temp_file.insert(index, new_line)
x += 1
index += 1
line = temp_file[index]
os.remove(tds_file)
with open(tds_file, 'x') as nf:
for line in temp_file:
nf.write(line + "\n")
with zip(tdsx_path, 'w') as z:
z.write(tds_file)
def write_measures(wb_name, dict):
with open(wb_name + ".twb", "r") as t:
temp_file = t.read().splitlines()
pre_line = ""
for line in temp_file:
if "formula" in line and "caption" in pre_line:
formula = line.split("formula='")[1].split("'")[0]
formula = formula.replace("&", "").replace("#", "").replace("\\", "")
try:
name = pre_line.split("caption='")[1].split("'")[0]
id = pre_line.split("name='")[1].split("'")[0]
update = True
for item in dict:
if id == dict[item]["id"]:
update = False
break
if update:
dict.update({name: {"id": id, "formula": formula, "wb_name": wb_name}})
except:
pass
pre_line = line
return dict