-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (67 loc) · 2.24 KB
/
main.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
from os import listdir, sep
from os.path import isfile
from methods import add_to_db, retrieve_from_db, write_to_config
import json
from typing import List
## For Linux:
# import pwd
# from os import getuid
## For Linux:
# REPORT_PATH = f'{sep}home{sep}{pwd.getpwuid(getuid())[0]}{sep}Coding{sep}oss-db-api{sep}allureReport{sep}'
## For Windows:
REPORT_PATH = f"D:{sep}Libraries{sep}Arkiralor's Documents{sep}Programs{sep}gits{sep}oss-db-api{sep}allureReport{sep}"
REPORT_PATH = f"allureReport{sep}"
def file_list(directory=REPORT_PATH) -> list:
'''
Returns a list of all files in the specified directory.
'''
files = [f for f in listdir(directory) if isfile(directory+f)]
return files
def lines_from_file(file: str):
'''
Returns a list of all the lines in the file as iterables.
'''
lines = []
with open(file, 'rt') as f:
lines.extend(f.readlines())
return lines
def make_dict(file_name: str) -> dict:
'''
Converts a json file to a dictionary.
'''
with open(file_name) as json_file:
data = json.load(json_file)
name = file_name.split(sep)
name = name[len(name)-1]
data['filename'] = name
return data
def upload_files_to_db():
'''
Main function: Scrapes allure report files and adds them to the database.
Also, if the read file has the extension '.txt', it will be added to the text_config table.
'''
files = file_list()
for file in files:
try:
if file.endswith('.json'):
data_dict = make_dict(REPORT_PATH+file)
add_to_db(data_dict)
elif file.endswith('.txt'):
lines_in_file = lines_from_file(REPORT_PATH+file)
for line in lines_in_file:
write_to_config(line)
except Exception as err:
print(f'Could not upload to database. Err: {err}')
def write_to_files(files: List[dict]):
'''
Write the output to a list of files.
'''
for item in files:
with open(f'output/{item["filename"]}', 'wt') as file:
del item['filename']
json.dump(item, file, indent=4)
if __name__ == "__main__":
# upload_files_to_db()
opt = retrieve_from_db()
write_to_files(opt)
print(opt)