Repository navigation
Expand file tree
/
Copy pathWTWController.py
More file actions
127 lines (104 loc) · 4.54 KB
/
Copy pathWTWController.py
File metadata and controls
127 lines (104 loc) · 4.54 KB
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
from WindowsTrackerWindow import WTWinodw
from LoggingCore import TrackingCore, LoggingTextBox
from PyQt5.QtWidgets import QMessageBox, QFileDialog
import os, logging, threading, ConfigJson
# Customize the QMessageBox with inheritance.
class MsgBox(QMessageBox):
def __init__(self):
super(MsgBox,self).__init__()
self.setIcon(QMessageBox.Critical)
def empty(self,target):
self.setWindowTitle(f"{target} is empty !")
self.setText(f"Make sure that the {target} field is not empty .")
self.show()
def pathError(self):
self.setWindowTitle("Path error")
self.setText("The path does not exist !")
self.show()
def setMessage(self,title,text):
self.setText(text)
self.setWindowTitle(title)
self.show()
# Main class
class WTW_Controller(WTWinodw):
def __init__(self,parent=None):
super(WTW_Controller,self).__init__(parent=parent)
self.fileFilters=[]
# Creating TrackingCore object (main logging core)
self.logCore=TrackingCore()
self.messageBox=MsgBox()
# Connecting click event of buttons to their functions
self.btn_start.clicked.connect(self.btnStartClicked)
self.btn_stop.clicked.connect(self.btnStopClicked)
self.btn_clear.clicked.connect(self.btnClearClicked)
self.btn_path.clicked.connect(self.btnPathClicked)
self.btn_submit.clicked.connect(self.btnSubmitClicked)
self.btn_save.clicked.connect(self.btnSaveClicked)
self.btn_json.clicked.connect(self.btnJsonClicked)
# Connecting the log message to QPlainTextEdit
self.handler=LoggingTextBox(self)
logging.getLogger().addHandler(self.handler)
self.handler.new_record.connect(self.ptxt_log.appendPlainText)
def btnStartClicked(self):
# Path validation :
# If return value is 0: path filed is empty
# 1: Path is correct and exists
# 2: doesn't exist
_pathValied=self.pathValidation(self.txt_path.text())
# If observer has not started :
if not self.logCore.observer.is_alive():
if _pathValied == 0:
self.messageBox.empty("PATH")
elif _pathValied == 1:
isfile=False
self.logCore.setPath(self.txt_path.text())
# If config.json file exists, sets the filter
if os.path.isfile('config.json'):
isfile=True
data=ConfigJson.getJsonData()['filters']
data=','.join(data)
self.txt_filter.setText(data)
self.logCore.start(isfile,self.txt_path.text(),data)
else:
self.logCore.start()
else:
self.messageBox.pathError()
else:
if _pathValied == 0 :
self.messageBox.empty("PATH")
elif _pathValied == 1:
self.logCore.setPath(self.txt_path.text())
else:
self.messageBox.pathError()
def btnStopClicked(self):
self.logCore.observer.unschedule_all()
def btnClearClicked(self):
self.ptxt_log.setPlainText("")
def btnPathClicked(self):
# Browse dialoge
directory=str(QFileDialog.getExistingDirectory(self,"Select Directory"))
if directory != "":
self.txt_path.setText(directory)
def btnSubmitClicked(self):
if self.logCore.observer.is_alive():
self.fileFilters=self.txt_filter.text().split(',')
self.logCore.setFilter(self.fileFilters,self.txt_path.text())
else:
self.messageBox.setMessage("Logger has not started !","""Please first set the path\nand after that set the filter""")
def btnSaveClicked(self):
# Write the filters to config.json file
if self.fileFilters:
t=threading.Thread(target=ConfigJson.createJsonFile,args=(self.fileFilters,))
t.start()
else:
self.messageBox.setMessage("FilterEmptyError","No filters yet!")
def btnJsonClicked(self):
if os.path.isfile("config.json"):
os.system("notepad config.json")
def pathValidation(self,path):
if path == "" : return 0
elif os.path.isdir(path) : return 1
else : return 2
def PlainTextEditer(self,msg):
# Print the log into ptxt
self.ptxt_log.appendPlainText(msg)