-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathporter.py
More file actions
240 lines (192 loc) · 9.17 KB
/
porter.py
File metadata and controls
240 lines (192 loc) · 9.17 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
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
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
## A simple but powerful application for automatic study-booking for the library of CDUT.
##
## This work is under Toay's PRE-MIT license with extra agreement that:
## - to help protecting the server, rate limit must be setted below 1 rps
##
## Toay's PRE-MIT license is an open source license that simulates but has prerequisites in front of the MIT license.
## - Take a step to https://toay.org/projects/pre-mit-license to know more.
##
## Author: twikor(@twic.me)
## Required essential packages/libraries:
## - sys, datetime, time, json
# import essential packages/libraries
import os, sys, datetime, time, json
from terminaltables import AsciiTable
# initialize assets
configurations = []
configurationsToStore = {}
while (True):
# basic settings
operationInitialDelay = 10#minimal intervals between two operations, same trigger date
shelfDirectory = "./shelf/"
creditsFile = "./credits.json"
recipeFile = "./recipe.json"
welcomeWords = "-----\n\
Molly pre-configuration file generator\n\
By twikor@(twic.me)\n\
\n"
os.system("clear")
print(welcomeWords + "\
Operation Menu:\n\
1. add configuration entry\n\
2. view configuration entries\n\
3. port to configurations file\n\
4. port configurations to task queue\n\
q. exit\n\
ctrl + c to return to this menu\n\
Force exit? try using ctrl + c twice\n\
-----")
try:
print("[#]Input task id")
taskId = input(" : ")
if (taskId == "1"):
os.system("clear")
print(welcomeWords + "\
Operation: add configuration entry\n\
-----")
print("[1/5]Input room id")
roomId = input(" : ")
if (roomId != ""):
roomId = int(roomId)
user = None
userList = []
print("[2/5]Input user ('' to finish, 'r' to refill)")
while (user != ""):
user = input(" : ")
if (user != ""):
user = int(user)
userList.append(user)
if (user == "r"):
userList = []
print(" [*]Input cleared, continue")
if (userList != []):
userList.pop(-1)
print("[3/5]Specify period start-time (%H%M%S)")
periodStart = input(" : ")
print("[4/5]Specify period end-time (%H%M%S)")
periodEnd = input(" : ")
#applyToList = input("Specify the dates you want to apply the period to (JSON, (%Y%m%d)): ")
applyTo = None
applyToList = []
print("[5/5]Specify dates to apply the period to ('' to finish, 'r' to refill)")
while (applyTo != ""):
applyTo = input(" : ")
applyToList.append(applyTo)
if (applyTo == "r"):
applyToList = []
print(" [*]Input cleared, continue")
if (applyToList != []):
applyToList.pop(-1)
currentTimestamp = int(time.time())
for applyToDate in applyToList:
startAtTimeStructure = time.strptime(str(applyToDate) + str(periodStart), "%Y%m%d%H%M%S")
startAtTimeStamp = time.mktime(startAtTimeStructure)
startAtTime = time.strftime('%Y-%m-%d %H:%M:%S', startAtTimeStructure)
endAtTimeStructure = time.strptime(applyToDate + periodEnd, "%Y%m%d%H%M%S")
endAtTimeStamp = time.mktime(endAtTimeStructure)
endAtTime = time.strftime('%Y-%m-%d %H:%M:%S', endAtTimeStructure)
triggerAtDate = time.strftime("%Y%m%d", time.localtime(startAtTimeStamp - 6 * 24 * 3600))
operationDelay = operationInitialDelay
configurationToStore = {"roomId": roomId, "users": userList, "startAt": startAtTime, "endAt": endAtTime}
if (triggerAtDate not in configurationsToStore):
configurationsToStore[triggerAtDate] = []
operationDelay = operationInitialDelay
else:
countSameTriggerAtDateRegistries = len(configurationsToStore[triggerAtDate])
operationDelay = operationInitialDelay + countSameTriggerAtDateRegistries * 20
configurationToStore["triggerAt"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.mktime(time.strptime(triggerAtDate, "%Y%m%d")) + operationDelay))
configurationsToStore[triggerAtDate].append(configurationToStore)
configurations = []
for configurationTriggerDateGroup in configurationsToStore.values():
for index, configuration in enumerate(configurationTriggerDateGroup):
configurations.append(configurationTriggerDateGroup[index])
elif (taskId == "2"):
os.system("clear")
print(welcomeWords + "\
Operation: view configuration entries\n\
-----")
tableData = [['RoomId', 'User', 'StartAt', 'EndAt', 'TriggerAt']]
for configuration in configurations:
tableData.append([configuration['roomId'], configuration['users'], configuration['startAt'], configuration['endAt'], configuration['triggerAt']])
tableTitle = " Configuration entries in stock "
generateTable = AsciiTable(tableData, tableTitle)
print(generateTable.table)
print("[*]Use operation 3 to stage all entries to the file, or operation 4 to stage directly to task queue")
elif (taskId == "3"):
os.system("clear")
print(welcomeWords + "\
Operation: port to configurations file\n\
-----")
if (configurations == []):
print("[*]No task in stock, you have to add them before porting")
else:
if (os.path.isfile(recipeFile) == True):
print("[*]Recipe file already exists, override? ['Y' to continue/any other to cancel]")
confirmAction = input(" : ")
if (confirmAction != "Y"):
print("[*]Operation canceled")
else:
try:
os.system("rm " + recipeFile)
except:
print("[*]Error removing recipe file")
else:
print("[*]Successfully removed old recipe file")
try:
os.system('touch ' + recipeFile)
recipeFile = open(recipeFile, 'a')
recipeList = configurations
recipeJson = json.dumps(recipeList, ensure_ascii = False)
recipeFile.write(recipeJson)
recipeFile.close()
except:
print("[*]Error porting to recipe file")
else:
print("[*]Successfully ported entries to recipe file")
print("[*]Clean up? ('Y' to continue, any other to cancel)")
confirmAction = input(" : ")
if (confirmAction == "Y"):
configurations = []
configurationsToStore = {}
elif (taskId == "4"):
os.system("clear")
print(welcomeWords + "\
Operation: port configurations to task queue\n\
-----")
print("Porting to task queue may influence standby tasks and can be hard to reverse, confirm? ('Y' to continue, any other to cancel)")
confirmAction = input(" : ")
if (confirmAction == "Y"):
coffees = []
toImportTasks = configurations
for toImportTaskIndex, toImportTask in enumerate(toImportTasks):
toImportTaskTriggerAt = toImportTask['triggerAt']
toImportTaskTriggerAtTimeStructure = time.strptime(toImportTaskTriggerAt,"%Y-%m-%d %H:%M:%S")
toImportTaskId = time.strftime("%Y%m%d%H%M%S", toImportTaskTriggerAtTimeStructure)
toImportTask['taskId'] = toImportTaskId
toImportTask['attemptedTimes'] = 0
coffees.append(toImportTask)
if (os.path.exists(shelfDirectory) == False):
os.system("mkdir " + shelfDirectory)
for coffee in coffees:
taskFileName = coffee['taskId']
os.system("touch " + shelfDirectory + taskFileName)
coffee = json.dumps(coffee, ensure_ascii = False)
coffeeFile = open(shelfDirectory + taskFileName, 'a')
contentLineToWrite = str(coffee)
coffeeFile.write(coffee)
coffeeFile.close()
else:
print("[*]Operation canceled")
elif (taskId == "q"):
print("[#]Bye :)")
break
else:
print("[#]Task not found")
print("[#]Process finished")
print("[#]To return to menu use ctrl + c, automatically return in 300 seconds")
time.sleep(300)
except KeyboardInterrupt:
print("\n[#]Signal received, returning to menu :)")
time.sleep(0.5)