-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsam_records_json_reports.py
58 lines (51 loc) · 2.12 KB
/
sam_records_json_reports.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
import json
import sqlite3
import subprocess
from dotenv import load_dotenv
from os import getenv
load_dotenv()
db = getenv('db_filepath')
folder = getenv('folder')
def create_connection(db_file):
connect = None
try:
connect = sqlite3.connect(db_file)
print("connect success")
return connect
except sqlite3.Error as e:
print("connect failure", e)
finally:
return connect
def all_vendors_report():
table = create_connection(db).cursor().execute("SELECT * FROM Vendors")
table_list_dict = [{"id": f"{row[0]}",
"Vendor": f"{row[1]}",
"Serial Number": f"{row[2]}",
"Product Key": f"{row[3]}"}
for row in table]
table_reformatted = {}
for row in table_list_dict:
table_reformatted[row["Vendor"]] = {}
for row in table_list_dict:
table_reformatted[row["Vendor"]][row["id"]] = {'Serial Number': row['Serial Number'], 'Product Key':
row['Product Key']}
json_dict = {"Software Vendors": table_reformatted}
av_json = open(f"{folder}sam_vendors.json", "w", encoding="utf-8")
json.dump(json_dict, av_json, ensure_ascii=False, indent=4, separators=(',', ': '))
av_json.close()
subprocess.Popen([f"{folder}sam_vendors.json"], shell=True)
def one_vendor_report(vendor):
table = create_connection(db).cursor().execute("SELECT * FROM Vendors WHERE Vendor = ?;", (vendor,))
table_list_dict = [{"id": f"{row[0]}",
"Serial Number": f"{row[2]}",
"Product Key": f"{row[3]}"}
for row in table]
table_reformatted = {str(vendor): {}}
for row in table_list_dict:
table_reformatted[str(vendor)][row["id"]] = {'Serial Number': row['Serial Number'], 'Product Key':
row['Product Key']}
json_dict = table_reformatted
sv_json = open(f"{folder}{vendor}.json", "w", encoding="utf-8")
json.dump(json_dict, sv_json, ensure_ascii=False, indent=4, separators=(',', ': '))
sv_json.close()
subprocess.Popen([f"{folder}{vendor}.json"], shell=True)