-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
322 lines (271 loc) · 11.7 KB
/
main.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
from PyQt5.QtWidgets import \
QWidget, QPushButton, QGridLayout, \
QLabel, QLineEdit, QComboBox, \
QTableWidget, QTableWidgetItem, QVBoxLayout, \
QHBoxLayout, QFrame, QFileDialog, \
QStackedWidget, QHeaderView
from PyQt5.QtCore import Qt, QTimer
import shutil
from datetime import datetime
import platform
try:
import sys
base_path = sys._MEIPASS
except:
base_path = "."
from pathlib import Path
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setAccessibleName("MainWindow")
with open(base_path / Path("mainwindow.qss"), "r") as windowStyle:
self.setStyleSheet(windowStyle.read())
self.setWindowTitle("RingSaver")
with open(base_path / Path("button.qss"), 'r') as infile:
buttonStyle = infile.read()
buttonStyle = buttonStyle.format(base_path=base_path)
# There has to be a better way....
if platform.system() == "Windows":
buttonStyle = buttonStyle.replace("\\", "/")
print(buttonStyle)
self.buttonSpace = QHBoxLayout(self)
self.bodySpace = QVBoxLayout(self)
self.fileToSave = None
self.backupLocation = None
# Choose file to backup
# backupFileButton = QPushButton("Choose File To Backup", self)
backupFileButton = QPushButton("", self)
backupFileButton.setAccessibleName("BackupFileButton")
backupFileButton.clicked.connect(self.getBackedFile)
backupFileButton.setStyleSheet(buttonStyle)
backupFileText = QLineEdit("")
print(backupFileButton.size())
# Choose backup location
# backupLocButton = QPushButton("Choose Backup Location", self)
backupLocButton = QPushButton("", self)
backupLocButton.clicked.connect(self.getBackedLoc)
backupLocText = QLineEdit("")
backupLocButton.setAccessibleName("BackupLocation")
backupLocButton.setStyleSheet(buttonStyle)
# Backup Timer
self.backupIntervalText = QLineEdit("")
self.backupIntervalUnits = QComboBox()
self.backupIntervalUnits.addItems(
[
"seconds",
"minutes",
"hours",
"days"
]
)
backupIntervalButton = TimerButton(self)
# Log window
self.entries = 0
self.logTable = QTableWidget(0, 2, self)
self.logTable.horizontalHeader().setStretchLastSection(True)
self.logTable.setHorizontalHeaderLabels(['Time', "Message"])
self.logTable.verticalHeader().setVisible(False)
self.logTable.setTextElideMode(Qt.ElideRight)
self.logTable.setWordWrap(True)
self.logTable.setAccessibleName("LogWindow")
with open(base_path / Path("log.qss"), 'r') as logstyle:
self.logTable.setStyleSheet(logstyle.read())
# Options
# optionsButton = QPushButton("Options", self)
optionsButton = QPushButton("", self)
optionsButton.clicked.connect(self.openOptions)
optionsButton.setAccessibleName("OptionsButton")
optionsButton.setStyleSheet(buttonStyle)
# Start Button
# startButton = QPushButton("Start", self)
startButton = QPushButton("", self)
startButton.clicked.connect(self.createTimer)
startButton.setAccessibleName("StartButton")
startButton.setStyleSheet(buttonStyle)
self.cTimer = None
# row, column, height, width r, c, h, w
self.buttonSpace.setSpacing(0)
self.buttonSpace.addWidget(backupFileButton)
self.buttonSpace.addWidget(backupLocButton)
self.buttonSpace.addWidget(backupIntervalButton)
self.buttonSpace.addWidget(optionsButton)
self.buttonSpace.addWidget(startButton)
# self.grid.addWidget()
# self.grid.addWidget()
# self.grid.addWidget()
fakeWidget = QWidget(self)
fakeWidget.setLayout(self.buttonSpace)
self.bodySpace.addWidget(fakeWidget)
self.bodySpace.addWidget(self.logTable)
backupIntervalButton.logTable = self.logTable
self.backupIntervalButton = backupIntervalButton
self.setLayout(self.bodySpace)
def openOptions(self):
pass
def getBackedFile(self):
fileToSave = QFileDialog.getOpenFileName(self)
fileToSave = fileToSave[0]
print(fileToSave)
if len(fileToSave) > 0:
self.fileToSave = fileToSave
self.addLogItem("Selected:\n{}".format(fileToSave))
print("Selected:\n{}".format(fileToSave))
def getBackedLoc(self):
backupLocation = QFileDialog.getExistingDirectory(self)
if len(backupLocation) > 0:
self.backupLocation = backupLocation
self.addLogItem("Backup To:\n{}".format(backupLocation))
print(backupLocation)
def setNewTimer(self):
interval = self.backupIntervalText.text()
if interval.isnumeric():
interval = int(interval)
else:
print("Not a valid integer: {}".format(interval))
return
units = self.backupIntervalUnits.currentText()
print("Backup every {} {}".format(interval, units))
pass
def addLogItem(self, message, insertAt=0):
messageWidget = QTableWidgetItem(message)
messageWidget.setFlags(messageWidget.flags() & ~Qt.ItemIsEditable)
cTime = QTableWidgetItem(self.getCurrentTimeAsString())
cTime.setFlags(cTime.flags() & ~Qt.ItemIsEditable)
self.logTable.insertRow(insertAt)
self.logTable.setItem(insertAt, 0, cTime)
self.logTable.setItem(insertAt, 1, messageWidget)
self.logTable.resizeRowsToContents()
if self.logTable.rowCount() == 1:
# self.logTable.horizontalHeader().ResizeMode(0, QHeaderView.ResizeToContents)
self.logTable.resizeColumnToContents(0)
def backupFile(self):
if self.fileToSave is not None and len(self.fileToSave) > 0 \
and self.backupLocation is not None and len(self.backupLocation) > 0:
cTime = self.getCurrentTimeAsString()
newBackup = "{}/{}.{}.bak".format(self.backupLocation, self.fileToSave.split("/")[-1], cTime)
self.addLogItem("Backing up \n{}\nas\n{}".format(self.fileToSave, newBackup))
import pathlib
firstFile = pathlib.Path(self.fileToSave)
# print(firstFile)
# print(firstFile.is_file())
secondFile = pathlib.Path(newBackup)
# print(secondFile)
# RUN COPY
shutil.copy2(firstFile, secondFile)
def getCurrentTimeAsString(self):
return datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
def createTimer(self):
if self.cTimer is not None:
# Stop timer
self.cTimer.stop()
self.addLogItem("Previous timer stopped.")
# Set to null
self.cTimer = None
# Check that we have a valid time interval for a timer
milliseconds = self.backupIntervalButton.getMilliseconds()
if milliseconds is None:
self.addLogItem("Valid backup interval not specified.")
return
if self.fileToSave is None or len(self.fileToSave) == 0:
self.addLogItem("No valid file to backup has been specified.")
return
if self.backupLocation is None or len(self.backupLocation) == 0:
self.addLogItem("No valid backup location has been specified.")
return
# Create the timer
self.addLogItem("Backup started.")
self.backupFile()
self.cTimer = QTimer()
# Link it to the backup function
self.cTimer.timeout.connect(self.backupFile)
self.cTimer.start(milliseconds)
class TimerButton(QStackedWidget):
def __init__(self, parent=None, log=None):
self.logTable = log
super(TimerButton, self).__init__(parent)
dummyWidget1 = QWidget(self)
self.layoutButton = QVBoxLayout(dummyWidget1)
self.intervalLabel = QLabel("", dummyWidget1)
self.intervalLabel.setAlignment(Qt.AlignCenter)
self.layoutButton.addWidget(self.intervalLabel)
firstButton = QPushButton("Timer")
self.layoutButton.addWidget(firstButton)
self.unitsLabel = QLabel("", dummyWidget1)
self.unitsLabel.setAlignment(Qt.AlignCenter)
self.layoutButton.addWidget(self.unitsLabel)
dummyWidget1.setLayout(self.layoutButton)
dummyWidget2 = QWidget(self)
self.layoutTimer = QVBoxLayout(dummyWidget2)
self.units = QComboBox()
self.units.addItems([
"seconds",
"minutes",
"hours",
"days"
])
self.intervalText = QLineEdit("", self)
acceptButton = QPushButton("Accept", self)
self.layoutTimer.addWidget(self.intervalText)
self.layoutTimer.addWidget(self.units)
self.layoutTimer.addWidget(acceptButton)
dummyWidget2.setLayout(self.layoutTimer)
self.addWidget(dummyWidget1)
self.addWidget(dummyWidget2)
firstButton.clicked.connect(self.switchLayout)
acceptButton.clicked.connect(self.switchLayout)
self.firstLayout = True
print(self.widget(0))
print(self.widget(1))
self.widget(0).show()
def switchLayout(self):
if self.firstLayout:
self.setCurrentWidget(self.widget(1))
else:
tyme = self.intervalText.text()
units = self.units.currentText()
self.intervalLabel.setText(tyme)
self.unitsLabel.setText(units)
self.addLogItem("Backing up every:\n{} {}".format(tyme, units))
self.setCurrentWidget(self.widget(0))
self.firstLayout = not self.firstLayout
def getUnits(self):
return self.units.currentText()
def getInterval(self):
return self.intervalText.text()
def addLogItem(self, message, insertAt=0):
if self.logTable is not None:
messageWidget = QTableWidgetItem(message)
messageWidget.setFlags(messageWidget.flags() & ~Qt.ItemIsEditable)
cTime = QTableWidgetItem(self.getCurrentTimeAsString())
cTime.setFlags(cTime.flags() & ~Qt.ItemIsEditable)
self.logTable.insertRow(insertAt)
self.logTable.setItem(insertAt, 0, cTime)
self.logTable.setItem(insertAt, 1, messageWidget)
self.logTable.resizeRowsToContents()
if self.logTable.rowCount() == 1:
# self.logTable.horizontalHeader().ResizeMode(0, QHeaderView.ResizeToContents)
self.logTable.resizeColumnToContents(0)
def getMilliseconds(self):
units = self.getUnits()
tyme = self.getInterval()
if len(tyme) < 0 or not tyme.isnumeric():
return None
conversion = {
'seconds': 1000,
"minutes": 1000*60,
"hours": 1000*60*60,
"days": 1000*60*60*24
}
tyme = int(tyme)
if tyme <= 0:
return None
return tyme * conversion[units]
def getCurrentTimeAsString(self):
return datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
if __name__ == "__main__":
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())