-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
150 lines (109 loc) · 4.55 KB
/
main.py
File metadata and controls
150 lines (109 loc) · 4.55 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QTableWidgetItem
import sys
import json
import MainWindow
import passgen
import options
import crypt
class MainWidget(QtWidgets.QMainWindow, MainWindow.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.tableWidget.setColumnWidth(2, 190)
self.tableWidget.setColumnCount(3)
self.copy_button.setEnabled(False)
self.add_to_list_button.setEnabled(False)
self.remove_from_list_button.setEnabled(False)
self.copy_button.clicked.connect(self.copy_on_click)
self.generate_button.clicked.connect(self.set_password)
self.add_to_list_button.clicked.connect(self.pwd_dict)
self.remove_from_list_button.clicked.connect(self.remove_from_dict)
self.tableWidget.cellChanged.connect(self.add_to_dict)
self.tableWidget.cellClicked.connect(self.pwd_show)
self.tableData = []
self.load_data()
def copy_on_click(self):
QtGui.QGuiApplication.clipboard().setText(self.pwd_out.text())
def load_data(self):
try:
data = crypt.decrypt_()
except FileNotFoundError:
print("File not found")
return
try:
self.tableData = json.loads(data)
except ValueError:
print("File is corrupted")
return
self.refresh_ui()
def refresh_ui(self):
if self.tableData:
self.remove_from_list_button.setEnabled(True)
else:
self.remove_from_list_button.setEnabled(False)
self.tableWidget.cellChanged.disconnect(self.add_to_dict)
self.tableWidget.setRowCount(len(self.tableData))
self.tableWidget.clearContents()
for idx, dict_ in enumerate(self.tableData):
for type_, data_ in dict_.items():
self.tableWidget.setItem(idx, int(type_), QTableWidgetItem(data_))
self.tableWidget.cellChanged.connect(self.add_to_dict)
def pwd_dict(self):
data = self.pwd_out.text()
dict_pwd = {"2": data}
self.tableData.append(dict_pwd)
self.write_to_json()
print(self.tableData)
def add_to_dict(self):
current_row = self.tableWidget.currentRow()
current_column = self.tableWidget.currentColumn()
self.tableData[current_row][str(current_column)] = self.tableWidget.currentItem().text()
self.write_to_json()
print(self.tableData)
def remove_from_dict(self):
current_row = self.tableWidget.currentRow()
del self.tableData[current_row]
self.write_to_json()
print(self.tableData)
def write_to_json(self):
json_ = json.dumps(self.tableData)
crypt.encrypt_(json_)
self.refresh_ui()
def get_character(self):
chars = ''
for checkbox_ in options.Characters:
if getattr(self, f"{checkbox_.name}Check").isChecked():
chars += checkbox_.value
return chars
def set_password(self):
if self.length_box.value() <= 3:
self.pwd_out.setText("Length must be greater than 3")
self.copy_button.setEnabled(False)
self.add_to_list_button.setEnabled(False)
return
if not self.get_character():
self.pwd_out.setText("Choose at least one option")
self.copy_button.setEnabled(False)
self.add_to_list_button.setEnabled(False)
return
generated_pwd = passgen.generate_password(
pwd_length=self.length_box.value(),
character=self.get_character())
self.pwd_out.setText(generated_pwd)
self.copy_button.setEnabled(True)
self.add_to_list_button.setEnabled(True)
def pwd_show(self):
current_row = self.tableWidget.currentRow()
self.pwd_out.setText(self.tableData[current_row]["2"])
self.add_to_list_button.setEnabled(True)
self.copy_button.setEnabled(True)
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
QApplication.clipboard().clear()
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) # enable highdpi scaling
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True) # use highdpi icons
if __name__ == "__main__":
app = QApplication([])
main_widget = MainWidget()
main_widget.show()
sys.exit(app.exec())