Skip to content

Commit f84ed93

Browse files
committed
Added ability to read and write options from a file.
1 parent af89d00 commit f84ed93

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

TardisDiff.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect
44
from PyQt5 import QtWidgets, QtCore, QtGui
55
from uptime import boottime
6+
from TardisUtil import TardisOptions
67

78

89
class TardisDiff(QtWidgets.QMainWindow):
@@ -11,9 +12,10 @@ def __init__(self):
1112
super(TardisDiff, self).__init__()
1213
self.diff = 0
1314
self.clipboard = QtWidgets.QApplication.clipboard()
14-
# Set hotkeys
15+
# Set hot keys
1516
QtWidgets.QShortcut(QtGui.QKeySequence("Ctrl+Shift+C"), self,
1617
self.setClipboard)
18+
self.options = TardisOptions()
1719

1820
# Get directory path
1921
# From: http://stackoverflow.com/questions/3718657/how-to-properly-determine-current-script-directory-in-python/22881871#22881871
@@ -30,7 +32,7 @@ def __init__(self):
3032
self.initUI()
3133

3234
def initUI(self):
33-
#Create and initialize UI elements
35+
# Create and initialize UI elements
3436
self.contentWidget = QtWidgets.QWidget()
3537
self.gridLayout = QtWidgets.QGridLayout(self.contentWidget)
3638
self.formLayout = QtWidgets.QFormLayout()
@@ -48,7 +50,7 @@ def initUI(self):
4850
self.label_breakTime.setText("Break Time:")
4951
self.label_timeDiff.setText("Difference")
5052
self.label_timeDiffOut.setText("")
51-
self.timeEdit1.setTime(self.getBootTimeAsQTime())
53+
self.timeEdit1.setTime(self.getStartTime())
5254
self.timeEdit2.setTime(QtCore.QTime.currentTime())
5355

5456
#Set relations
@@ -100,24 +102,17 @@ def setClipboard(self):
100102
self.clipboard.setText(str(self.diff))
101103
self.statusBar().showMessage("Copied to clipboard.")
102104

103-
def getBootTimeAsQTime(self):
105+
def getStartTime(self):
106+
return TardisDiff.getBootTimeAsQTime()\
107+
if self.options.isStartTimeAuto()\
108+
else QtCore.QTime.fromString(self.options.getStartTime())
109+
110+
@staticmethod
111+
def getBootTimeAsQTime():
104112
return QtCore.QDateTime(boottime()).time()
105113

106114

107115
def main():
108-
'''
109-
myappid = 'net.xerael.tardisdiff'
110-
if sys.platform == "win32":
111-
"""
112-
This is for collapsing on the task bar on windows 7/8 and using
113-
the application icon on the task bar instead of the python icon
114-
if running using a python executable.
115-
116-
See:
117-
http://stackoverflow.com/questions/1551605/how-to-set-applications-taskbar-icon-in-windows-7
118-
"""
119-
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
120-
'''
121116
app = QtWidgets.QApplication(sys.argv)
122117
ed = TardisDiff()
123118
sys.exit(app.exec_())

TardisUtil.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import configparser
2+
import os
3+
4+
class TardisOptions:
5+
6+
"""
7+
Class for handling options.
8+
Has a getter and setter for every property and saves changes automatically.
9+
10+
Current keys are:
11+
- start_time
12+
"""
13+
14+
def __init__(self, optionFileName=".tardisrc"):
15+
self.config = TardisOptions.generateDefaultConfig()
16+
17+
#Make path
18+
home_dir = os.path.expanduser("~")
19+
self._optionFilePath = os.path.join(home_dir, optionFileName)
20+
#Try to load config file
21+
filesLoaded = self.config.read(self._optionFilePath)
22+
if len(filesLoaded) == 0: # No option file found
23+
self._saveOptionFile()
24+
self.options = self.config['TardisDiff']
25+
26+
def isStartTimeAuto(self):
27+
return self._getOption('start_time') == 'auto'
28+
29+
def getStartTime(self):
30+
return self._getOption('start_time')
31+
32+
def setStartTime(self, start_time):
33+
self._setOption('start_time', start_time)
34+
35+
def _setOption(self, option_name, option_value):
36+
self.options[option_name] = option_value
37+
self._saveOptionFile()
38+
39+
def _getOption(self, option_name):
40+
return self.options[option_name]
41+
42+
def _saveOptionFile(self):
43+
with open(self._optionFilePath, 'w') as configFile:
44+
self.config.write(configFile)
45+
46+
@staticmethod
47+
def generateDefaultConfig():
48+
config = configparser.ConfigParser()
49+
config['TardisDiff'] = {'start_time': 'auto',
50+
51+
}
52+
return config

0 commit comments

Comments
 (0)