forked from NBO2001/Automatizador-de-rotina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
370 lines (253 loc) · 10.3 KB
/
utils.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from seach_box import Seach_Box
from time import sleep
import os
from pathlib import Path
from os import path
from os import mkdir
import json
from os import walk
from os import path
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from sys import platform
def trim(value):
try:
return ((value).lstrip()).rstrip()
except:
return value
def file_is_exists(location):
return Path(location).is_file()
def get_index(table, value):
for n in range(0, len(table)):
if (((table[n].span.get_text()).replace(" ", "")).upper()) == (value.replace(" ", "")).upper() :
return n+1
return 1
def create_json(arq_name,dictionary):
with open(f'{arq_name}.json', 'w', encoding='utf-8') as fp:
json.dump(dictionary, fp, ensure_ascii=False, indent=2)
def reading_json(name):
with open(f'{name}.json', encoding='utf-8') as fp:
data = json.load(fp)
return data
def save_button(instance, validate = False):
if validate:
element_is_clickable(instance,"//div[@class='actions']/div[2]/button")
button_save = instance.driver.find_element_by_xpath("//div[@class='actions']/div[2]/button")
button_save.click()
def options_fil(instance, indx):
dep = f'mat-option[{indx}]'
options_department = instance.driver.find_element_by_xpath("//div[@class='cdk-overlay-pane']/div/div/" + dep)
return options_department
def option_select(instance, value):
return options_fil(instance, seach_index(instance, value))
def seach_index(instance, value):
overlay_pane = instance.driver.find_element_by_xpath("//div[@class='cdk-overlay-pane']/div/div")
return get_index(get_table(overlay_pane, "mat-option"), value)
def add_informations(instance, depart, subj, indx, data_start, data_end, note_v=""):
department = instance.driver.find_element_by_xpath("//mat-card/div/div[2]/mat-form-field[4]/div/div[1]/div/mat-select")
department.click()
option_select(instance, depart).click()
subject = instance.driver.find_element_by_xpath("//mat-card/div/div[3]/mat-form-field/div/div[1]/div/input")
subject.send_keys(subj)
indexing = instance.driver.find_element_by_xpath("//mat-card/div/div[4]/mat-form-field[1]/div/div[1]/div/input")
indexing.send_keys(indx)
start = instance.driver.find_element_by_xpath("//mat-card/div/div[4]/mat-form-field[2]/div/div[1]/div/input")
start.send_keys(data_start)
end = instance.driver.find_element_by_xpath("//mat-card/div/div[4]/mat-form-field[3]/div/div[1]/div/input")
end.send_keys(data_end)
note = instance.driver.find_element_by_xpath("//mat-card/div/div[5]/mat-form-field[1]/div/div[1]/div/input")
note.send_keys(note_v)
def add_box(instance, galpao, prateleira, code, client):
input_galpao = instance.driver.find_element_by_xpath("//mat-card/div/div[1]/mat-form-field[1]/div/div[1]/div/mat-select")
input_galpao.click()
try:
option_select(instance, galpao).click()
except:
sleep(2)
input_galpao.click()
option_select(instance, galpao).click()
input_prateleira= instance.driver.find_element_by_xpath("//mat-card/div/div[1]/mat-form-field[2]/div/div[1]/div/mat-select")
input_prateleira.click()
try:
option_select(instance,prateleira).click()
except:
sleep(2)
input_prateleira.click()
option_select(instance,prateleira).click()
input_cod = instance.driver.find_element_by_xpath("//mat-card/div/div[3]/mat-form-field[1]/div/div[1]/div/input")
input_cod.clear()
input_cod.send_keys(code)
input_client= instance.driver.find_element_by_xpath("//mat-card/div/div[3]/mat-form-field[3]/div/div[1]/div/mat-select")
input_client.click()
try:
option_select(instance, client).click()
except:
input_client.send_keys(Keys.RETURN)
option_select(instance, client).click()
return True
def get_table(table_conteinner, element="mat-row"):
table_html = table_conteinner.get_attribute('outerHTML')
table_bea = BeautifulSoup(table_html, "html.parser")
table_s = table_bea.find_all(element)
return table_s
def get_link(instance):
table_conteinner = instance.driver.find_element_by_xpath("//div[@class='table-container']/mat-table")
rows = get_table(table_conteinner)[0].find_all("mat-cell")
link = rows[0].a['href']
return link
def driving_to_box(instance, code):
instance.driver.get(f'{instance.get_urlBase()}#/modules/pack')
sleep(1)
Seach_Box(instance, code)
sleep(1)
instance.driver.get(f'{instance.get_urlBase()}{get_link(instance)}')
def add_folders(instance, boxs, app=False):
input_code = instance.driver.find_element_by_xpath("//app-pack-itens/mat-card/div/div[1]/mat-form-field/div/div[1]/div/input")
button_code = instance.driver.find_element_by_xpath("//app-pack-itens/mat-card/div/div[1]/div/button")
for box in boxs:
app.situation(f'Envindo a pasta: {box["index"]}') if app else print(f'Envindo a pasta: {box["index"]}')
input_code.send_keys(f'{trim(box["index"])}')
button_code.click()
sleep(1)
input_code.clear()
alert_remove(instance)
def add_values_in_dictionary(table):
datas = list()
dicty = dict()
for tab in table:
rows = tab.find_all("mat-cell")
dicty = {
"index": trim(rows[0].text),
"link": trim(rows[0].a['href'])
}
datas.append(dicty.copy())
return datas
def bea_json(folders, datas):
for folder in folders:
if trim(folders[folder]['type']) == "Caixa":
datas[trim(folder)] = {}
for attr in folders[folder]:
datas[trim(folder)][attr] = folders[folder][attr]
datas[trim(folder)]["boxs"] = {}
else:
datas[trim(folder)[:-2]]['boxs'][trim(folder)] = {}
temp = {}
for attr in folders[folder]:
temp[attr] = folders[folder][attr]
datas[trim(folder)[:-2]]['boxs'][trim(folder)] = temp
return datas
def create_folder(father_code, children={}, location="./"):
if not path.exists(convert_url(f'{location}{father_code}')):
mkdir(convert_url(f'{location}{father_code}'), 0o777)
for kid in children:
if not path.exists(convert_url(f'{location}{father_code}/{kid["index"]}')):
mkdir(convert_url(f'{location}{father_code}/{kid["index"]}',), 0o777)
create_json(convert_url(f'{location}{father_code}/{kid["index"]}/conf'), {
"describe": "",
"subject": ""
})
# Modificar depois
def add_configuration():
fileObj = Path(convert_url("./config/monitoration.json"))
if fileObj.is_file():
configurations = reading_json(convert_url('./config/monitoration'))
configurations['scan'] = os.path.getmtime(convert_url("./scan.xlsx"))
create_json(convert_url('./config/monitoration'), configurations)
else:
configurations = {}
configurations['scan'] = os.path.getmtime(convert_url("./scan.xlsx"))
create_json(convert_url('./config/monitoration'), configurations)
def retun_md(location):
return path.getmtime(location)
def verify_folders(location="./"):
lis = list()
for paths, subpath, files in walk(location):
try:
name = paths.split(separator())[1]
except:
print(f'Error: location {location}, Path: {paths}, Row 254 ')
name = paths.split(separator())[0]
if name != "config" and name != "":
if len(subpath):
lis.append({
"index": name,
"boxs": subpath
})
for li in lis:
li['modification'] = path.getmtime(convert_url(f'./{li["index"]}'))
temp = list()
for box in li['boxs']:
temp.append({
box: path.getmtime(convert_url(f'./{li["index"]}/{box}'))
})
li['boxs'] = temp
return lis
def is_exists_uep(seach):
datas = reading_json(convert_url('./config/boxs'))
for data in datas:
if trim(data['index']) == trim(seach):
return True
return False
def reading_folders(link, list_it):
create_folder(convert_url('./config/folders'))
links = link.split('/')
name = convert_url(f'./config/folders/{links[3]}')
try:
dtas = reading_json(name)
except:
dtas = dict()
finally:
dtas[link] = list_it
create_json(name, dtas)
def convert_url(value):
if platform.upper() == "LINUX":
return value
else:
return "\\".join(value.split("/"))
def separator():
if platform.upper() == "LINUX":
return "/"
else:
return "\\"
def is_load_select(instance):
try:
WebDriverWait(instance.driver, 40).until(
EC.presence_of_element_located((By.XPATH, "//div[@class='cdk-overlay-pane']/div/div"))
)
return True
except:
return False
def element_is_load(instance, location, time_in=20):
try:
WebDriverWait(instance.driver, time_in).until(
EC.presence_of_element_located((By.XPATH, location))
)
return True
except:
return False
def element_is_clickable(instance, location, time_in=20):
try:
WebDriverWait(instance.driver, time_in).until(
EC.element_to_be_clickable((By.XPATH, location))
)
return True
except:
return False
def element_is_visibility(instance, location, time_in=10):
try:
WebDriverWait(instance.driver, time_in).until(
EC.visibility_of_element_located((By.XPATH, location))
)
return True
except:
return False
def alert_remove(instance):
if element_is_visibility(instance, "//simple-notifications/div/simple-notification/div"):
try:
instance.driver.find_element_by_xpath("//simple-notifications/div/simple-notification/div").click()
return True
except:
return False