-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemisAudits.py
executable file
·478 lines (431 loc) · 16.8 KB
/
ThemisAudits.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# Project Name: ThemisHIM™ Audits
#
# Version: 1.2.7
#
# Description: ThemisHIMAudits is a Python-based tool designed for automated auditing and analysis of legal and administrative documents, particularly focusing on public procurement processes. This intelligent system processes multiple file formats, extracts relevant information, and generates structured data for easy analysis.
#
# License: CC0 1.0 Universal, CC-BY-4.0 Licence, and Apache-2.0.
#
# Author: David C Cavalcante
# Email: davcavalcante@proton.me
# LinkedIn: https://www.linkedin.com/in/hellodav/
# X: https://twitter.com/takk8is/
# Medium: https://takk8is.medium.com/
# Website: https://takk.ag/
#
# Designed by Takk™ Innovate Studio
# Takk™ Innovate Studio is at the forefront of the digital revolution, leading with a 100% Artificial Intelligence team.
#
# Research by TeleologyHI™
# Semiotics Research and Development of Hybrid Intelligence that adapts and evolves with humanity.
#
# Donations: If this project has been helpful to you, please consider making a donation to support our ongoing development of innovative tools. USDT (TRC-20) `TGpiWetnYK2VQpxNGPR27D9vfM6Mei5vNA`
import os
import sys
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from PIL import Image, ImageTk
import requests
from io import BytesIO
import pandas as pd
import pdfplumber
from docx import Document
import markdown
import pytesseract
from pdf2image import convert_from_path
import threading
import time
import logging
from pathlib import Path
import re
import csv
import tempfile
import ttkbootstrap as ttk
from typing import List, Dict, Any, Callable, Optional
log_file = os.path.join(tempfile.gettempdir(), "ThemisAudits.log")
logging.basicConfig(
filename=log_file,
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
)
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
bundle_dir = sys._MEIPASS
else:
bundle_dir = os.path.dirname(os.path.abspath(__file__))
class ThemisHIMAudits:
def __init__(self, master: ttk.Window):
self.master = master
master.title("ThemisHIM™ Audits")
master.geometry("414x736")
self.style = ttk.Style(theme="flatly")
self._configure_styles()
self.file_path: str = ""
self.processed_data: List[List[str]] = []
self.processing_thread: Optional[threading.Thread] = None
self.create_widgets()
def _configure_styles(self):
self.style.configure("TFrame", background="#f0f0f0")
self.style.configure(
"TLabel", background="#f0f0f0", foreground="#4a4a4a", font=("Inter", 12)
)
self.style.configure("TButton", font=("Inter", 14), padding=12, cursor="hand2")
self.style.map(
"TButton",
relief=[("pressed", "flat"), ("!pressed", "flat")],
background=[("active", "#e0e0e0"), ("!active", "#d9d9d9")],
foreground=[("active", "#333333"), ("!active", "#4a4a4a")],
)
self.style.configure(
"Accent.TButton", background="#bf7245", foreground="#ffffff"
)
self.style.map(
"Accent.TButton",
background=[("active", "#cc7949"), ("disabled", "#d9d9d9")],
foreground=[("disabled", "#a0a0a0")],
)
self.style.configure(
"Enabled.TButton", background="#bf7245", foreground="#ffffff"
)
self.style.map(
"Enabled.TButton",
background=[("active", "#cc7949"), ("!active", "#bf7245")],
foreground=[("active", "#ffffff"), ("!active", "#ffffff")],
)
self.style.configure(
"TProgressbar", background="#bf7245", troughcolor="#f0f0f0"
)
def create_widgets(self):
self.main_frame = ttk.Frame(self.master, padding="30")
self.main_frame.pack(expand=True, fill="both")
self._create_header()
self._create_buttons()
self._create_progress_bar()
self._create_footer()
def _create_header(self):
self.load_image()
self.version_label = ttk.Label(
self.main_frame,
text="Version 1.2.7",
font=("Inter", 14),
foreground="#bf7245",
)
self.version_label.pack(pady=(10, 0))
self.title_label = ttk.Label(
self.main_frame, text="ThemisHIM™ Audits", font=("Inter", 32, "bold")
)
self.title_label.pack(pady=10)
self.status_label = ttk.Label(
self.main_frame, text="Ready for audit", font=("Inter", 16)
)
self.status_label.pack(pady=10)
def _create_buttons(self):
self.button_frame = ttk.Frame(self.main_frame)
self.button_frame.pack(pady=10)
self.audit_button = ttk.Button(
self.button_frame,
text="Perform Audit",
command=self.select_file,
style="Accent.TButton",
width=25,
)
self.audit_button.pack(fill="x", pady=8)
self.report_button = ttk.Button(
self.button_frame,
text="Download Report",
command=self.generate_csv,
state="disabled",
style="TButton",
width=25,
)
self.report_button.pack(fill="x", pady=8)
def _create_progress_bar(self):
self.progress_var = tk.DoubleVar()
self.progress_bar = ttk.Progressbar(
self.main_frame,
variable=self.progress_var,
maximum=100,
length=300,
style="TProgressbar",
)
self.progress_bar.pack(fill="x", pady=10)
self.progress_bar.pack_forget()
def _create_footer(self):
self.signature_label = ttk.Label(
self.main_frame,
text="Developed by",
font=("Inter", 12),
foreground="#bf7245",
)
self.signature_label.pack(pady=(30, 0))
self.signature_link = ttk.Label(
self.main_frame,
text="Takk™ Innovate Studio",
font=("Inter", 12, "underline"),
foreground="#bf7245",
cursor="hand2",
)
self.signature_link.pack()
self.signature_link.bind(
"<Button-1>",
lambda e: self.open_url("https://www.linkedin.com/in/hellodav/"),
)
def load_image(self):
try:
image_path = os.path.join(bundle_dir, "images", "icon512.png")
if os.path.exists(image_path):
img = Image.open(image_path).convert("RGBA")
else:
image_url = "https://raw.githubusercontent.com/Takk8IS/ThemisHIMAudits/main/images/icon512.png"
response = requests.get(image_url)
img = Image.open(BytesIO(response.content)).convert("RGBA")
img = img.resize((250, 250), Image.LANCZOS)
photo = ImageTk.PhotoImage(img)
self.image_label = ttk.Label(
self.main_frame, image=photo, background="#f0f0f0"
)
self.image_label.image = photo
self.image_label.pack(pady=10)
except Exception as e:
logging.error(f"Error loading image: {e}")
self.image_label = ttk.Label(
self.main_frame,
text="ThemisHIM™",
font=("Inter", 24, "bold"),
background="#f0f0f0",
)
self.image_label.pack(pady=10)
def select_file(self):
filetypes = [
(
"All supported files",
"*.pdf *.md *.rtf *.doc *.docx *.png *.jpg *.jpeg *.tiff",
),
("PDF files", "*.pdf"),
("Markdown files", "*.md"),
("RTF files", "*.rtf"),
("Word files", "*.doc *.docx"),
("Image files", "*.png *.jpg *.jpeg *.tiff"),
]
file_path = filedialog.askopenfilename(filetypes=filetypes)
if file_path:
self.file_path = file_path
self.status_label.config(text="Processing...")
self.progress_var.set(0)
self.progress_bar.pack(fill="x", pady=10)
self.audit_button.config(state="disabled")
self.report_button.config(state="disabled")
self.processing_thread = threading.Thread(
target=self.process_file, daemon=True
)
self.processing_thread.start()
self.master.after(100, self.check_processing)
def check_processing(self):
if self.processing_thread and self.processing_thread.is_alive():
self.master.after(100, self.check_processing)
else:
self.finalize_processing()
def process_file(self):
try:
extension = Path(self.file_path).suffix.lower()
processor = self.get_processor(extension)
self.processed_data = processor(self.file_path)
except Exception as e:
logging.error(f"Error processing file: {e}")
self.processed_data = []
def finalize_processing(self):
if self.processed_data:
self.status_label.config(text="Audit completed")
self.report_button.config(state="normal", style="Enabled.TButton")
else:
self.status_label.config(text="No data found or error occurred")
self.progress_var.set(100)
self.audit_button.config(state="normal")
self.master.after(3000, self.hide_progress_bar)
def get_processor(self, extension: str) -> Callable[[str], List[List[str]]]:
processors = {
".pdf": self.process_pdf,
".doc": self.process_docx,
".docx": self.process_docx,
".rtf": self.process_rtf,
".md": self.process_md,
".png": self.process_image,
".jpg": self.process_image,
".jpeg": self.process_image,
".tiff": self.process_image,
}
return processors.get(extension, lambda _: [])
def process_pdf(self, file_path: str) -> List[List[str]]:
all_tables = []
try:
with pdfplumber.open(file_path) as pdf:
total_pages = len(pdf.pages)
for i, page in enumerate(pdf.pages):
tables = page.extract_tables()
for table in tables:
cleaned_table = self.clean_table(table)
if cleaned_table:
all_tables.extend(cleaned_table)
self.update_progress((i + 1) / total_pages * 100)
except Exception as e:
logging.error(f"Error processing PDF: {e}")
return all_tables
def process_docx(self, file_path: str) -> List[List[str]]:
all_tables = []
try:
doc = Document(file_path)
total_tables = len(doc.tables)
for i, table in enumerate(doc.tables):
data = [[cell.text for cell in row.cells] for row in table.rows]
cleaned_table = self.clean_table(data)
if cleaned_table:
all_tables.extend(cleaned_table)
self.update_progress((i + 1) / total_tables * 100)
except Exception as e:
logging.error(f"Error processing DOCX: {e}")
return all_tables
def process_rtf(self, file_path: str) -> List[List[str]]:
all_tables = []
try:
with open(file_path, "r", encoding="utf-8", errors="ignore") as file:
content = file.read()
tables = self.extract_tables_from_text(content)
for table in tables:
cleaned_table = self.clean_table(table)
if cleaned_table:
all_tables.extend(cleaned_table)
except Exception as e:
logging.error(f"Error processing RTF: {e}")
return all_tables
def process_md(self, file_path: str) -> List[List[str]]:
all_tables = []
try:
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
html = markdown.markdown(content)
tables = self.extract_tables_from_html(html)
for table in tables:
cleaned_table = self.clean_table(table)
if cleaned_table:
all_tables.extend(cleaned_table)
except Exception as e:
logging.error(f"Error processing Markdown: {e}")
return all_tables
def process_image(self, file_path: str) -> List[List[str]]:
all_tables = []
try:
text = pytesseract.image_to_string(Image.open(file_path))
tables = self.extract_tables_from_text(text)
for table in tables:
cleaned_table = self.clean_table(table)
if cleaned_table:
all_tables.extend(cleaned_table)
except Exception as e:
logging.error(f"Error processing Image: {e}")
return all_tables
def extract_tables_from_text(self, text: str) -> List[List[str]]:
lines = text.split("\n")
tables = []
current_table = []
for i, line in enumerate(lines):
if "|" in line:
current_table.append(line.split("|"))
elif current_table:
tables.append(current_table)
current_table = []
self.update_progress((i + 1) / len(lines) * 100)
if current_table:
tables.append(current_table)
return tables
def extract_tables_from_html(self, html: str) -> List[List[str]]:
tables = []
pattern = r"<table.*?>(.*?)</table>"
table_matches = re.findall(pattern, html, re.DOTALL)
for i, table_html in enumerate(table_matches):
rows = re.findall(r"<tr.*?>(.*?)</tr>", table_html, re.DOTALL)
table = []
for row in rows:
cells = re.findall(r"<t[dh].*?>(.*?)</t[dh]>", row, re.DOTALL)
table.append([cell.strip() for cell in cells])
tables.append(table)
self.update_progress((i + 1) / len(table_matches) * 100)
return tables
def clean_table(self, table: List[List[Any]]) -> List[List[str]]:
cleaned_table = []
for row in table:
cleaned_row = [self.clean_cell(cell) for cell in row]
if any(cleaned_row):
cleaned_table.append(cleaned_row)
return cleaned_table
@staticmethod
def clean_cell(cell: Any) -> str:
if cell is None:
return ""
return re.sub(r"\s+", " ", str(cell)).strip()
def generate_csv(self):
if self.processed_data:
output_file = filedialog.asksaveasfilename(
defaultextension=".csv", filetypes=[("CSV files", "*.csv")]
)
if output_file:
self.report_button.config(state="disabled")
threading.Thread(
target=self.save_to_csv,
args=(self.processed_data, output_file),
daemon=True,
).start()
def save_to_csv(self, data: List[List[str]], output_file: str):
try:
with open(output_file, "w", newline="", encoding="utf-8-sig") as file:
writer = csv.writer(file)
for row in data:
writer.writerow(row)
self.master.after(
0,
lambda: self.status_label.config(
text=f"Report saved: {Path(output_file).name}"
),
)
except Exception as e:
logging.error(f"Error saving CSV: {e}")
self.master.after(
0, lambda: self.status_label.config(text="Error saving report")
)
finally:
self.master.after(
0,
lambda: self.report_button.config(
state="normal", style="Enabled.TButton"
),
)
def update_progress(self, value: float):
self.master.after(0, lambda: self.progress_var.set(value))
def hide_progress_bar(self):
self.progress_bar.pack_forget()
self.progress_var.set(0)
@staticmethod
def open_url(url: str):
import webbrowser
webbrowser.open_new(url)
def show_error(error_message):
root = tk.Tk()
root.withdraw()
messagebox.showerror("Error", error_message)
root.destroy()
def main():
try:
root = ttk.Window(themename="flatly")
time.sleep(1)
app = ThemisHIMAudits(root)
root.mainloop()
except Exception as e:
error_message = f"An error occurred: {str(e)}\n\nPlease contact support."
logging.exception("Unhandled exception:")
show_error(error_message)
if __name__ == "__main__":
try:
main()
except Exception as e:
logging.exception("Fatal error in main loop")
show_error(f"A fatal error occurred: {str(e)}\nThe application will now close.")
finally:
logging.info("Application closing")