-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_window.py
More file actions
104 lines (87 loc) · 3.62 KB
/
add_window.py
File metadata and controls
104 lines (87 loc) · 3.62 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
import os
import os.path
import dropbox
from cryptography.fernet import Fernet
from PyQt5.Qt import QEvent, Qt
from PyQt5.QtWidgets import (QDesktopWidget, QPushButton, QTextEdit,
QVBoxLayout, QWidget)
import access_token
import config
class AddWindow(QWidget):
# Need to create the window for the user the add to the file
def __init__(self):
super().__init__()
self.appendBox()
self.initUI() # Initialise the UI
# Reads the key and generates the Fernet
self.f = Fernet(open("key.key", "rb").read())
def initUI(self):
self.setWindowTitle('Append...')
self.setGeometry(0, 0, 500, 500)
self.off_center() # Sets the window slightly off from the main window
def off_center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center() * 1.1
qr.moveCenter(cp)
self.move(qr.topLeft())
def encrypt(self):
# Encrypt the file after changes have been made
with open(config.file_destination, "rb") as log:
log_data = log.read()
encrypted_log = self.f.encrypt(log_data)
with open(config.file_destination, "wb") as log:
log.write(encrypted_log)
def decrypt(self):
# Decrypt the file so changes can be written
if os.path.isfile(config.file_destination):
with open(config.file_destination, "rb") as log:
encrypted_data = log.read()
if len(encrypted_data) > 0:
decrypted_data = self.f.decrypt(encrypted_data)
with open(config.file_destination, "wb") as log:
log.write(decrypted_data)
def appendBox(self):
layout = QVBoxLayout()
self.appendData = QTextEdit(self)
layout.addWidget(self.appendData)
self.appendData.installEventFilter(self)
addbtn = QPushButton('Add to Log')
addbtn.clicked.connect(lambda: self.writeToFile())
addbtn.setAutoDefault(True)
layout.addWidget(addbtn)
self.setLayout(layout)
def writeToFile(self):
import time
date = time.strftime('%d/%m/%Y')
time = time.strftime('%H:%M:%S')
self.decrypt()
with open(config.file_destination, 'a') as myfile:
myfile.write(
f"[ {date} - {time} ] - {self.appendData.toPlainText()}\n")
self.encrypt()
self.upload()
self.close()
def upload(self):
try:
# Check if the access token is valid and if it is then upload log.txt to Dropbox
dbx = dropbox.Dropbox(access_token.access_token)
with open(config.file_destination, "rb") as logtxt:
log = logtxt.read()
dbx.files_upload(
log, "/log.txt", mode=dropbox.files.WriteMode.overwrite)
except:
print("Access token error! Run setup_upload.py to upload to Dropbox")
# Listens for the user pressing the return/enter key.
# When return is pressed it executes the writeToFile function
# This is the same as pressing the "Add to Log" button
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress and obj is self.appendData:
if event.key() == Qt.Key_Return and self.appendData.hasFocus():
self.writeToFile()
# elif event.key() == Qt.Key_Escape and self.appendData.hasFocus():
# self.close()
return super().eventFilter(obj, event)
# Closes the window if the user presses the ESC key
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()