-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities.py
More file actions
118 lines (99 loc) · 4.27 KB
/
utilities.py
File metadata and controls
118 lines (99 loc) · 4.27 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
"""
A few assistant classes
Author: Justin Duan
Date: 2018-05-22
"""
import time
import constants
import datetime
import os
import re
from pynput.keyboard import Listener
_logFileName = constants.FILE_IO.logFileName
class KeyboardListener(object):
"""
A keyboard event listener. The keyboard event is used to control the
program behavior such as stop, pause, toggle save
"""
def __init__(self, plotterObj=None, saveFlag=True):
self.stopFlag = False
self.pauseFlag= False
self.saveFlag = saveFlag
self._plotter = plotterObj
self._keyboardListener = Listener(on_release=self._onRelease)
self._keyboardListener.daemon = True
self._keyboardListener.start()
print("At any time, press {} to stop, {} to toggle save, {} for pause".format(
constants.FLOW_CTRL.stopKey.name.capitalize(),
constants.FLOW_CTRL.toggleSaveKey.name.capitalize(),
constants.FLOW_CTRL.togglePauseKey.name.capitalize()))
def _onRelease(self, key):
"""
Keyboard release event callback function
"""
if key == constants.FLOW_CTRL.togglePauseKey:
self.pauseFlag = not self.pauseFlag
if self.pauseFlag:
print("Program paused!")
else:
print("Program resumed!")
elif key == constants.FLOW_CTRL.stopKey:
print("Stop requested ...")
if self._plotter is not None:
self._plotter.addPoints(self._plotter.STOP_FLAG)
self.stopFlag = True
return False
elif key == constants.FLOW_CTRL.toggleSaveKey:
self.saveFlag = not self.saveFlag
if self._plotter is not None:
self._plotter.addPoints(self._plotter.TOGGLE_SAVE)
print("Save flag is toggled to: {}".format(self.saveFlag))
class FileIO(object):
"""
A class that deals with file name, file IOs, and a variety of other trivial
stuff
"""
def __init__(self, fileName, folderName, basePath):
self.markStartTime()
self.runID = self.generateRunID(self.startTimeObj)
self.fileName = fileName
self.fileName = self.runID + '_' + self.fileName
self.folderName = folderName
self.basePath = basePath
# Prepare the data folder and file pathes
self.folderPath = os.path.join(self.basePath, self.folderName)
self.filePath = os.path.join(self.folderPath, self.fileName)
def markStartTime(self):
self.startTimeObj =datetime.datetime.now()
self.startTime = re.search('[^.]+', str(self.startTimeObj)).group()
def markEndTime(self):
endTimeObj = datetime.datetime.now()
self.endTime = re.search('[^.]+', str(endTimeObj)).group()
def generateHeader(self, header, columnNames):
rlt = []
for key, val in header.items():
rlt.append("# {} = {}".format(key, val))
# Column names are included for convenience
return '\r\n'.join(rlt) + '\r\n\r\n' + '\t'.join(columnNames)
def generateRunID(self, startTimeObj):
return "{}-{}-{}_{}-{}-{}".format(startTimeObj.year,
startTimeObj.month, startTimeObj.day,
startTimeObj.hour, startTimeObj.minute,
startTimeObj.second)
def createLog(self, logFilepath, content):
# Create the base path folder if it does not exist
if not os.path.exists(self.basePath):
os.makedirs(self.basePath)
with open(logFilepath, mode='x') as f:
f.writelines('\t'.join(content.keys()) + '\n')
def saveLog(self, content):
if len(content) == 0:
return
logFilepath = os.path.join(self.basePath, _logFileName)
# Create the log file if it does not exist
if not os.path.exists(logFilepath):
self.createLog(logFilepath, content)
# Save then new entry to the log file
rlt = map(str, content.values())
with open(logFilepath, mode='a') as f:
f.writelines('\t'.join(rlt) + '\n')