-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze_copy.py
181 lines (166 loc) · 6.3 KB
/
analyze_copy.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
import important_functions as important_functions
imf= important_functions.important_func()
import os
headers_content=[]
def modify_headers(headers):
new_headers=[]
for i in headers:
if i[-1]==">":
new_headers.append(i[:-1])
else:
new_headers.append(i)
return new_headers
def end_headers_generator(headers):
end_headers=[]
for i in headers:
end_headers.append(i[0]+"/"+i[1:]+">")
return end_headers
def analyze_source(headers, file_name, file, contents):
headers=modify_headers(headers)
end_headers=end_headers_generator(headers)
headers_i=0
for i in headers:
copy_text=""
analyze_header="<"
end_header=""
check_end_header=False
if_analyze_header=False
if_copy_text=False
for j in contents:
if j=="<" and if_analyze_header==False:
if_analyze_header=True
continue
if if_analyze_header==True and if_copy_text==False:
if j==" " or j==">":
if i==analyze_header:
if_copy_text=True
copy_text+=analyze_header+j
continue
else:
if_analyze_header=False
analyze_header="<"
continue
analyze_header+=j
if if_copy_text==True:
copy_text+=j
if j=="<":
end_header+=j
check_end_header=True
continue
if check_end_header==True:
end_header+=j
if j==">" and end_headers[headers_i]==end_header:
headers_content.append(copy_text)
headers_i=headers_i+1
break
elif j==">" and end_header[headers_i]!=end_header:
end_header=""
check_end_header=False
def find_files_to_edit(file_name, search_subfolders):
def search_files(files_to_edit_temp, loc):
folders=[]
for i in files_to_edit_temp:
if os.path.isfile(loc+i)==False:
if search_subfolders==True:
folders.append(loc+i)
else:
files_to_edit.append(loc+i)
return folders
def look_subfolders(loc, folders):
if search_subfolders==True:
print("przeszukuję foldery")
for i in folders:
print(f"Jestem w folderze: {i}")
files=os.listdir(loc+i)
foldersi=search_files(files, loc+i)
print(f"foldersi: {foldersi}")
print(foldersi)
if foldersi!=['']:
look_subfolders(loc+i, foldersi)
files_to_edit=[]
loc=""
path_char=imf.find_path_char(imf.find_os())
loc_temp=file_name.split(path_char)[:-1]
if(loc_temp==[]):
loc=file_name
else:
for i in loc_temp:
loc+=i+path_char
os.chdir(loc)
files_to_edit_temp=os.listdir()
print(f"tymczasowe: {files_to_edit_temp}")
folders=search_files(files_to_edit_temp, loc)
print(f"foldery: {folders}")
look_subfolders(loc, folders)
print(files_to_edit)
return files_to_edit
def edit_found_files(files_to_edit, headers):
def open_file(file_name):
file=open(file_name, 'r', errors='ignore', encoding='utf-8')
content=file.read()
file.close()
return content
end_headers=end_headers_generator(headers)
for i in files_to_edit:
print(f"Otwieram plik: {i}")
file_content=open_file(i)
headers_i=0
for j in headers:
copy_text=""
analyze_header="<"
end_header=""
check_end_header=False
if_analyze_header=False
if_change_text=False
file_content_temp=file_content
for k in file_content:
file_content_temp=file_content_temp[1:]
if k=="<" and if_analyze_header==False:
if_analyze_header=True
copy_text=copy_text+k
continue
if if_analyze_header==True and if_change_text==False:
if k==" " or k==">":
print(f"analizowany nagłówek: {analyze_header}")
if j==analyze_header:
print("zgodny")
if_change_text=True
copy_text=copy_text[0:-len(analyze_header)]+headers_content[headers_i]
continue
else:
if_analyze_header=False
analyze_header="<"
copy_text=copy_text+k
continue
analyze_header+=k
if if_change_text==True:
if k=="<":
end_header+=k
check_end_header=True
continue
if check_end_header==True:
end_header+=k
if k==">" and end_headers[headers_i]==end_header:
copy_text=copy_text+file_content_temp
headers_i=headers_i+1
print(f"zamykam nagłówek: {end_header}")
break
elif k==">" and end_header[headers_i]!=end_header:
end_header=""
check_end_header=False
if if_change_text==False:
copy_text=copy_text+k
file=open(i, 'w', encoding='utf-8', errors='ignore')
file.write(copy_text)
file.close()
file_content=copy_text
def main_analyze_copy(headers, file_name, file, search_subfolder, only_html=True):
#headers is table
print(headers)
file=open(file_name, "r", encoding='utf-8', errors='ignore')
contents=file.read()
analyze_source(headers, file_name, file, contents)
file.close()
files_to_edit=find_files_to_edit(file_name, search_subfolder)
edit_found_files(files_to_edit, headers)
return files_to_edit