-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_interface.py
More file actions
315 lines (272 loc) · 8.92 KB
/
cmd_interface.py
File metadata and controls
315 lines (272 loc) · 8.92 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
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
import config
from feedobject import FeedObject
from tkinter import Tk
import os
from datetime import datetime
"""
TODO - seperate the two addplayers options into two main menu options
TODO - ways to delete players
TODO - regex analysis method
TODO - exportation methods to spreadsheet
TODO - database last updated
"""
def clear():
os.system('cls')
"""
Return a value, or -1 if enter was pressed and accept_enter is true
"""
def get_int(prompt,min,max, accept_enter):
while True:
try:
data = input(prompt)
if(accept_enter and len(data)==0):
return -1
result = int(data)
if(result >= min and result <= max):
return result
except Exception:
pass
def getusername(prompt):
"""
Gets a username. Returns the empty string if enter was pressed without any other input.
:param prompt:
:return:
"""
while True:
value = input(prompt)
if(len(value)==0):
return ""
else:
if(checkusername(value)):
return value
else:
print("Invalid username: {0}\n".format(value))
def get_date(prompt,format):
"""
Gets a date in a specified format. Returns None if enter was pressed without any other input
:param prompt:
:return:
"""
while True:
value = input(prompt)
if (len(value) == 0):
return None
else:
try:
value = value.rstrip()
output = datetime.strptime(value, format)
return output
except ValueError:
print("Cannot parse time: {0}\n".format(value))
def checkusername(name):
if len(name)<1 or len(name)>12:
return False
if(name[0] in " -_" or name[-1] in " -_"):
return False
for c in name:
if not (c.lower() in "abcdefghijklmnopqrstuvwxyz1234567890 -_"):
return False
return True
def UpdateCurrentEvents():
result = config.feeds.update()
print("=== Results ===")
if(len(result)==0):
print("(None)")
else:
new_entries = 0
for item in result:
new_entries += item.new_entries
for msg in item.warning_messages:
print("[{0}]: {1}".format(item.username,msg))
print("[{0}]: {1} new items found".format(item.username,item.new_entries))
def SaveFeeds():
curdir = config.default_feed_dir
config.feeds.save_feed(curdir)
print("Feeds saved. Press [ENTER] to continue. \n")
input()
def AddPlayers():
"""
:return: A list of warning/error messages.
"""
while True:
clear()
print("=== Add Player options ===")
print("")
print("1. Add one by one")
print("2. Paste from clipboard")
print("3. Accept and go back")
print("")
print("To swap or remove players, please edit the players.txt file.\n")
choice = get_int("Enter the choice >> ",1,3,False)
if(choice == 1):
while True:
next = getusername("Enter a username to add, or [ENTER] to return to main menu >> ")
if(len(next)==0):
return #return or break here?
try:
config.feeds.add_player(next)
print("Added username: "+next)
except KeyError as ex:
print("{0}".format(ex))
except Error as ex:
raise
elif choice == 2:
r = Tk()
r.withdraw()
k = r.clipboard_get()
r.destroy()
for name in k.splitlines():
try:
if(checkusername(name)):
config.feeds.add_player(name)
else:
print("Invalid username: {0}".format(name))
except Exception as ex:
raise
print("Error - {0}".format(ex))
elif choice == 3:
return
def ListPlayers():
while True:
clear()
print("=== List of Players ===")
if(len(config.feeds.logs)==0):
print("(None)")
print("")
print("Press [ENTER] to continue.")
input()
return
print("")
count = len(config.feeds.logs)
for i in range(0,count):
print((str(i+1)+".").ljust(5)+config.feeds.logs[i].username.ljust(20) + str(len(config.feeds.logs[i].events))+" events")
print("")
print("Enter a number to display the log, or press [ENTER] to exit.")
print("")
result = get_int(">> ",1,count,True)
if(result == -1):
return
else:
c=0
for evt in config.feeds.logs[result-1].events:
c+= 1
print("{:<4}".format(c) + str(evt))
print("")
print("Press [ENTER] to return to player list.")
print("Press [ENTER] twice to return to main menu.")
input("\n")
def DataAnalyze():
while 1:
count = len(config.regex_array)
print("Data Analyzer\n")
print("=== Predefined options ===")
for i in range(0,count):
print(str(i+1)+". "+config.regex_array[i][0])
print("")
print("0. Custom Regex")
print("")
print("Press [ENTER] to go back.")
print("")
pick = get_int("Choose an option >> ",0,count,True)
if(pick == 0):
pass #TODO
elif(pick==-1):
break
else:
picked_regex = config.regex_array[pick-1]
#so the month and day have to be 0 padded... and the hours is in 24h format...
print("Enter the START date in 'YYYY-MM-DD hh:mm' format, or press [ENTER] for the beginning of time.")
start_date = get_date(">> ","%Y-%m-%d %H:%M")
if (start_date is not None):
print("Picked start date", start_date.strftime(config.timestring))
print("Enter the ENDING date in 'YYYY-MM-DD hh:mm' format, or press [ENTER] for the end of time.")
end_date = get_date(">> ","%Y-%m-%d %H:%M")
if(end_date is not None):
print("Picked end",end_date.strftime(config.timestring))
result = config.feeds.count_matches(picked_regex[1], start_date, end_date)
print("\n=== Analysis of " + picked_regex[0]+" ===")
if(start_date is not None):
print("Starting at",start_date.strftime(config.timestring))
if(end_date is not None):
print("Ending at",end_date.strftime(config.timestring))
for item in result:
print(item[0].username+": " + str(item[1]))
print("\nPress [ENTER] to continue. \n")
input()
return
def InitRegex():
if(os.path.isdir(config.regexfile)):
print("Your regex file is a directory! Get rid of it.")
return
elif(not os.path.isfile(config.regexfile)):
open(config.regexfile,"w+").close()
f = open(config.regexfile, "r")
lines = f.readlines()
for i in range(0, len(lines), 2):
line = lines[i].rstrip()
line2 = lines[i + 1].rstrip()
if (len(line) == 0):
continue
config.regex_array.append([line, line2])
InitRegex()
while True:
clear()
try:
if(config.feeds is None):
config.feeds = FeedObject.init_feed()
except Exception as ex:
print("Error while attempting to create feed: {0}".format(ex))
raise
break
print("=== Player Adventurer Log Tracker ===")
print("Players tracked:",len(config.feeds.logs))
print("Opened: ",config.feeds.lastUpdated.strftime(config.timestring))
print("")
print("=== Options ===")
print("1. Update events")
print("2. Add players - from clipboard or manual entry")
print("3. List players - view events")
print("4. Analyze data - select date range")
print("5. Exit")
pick = get_int("Choose an option >> ",1,5,False)
if(pick==1):
UpdateCurrentEvents()
SaveFeeds()
elif(pick==2):
AddPlayers()
#UpdateCurrentEvents()
SaveFeeds()
elif(pick==3):
ListPlayers()
elif(pick==4):
DataAnalyze()
elif(pick==5):
print("Exiting..")
break
#########################
# CODE GRAVE LIES BELOW #
#########################
"""
while True:
print(checkusername(input("\n")))
"""
#filestorage.load_config()
"""
print("=== Feed Collections ===\n")
feeds = feedobject.list_feed_names()
if len(feeds == 0):
print("(None)")
else:
for i in range(0,len(feeds)):
print(str(i+1)+". " + feeds[i][len(config.feed_prefix):])
print("\n")
pick = get_int("Enter an option >> ",0,5,accept_enter=False)
if pick == 1:
pass
elif pick == 2:
pass
elif pick == 3:
pass
elif pick == 4:
pass
"""