-
Notifications
You must be signed in to change notification settings - Fork 0
/
bjjournalCli.py
356 lines (251 loc) · 11.9 KB
/
bjjournalCli.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
import sys
import os
import re
import glob
import getch
from colorama import init, deinit, Fore, Back, Style
#for clearing terminal in Windows and Unix too
os.system('cls' if os.name == 'nt' else 'clear')
class BG:
BLACK='\033[48;2;0;0;0m'
BROWN='\033[48;2;9;67;58m'
PURPLE='\033[48;2;150;0;120m'
BLUE='\033[48;2;10;0;88m'
WHITE='\033[48;2;255;255;255m'
init()
# function to convert a list to string
def listToString(s):
# initialize an empty string
str1 = ""
# traverse in the string
for ele in s:
str1 += ele
# return string
return str1
def getMultilineInput(lineArray):
line = "NaN"
while True:
if ( line != "> \n" ):
line = "> " + input()
#Make a new line
line +="\n"
else:
lineArray.pop()
line ="-*-"
lineArray.append(line)
break
lineArray.append(line)
#making notes into a string to copy to the file
return listToString(lineArray)
def makeFilename(text, mode):
##This function makes the string strictly alphanumeric except the character specified by the variable 'pop', thus ok for a filename.
#This is used for replacing special chars in dates in filenames
if mode == 0 :
pop = '_'
#This, for hours
else:
pop = '.'
text = re.sub('[^A-Za-z0-9]+', pop , text)
return text
def checkAndFormatYN(gi_str):
if gi_str.lower() in ['yes', 'y', 'ye', 'yah', 'ja', 'yeh', 'da']:
gi_str = "y"
elif gi_str.lower() in ['no','n','nope','nah','nein','niet', 'wtf']:
gi_str = "n"
else:
gi_str = input(Style.RESET_ALL +Fore.RED +' (Invalid Answer) '+ Fore.GREEN +'├─Did you wear a Gi? (y/n) -> ' + Style.RESET_ALL + Fore.CYAN)
checkAndFormatYN(gi_str)
return gi_str
def checkAndFormatBeltColor(color_str):
#Checks the belt color name and formats it properly in case it is messed up, because it is important
if color_str.lower() == 'white':
color_str = 'White'
elif color_str.lower() == 'blue':
color_str = 'Blue'
elif color_str.lower() == 'PURPLE':
color_str = 'PURPLE'
elif color_str.lower() == 'brown':
color_str = 'Brown'
elif color_str.lower() == 'black':
color_str = 'Black'
else:
color_str = input(Style.RESET_ALL +Fore.GREEN +'║ ' + Fore.BLUE + 'NEW PROFILE ' + Fore.GREEN + ' ╟──┼─What color of belt do you have?' + Fore.RED + '(Input a valid color!)' + Fore.GREEN + ' -> ' + Style.RESET_ALL + Fore.CYAN)
color_str = checkAndFormatBeltColor(color_str)
return color_str
def getInfo():
print(Fore.YELLOW + '\n** ADDING NEW USER PROFILE' + Style.RESET_ALL)
newName = input(Fore.GREEN + ' ╭─What\'s your name? -> ' + Style.RESET_ALL + Fore.CYAN)
newClub = input(Style.RESET_ALL +Fore.GREEN + '╔══════════════╗ ├─What is the name of your club -> ' + Style.RESET_ALL + Fore.CYAN)
newBelt = input(Style.RESET_ALL +Fore.GREEN +'║ ' + Fore.BLUE + 'NEW PROFILE ' + Fore.GREEN + ' ╟──┼─What color of belt do you have? -> ' + Style.RESET_ALL + Fore.CYAN)
newBelt = checkAndFormatBeltColor(newBelt)
newStripes = input(Style.RESET_ALL +Fore.GREEN +'╚══════════════╝ ├─How many stripes do you have? -> ' + Style.RESET_ALL + Fore.CYAN)
print(Style.RESET_ALL + Fore.GREEN +' ╰─Any additional notes/achievements (Championships etc, '+ Fore.RED +' press enter twice to stop' + Fore.GREEN + '): ' + Style.RESET_ALL + Fore.CYAN)
newAchievements = []
newAchievementsStr = getMultilineInput(newAchievements)
with open('info/userInfo', 'w') as f:
print("++++ PROFILE INFO ++++\n" + 'Name: ' + newName + '\nClub: ' + newClub + '\nBelt: ' + newBelt + '\nStripes: ' + newStripes + '\nAchievements:\n' + newAchievementsStr, file=f)
def printBelt(config):
file_contents = config.read()
if "Belt: White" in file_contents:
belt_color = BG.WHITE
letter_color = Fore.BLACK
stripe_area_color = BG.BLACK
elif "Belt: Blue" in file_contents:
belt_color = BG.BLUE
letter_color = Fore.WHITE
stripe_area_color = BG.BLACK
elif "Belt: Purple" in file_contents:
belt_color = BG.PURPLE
letter_color = Fore.WHITE
stripe_area_color = BG.BLACK
elif "Belt: Brown" in file_contents:
belt_color = BG.BROWN
letter_color = Fore.WHITE
stripe_area_color = BG.BLACK
elif "Belt: Black" in file_contents:
belt_color = BG.BLACK
letter_color = Fore.WHITE
stripe_area_color = Back.RED
else:
belt_color = Style.RESET_ALL
letter_color = Fore.RED + "(Error reading Belt Color)"
stripe_area_color = Style.RESET_ALL
stripe_no = 0
#double regex to find out where the stripe info is written and how many stripes are there
if "Stripes:" in file_contents:
stripe_no = re.findall(r'Stripes: \d+', file_contents)
stripe_no = re.findall(r'\d+', stripe_no[0])
name = "Unnamed Grappler"
if "Name:" in file_contents:
name = re.findall(r'Name: .*', file_contents)
name = re.sub(r'Name: ','', name[0])
#Printing the belt graphic
print(belt_color + ' ' + letter_color + name + ' ' + stripe_area_color + ' ', end = '' )
for i in range(int(stripe_no[0])) :
print(BG.WHITE + ' ' + stripe_area_color + ' ', end = '' )
print(' ' + Style.RESET_ALL)
def printTotalTime():
try:
journalPath = glob.glob("journalEntries/*")
if journalPath == None:
raise OSError
time = 0
GiTime = 0
NoGiTime = 0
r = re.compile("Duration: .*")
r2 = re.compile(r"\*\*\* .*GI TRAINING \*\*\*")
for journal in journalPath:
journal=open(journal, 'r')
journal_contents = journal.readlines()
journal_contents_dur = list(filter(r.match, journal_contents))
journal_contents_gi = list(filter(r2.match, journal_contents))
if journal_contents_dur:
for i in range(len(journal_contents_dur)):
## For Debugging
# print(journal_contents_gi)
# print(journal_contents_dur)
if journal_contents_gi[0] == "*** NOGI TRAINING ***\n":
NoGi = 0
else:
NoGi = 1
tempTime = re.findall(r'Duration: \d+', journal_contents_dur[i])
tempTime = re.findall(r'\d+', tempTime[0])
tempTime_str=''.join(tempTime)
time += int(tempTime_str)
if NoGi:
NoGiTime += int(tempTime_str)
else:
GiTime += int(tempTime_str)
print(Fore.BLUE + "* " + Fore.GREEN + "Total mat time: " + Fore.CYAN + str(time) + Fore.GREEN + " hours!" + Style.RESET_ALL)
print("├─ "+ Fore.GREEN +" NoGi time: " + Style.RESET_ALL + str(NoGiTime) + Fore.GREEN + " hours" + Style.RESET_ALL)
print("╰─ "+ Fore.GREEN +" Gi time: " + Style.RESET_ALL + str(GiTime) + Fore.GREEN + " hours" + Style.RESET_ALL)
except OSError:
print(' !! No journalEntries found!')
def printClubAndNotes(config):
file_contents = config.read()
club = re.findall(r'Club:.*', file_contents)
club = re.sub(r'Club:','', club[0])
print(Fore.GREEN + "Club:" + Fore.BLUE + club + Style.RESET_ALL)
print(Fore.GREEN + "Bio:" + Style.RESET_ALL)
#re.MULTILINE is important, it is needed for the regex to match the EOF
ach = re.findall(r'^> .*', file_contents, re.MULTILINE)
for i in range(len(ach)):
#Remove the '> ' string for display
ach[i] = re.sub(r'> ','', ach[i])
print(Fore.BLUE + ach[i] + Style.RESET_ALL)
def displayAllInfo():
try:
inf = open('info/userInfo', 'r')
except OSError:
print(' !! No Config found! Want to make a new user profile?')
getInfo()
displayAllInfo()
else:
#if the opening of the file fails, this is not executed and the function is called again (see 'except OSerror' section 5 lines up)
print(Fore.GREEN + '*' + Style.RESET_ALL + ' Config loaded!')
printBelt(inf)
printTotalTime()
#placing the reader at the start of the file again
inf.seek(0)
printClubAndNotes(inf)
inf.close()
def editInfo():
print(" > Options:\n· Press 1 for full setup\n· Press 2 to edit Name\n· Press 3 to edit Belt Level.\n· Press 4 to edit Dojo/Club.\n· Press 0 to go back.")
choice = input("Enter your choice here -> ")
if choice == '1':
addNew()
if choice == '2':
editInfo()
def addNew():
print(Fore.YELLOW + '\n** ADDING NEW TRAINING LOG' + Style.RESET_ALL)
newDate = makeFilename( input(Fore.GREEN + ' ╭─Enter date of training session -> ' + Style.RESET_ALL + Fore.CYAN), 0)
newStart = makeFilename( input(Style.RESET_ALL +Fore.GREEN + '╔══════════════╗ ├─Enter the time it started -> ' + Style.RESET_ALL + Fore.CYAN), 1)
newHours = input(Style.RESET_ALL +Fore.GREEN +'║ ' + Fore.BLUE + 'TRAINING LOG' + Fore.GREEN + ' ╟──┼─Enter duration -> ' + Style.RESET_ALL + Fore.CYAN)
newTags = input(Style.RESET_ALL +Fore.GREEN +'╚══════════════╝ ├─Enter Tags -> ' + Style.RESET_ALL + Fore.CYAN)
newGi = input(Style.RESET_ALL +Fore.GREEN +' ├─Did you wear a Gi? (y/n) -> ' + Style.RESET_ALL + Fore.CYAN)
#Checking and formatting this answer
newGi = checkAndFormatYN(newGi)
#Getting multiline input from user
print(Style.RESET_ALL + Fore.GREEN +' ╰─Enter Notes (' + Fore.BLUE + 'Double enter to stop taking notes' + Fore.GREEN + '): ' + Style.RESET_ALL + Fore.CYAN)
newNotes = []
newNotesStr = getMultilineInput(newNotes)
with open('journalEntries/'+newDate+'_'+newStart, 'w') as f:
if ( newGi == 'n' ):
print("*** NOGI TRAINING ***", file=f)
else:
print("*** GI TRAINING ***", file=f)
print("Tags: " + newTags, file=f)
print("Date: " + newDate, file=f)
print("Duration: " + newHours + " hours", file=f)
print("-*- Notes: \n\n" + newNotesStr, file=f)
####Main Program starts here
print(Fore.BLUE + '*** BJJOURNAL ***' + Style.RESET_ALL)
##Making directory for fighter's info, if it exists throw error
try:
os.mkdir('info')
except OSError:
print(Fore.GREEN + '*' + Style.RESET_ALL + ' Config directory \'info\' found!')
else:
print('No Config directory found (and thus no \'userInfo\' Config)! Want to make a new user profile?')
getInfo()
##Making dir for training logs, if it exists throw error
try:
os.mkdir('journalEntries')
except OSError:
print(Fore.GREEN + '*' + Style.RESET_ALL + ' Journal Entries found!')
while 1:
displayAllInfo()
print(" > Options:\n" + Fore.YELLOW + "·" + Style.RESET_ALL + " Press " + Fore.YELLOW + "1" + Style.RESET_ALL + " to add a new training log\n" + Fore.YELLOW + "·" + Style.RESET_ALL + " Press " + Fore.YELLOW + "2" + Style.RESET_ALL + " for editing your profile info\n" + Fore.YELLOW + "·" + Style.RESET_ALL + " Press " + Fore.YELLOW + "0" + Style.RESET_ALL + " to exit.")
#choice = input("Enter your choice here -> ")
choice = getch.getch()
if choice == '1':
addNew()
elif choice == '2':
editInfo()
elif choice == '0':
print("\nBye :)\n\n")
sys.exit()
else:
print(Fore.RED + 'Please enter a valid value' + Style.RESET_ALL)
deinit()