-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiary.py
546 lines (477 loc) · 19.1 KB
/
diary.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
VERSION = '3.2'
TESTING = VERSION[-5:] == 'debug'
if TESTING: from traceback import print_exc
import calendar
import re
import sys
from abc import abstractmethod
from datetime import datetime, timedelta
from msvcrt import getch, kbhit
from os import path, sep, system
from time import sleep
try:
try:
exec(open(sep.join([path.expanduser('~'), 'diary_config']), 'r').read())
if TESTING: filelocation = 'testdiary'
except:
file = input('Specify a location to read/write your Diary file : ')
filelocation = file.split(sep)+['diary']
typespeed = 1.25
try:
with open(sep.join([path.expanduser('~'), 'diary_config']), 'w') as f:
f.write('filelocation, typespeed = %s, %s'%(filelocation, typespeed))
except Exception as e:
input('There was an error, try a valid filename with write permissions.')
if TESTING: print(e)
exit()
filename = sep.join(filelocation)
if TESTING: filename='testdiary'
except Exception as e:
input('include error '+str(e))
exit()
class EmergencyStop(Exception):
'''raised when User presses Ctrl+C during the record of an entry.'''
pass
class BadFileHeader(Exception):
'''raised when the metadata in the provided diary file does not match any version of FileManagers.'''
class FileManager:
'''To manage file read/write across different versions'''
def backup(self, name):
file = open(self.fileName, 'r')
back = open(name, 'w')
for line in file:
back.write(line)
file.close()
back.close()
return True
@staticmethod
def getManager(fileName:str, preferredFiler:"FileManager"=None):
'''Returns the registered class for accessing the file and ensure file is initialized.'''
REGISTERED_FILERS = [Filer_2_10, Filer_3_2,] # arranged earliest first
if preferredFiler and preferredFiler in REGISTERED_FILERS:
return preferredFiler(fileName)
# if file don't exist, use the latest File Manager
if not path.isfile(fileName):
return REGISTERED_FILERS[-1](fileName)
# if file exists, use corresponding FileManager
with open(fileName, 'r') as file:
meta = file.readline()[:-1]
for filer in REGISTERED_FILERS:
log('checking', meta, filer.headerString())
if meta == filer.headerString():
log(filer)
return filer(fileName)
else:
# TODO: file don't have a suitable meta, catch error in controller
raise BadFileHeader
@abstractmethod
def __init__(self, fileName):
self.fileName = fileName
@classmethod
@abstractmethod
def headerString(self):
'''returns the header string that should be found at the first line of the file'''
@abstractmethod
def load(self) -> list:
'''Loads data if file exists and header matches.\n
Raises exception if file don't exist.\n
Raises BadFileHeader exception if header don't match.'''
@abstractmethod
def write(self, *entries):
'''write entries into file, if file don't exist: Creates a new one'''
class Filer_3_2(FileManager):
HEADER = 'diary v%s github.com/madhaven/diary'
VERSION = '3.2'
ENTRY = '\n%s%s%s\n'
@classmethod
def headerString(self):
return self.HEADER%self.VERSION
def entryString(self, entry:"Entry"):
return '\n%s%s%s\n'%(
entry.time.ctime(),
entry.text,
str(entry.intervals)
)
def load(self) -> list:
entries = []
with open(self.fileName, 'r') as file:
if self.headerString() != file.readline()[:-1]:
raise BadFileHeader
file.readline()
while True:
text = file.readline()
if not text:
# TODO: escape blank lines / add info for SESSION concept in Diary
break
intervals = [float(time) for time in file.readline()[1:-2].split(', ')]
file.readline()
entry = Entry(text[24:], datetime.strptime(text[:24], '%a %b %d %H:%M:%S %Y'), intervals)
entry.printdate = False if (entries and entries[-1].time.day==entry.time.day) else True
entries.append(entry)
return entries
def write(self, *entries):
with open(self.fileName, 'a') as file:
if file.tell()==0:
file.writelines([self.headerString(), '\n'])
for entry in entries:
if entry:
file.write(self.entryString(entry))
class Filer_2_10(FileManager):
HEADER = 'diary v2.10 github.com/madhaven/diary'
@classmethod
def headerString(self):
return self.HEADER
def load(self) -> list:
entries = []
with open(self.fileName, 'r') as file:
if self.headerString() != file.readline()[:-1]:
raise BadFileHeader
file.readline()
while True:
text = file.readline()
if not text:
# skip blank lines / add info for session concept in Diary
break
intervals = [float(time) for time in file.readline()[1:-2].split(', ')]
file.readline()
entry = Entry(text[24:], datetime.strptime(text[:24], '%a %b %d %H:%M:%S %Y'), intervals)
entry.printdate = False if (entries and entries[-1].time.day == entry.time.day) else True
entries.append(entry)
return entries
def write(self, *entries):
with open(self.fileName, 'a') as file:
if not file.tell():
file.writelines([self.headerString()])
for entry in entries:
file.write('\n\n%s%s%s'%(entry.time.ctime(), entry.text, entry.intervals))
class Entry:
'''stores an entry that the user makes'''
def __init__(self, text:str='', time:datetime=None, intervals:list=[], printdate:bool=False):
'''set empty strings'''
self.text:str = text
self.time:datetime = time
self.printdate:bool = printdate
if intervals and len(intervals)==len(text):
self.intervals:list = intervals
else:
self.intervals:list = [0 for _ in range(len(text))]
def __bool__(self) -> bool:
return len(self.text) > 0
def __eq__(self, __o: "Entry") -> bool:
if self.text != __o.text:
return False
for x, y in zip(self.intervals, __o.intervals):
if x != y:
return False
return True
def __str__(self):
'''
Convert user input information and returns data alone\n
used in search ops where backspace characters need not be considered\n
in the entry
'''
text = ''
for x in self.text:
text = text[:-1] if x == '\b' else text + x
return text
def addChar(self, char:str, time:float):
'''adds another character to the entry.\n
the time attribute saves the time taken before/after the keypress.'''
self.text += char
self.intervals += [time]
class Diary:
'''
class to handle all Diary interactions
'''
def __init__(self, filename:str):
'''initializes the Diary'''
self.entries = []
self.filer:FileManager = FileManager.getManager(filename)
def add(self, *entries):
'''writes the entry/entries to the file'''
self.filer.write(*entries)
def load(self):
'''
Scans the file specified on creation and initializes the list of Entry objects\n
This replaces any existing Entries in memory with data from the file\n
\n
Only recommended when reading entries as adding entries to the diary do not require any data in memory
'''
self.entries = self.filer.load()
def filter(self, year:int=None, month:int=None, day:int=None, fetchLatest=False) -> list:
'''fetches diary records acc to date match, if `fetchLatest` is set to True, entries from the last day is fetched.'''
self.load()
if fetchLatest:
lastDate:datetime = self.entries[-1].time
year, month, day = lastDate.year, lastDate.month, lastDate.day
return [
entry for entry in self.entries
if ((not year or year==entry.time.year) and
(not month or month==entry.time.month) and
(not day or day==entry.time.day))
]
def search(self, *args, strictMode:bool=False) -> list:
'''Search/Find keywords, returns a list of Entry objects'''
results = []
self.load()
if strictMode:
for entry in self.entries:
entry_str = str(entry).lower()
for i, s in enumerate(args):
if s.lower() not in entry_str:
break
elif i == len(args) - 1:
results.append(entry)
else:
for entry in self.entries:
entry_str = str(entry).lower()
for i, s in enumerate(args):
if s.lower() in entry_str:
results.append(entry)
break
return results
def backup(self, name:str=None, *args):
if not name:
name = self.filer.fileName
name = name[:name.rfind(sep)+1] + 'diaryback_'
name += datetime.now().strftime('%Y%m%d%H%M')
self.filer.backup(name)
def export(self, args:str=None):
'''Handles Export options'''
if not args:
print(
'not implemented',
# 'Usage','-----',
# 'diary export[to] text/txt',
# 'diary export[to] csv',
sep = '\n'
)
return
class DiaryController():
'''provide access to Diary'''
def __init__(self, filename:str, stopWord:str='bye', typespeed:float=1.5):
self.stopWord:str = stopWord
self.typespeed:int = typespeed
try:
self.diary:Diary = Diary(filename)
except BadFileHeader as e:
raise NotImplementedError
self.version:str = VERSION
def _showVersion(self, args=None):
'''Shows Controller Version'''
print("Diary.py v" + self.version + "\ngithub.com/madhaven/Diary")
def _showInfo(self):
'''Shows Manual'''
self._showVersion()
print(
'\nUsage','-----',
'diary log|entry - to add to your diary',
'diary version - to check which version your diary is running',
'diary read - to access older entries or logs that you have made',
'diary search|find - to search for keywords',
'diary searchall|findall - to search for entries containing all the keywords',
'diary backup [filename]',
'diary export - to export your entries to portable formats | NOT AVAILABLE',
sep='\n'
)
def main(self, *args):
'''Main entry point into Diary. parses cli args to select menu'''
try:
if not args:
self._showInfo()
elif args[0] in ['log', 'entry']:
self.log()
elif args[0] in ['read', 'show']:
self.read(*args[1:])
elif args[0] in ['search', 'find']:
self.search(*args[1:])
elif args[0] in ['searchall', 'findall', 'search all', 'find all']:
self.search(*args[1:], strictMode=True)
elif args[0] in ['export as', 'export to', 'export']:
self.diary.export(*args[1:])
elif args[0] in ['version', '--version']:
self._showVersion()
elif args[0] in ['backup']:
self.backup(*args[1:])
else:
self._showInfo()
except FileNotFoundError as e:
print('\nDiary file missing "%s"?'%self.diary.filer.fileName)
def log(self):
'''
To record diary entries\n
Initiates a loop of Entry recordings\n
Loop ends when the stop word is found in an Entry\n
'''
try:
while True:
entry = self._record()
self.diary.add(entry)
if self.stopWord in str(entry).lower():
break
except EmergencyStop as e:
self.diary.add(e.entry)
system('cls')
except Exception as e:
print('your last entry was broken: %s'%entry)
raise e
def _record(self) -> Entry:
'''method to record an entry from the cli interface. A single entry ends when the return key is pressed. The diary log ends if the stopword is found in the entry.'''
try:
# record current time
entry = Entry(time=datetime.now())
t1 = entry.time.timestamp()
while True:
# get pressed character
char = str(getch())[2:-1]
# format time
t2 = datetime.now().timestamp()
if (t2-t1) > 60:
t2, t1 = (t2-t1) - int(t2-t1) + 5, t2
else:
t2, t1 = t2 - t1, t2
t2 = round(t2, 4)
# return key
if char == '\\r':
# do nothing if text is empty
if not entry:
continue
# add a new line character to the entry and format the entry object
entry.addChar('\n', t2)
print()
return entry
# ctrl+c
elif char == '\\x03':
if entry:
entry.addChar('\n', t2)
raise EmergencyStop(entry)
# escape char
elif char == '\\\\':
print('\\', end='', flush=True)
char = '\\'
# tabspace
elif char == '\\t':
print('\t', end='', flush=True)
char = '\t'
# backspace?
elif char == '\\x08':
print('\b \b', end='', flush=True)
char = '\b'
# stray char
elif len(char) > 1:
char = str(getch())[2:-1]
continue
# normal char
else:
print(char, end='', flush=True)
# add char to entry list
entry.addChar(char, t2)
except Exception as e:
print(e)
raise e
def read(self, *args):
'''displays the diary entries of the queried day'''
if not args:
print(
'Usage',
'-----',
'diary read all',
'diary read today',
'diary read yesterday',
'diary read [YYYY] [Mon] [DD]',
sep='\n'
)
return
if args[0]=='all':
year, month, day, getLatest = None, None, None, False
elif args[0] in ['yesterday', 'today']:
args = datetime.now()-timedelta(days=1) if args[0]=='yesterday' else datetime.now()
year, month, day, getLatest = args.year, args.month, args.day, False
elif args[0] in ['latest', 'last']:
year, month, day, getLatest = None, None, None, True
else:
try:
year = list(filter(re.compile(r'^\d{4}$').match, args))
year = int(year[0]) if year else None
monthname = list(filter(re.compile(r'^[a-zA-Z]{3}.*$').match, args))
monthname = monthname[0] if monthname else None
month = {
month[:3].lower(): index
for index, month in enumerate(calendar.month_abbr)
if month
}[monthname[:3]] if monthname else None
day = list(filter(re.compile(r'^\d{1,2}$').match, args))
day = int(day[0]) if day else None
getLatest = False
except:
print("That date doesn't look right")
if TESTING: print_exc()
return
entries = self.diary.filter(year, month, day, getLatest)
count = len(entries)
print('%s %s found'%(count, 'entry' if count==1 else 'entries'))
try:
for entry in entries:
self.printEntry(entry)
except KeyboardInterrupt:
print("\nDiary closed")
return
def search(self, *args, strictMode:bool=False):
'''Search/Find keywords'''
if not args:
print(
'Usage',
'-----',
'diary search [search_text [search_text2 [...]]]',
'diary searchall [search_text [search_text2 [...]]]',
'diary searchall matches all strings together',
'PS : find and findall also works the same',
sep = '\n'
)
return
results = self.diary.search(*args, strictMode=strictMode)
for entry in results:
print(entry.time.strftime('%Y %b %d %H:%M:%S %a'), '|', str(entry), end='')
count = len(results)
print("%s %s found"%(count, "entries" if count!=1 else "entry"))
def printEntry(self, entry:Entry, speed:int=None):
'''
prints the entry\n
Contains sleep() to imitate the user's type speed\n
self.printdate decides whether or not to print the timestamp\n
'''
speed = self.typespeed if not speed else speed
skipFactor = 1 # speed when user decides to skip an entry
if entry.printdate:
print('\n' + entry.time.ctime(), flush=True)
for letter, time in zip(entry.text, entry.intervals):
sleep(time * skipFactor / speed)
if kbhit() and str(getch())[2:-1] in ['\\r', ' ']:
skipFactor=0
print(
'\b \b' if letter=='\b' else letter,
end='', flush=True
)
def backup(self, name:str=None, *args):
print('backing up diary%s...'%((' to %s'%name) if name else ''))
self.diary.backup(name)
print('COMPLETE')
def log(*args, pause=False, **kwargs):
'''to log values while testing'''
if TESTING:
print(*args, **kwargs)
if pause:input()
if __name__ == '__main__':
#get cli args
cliargs = [arg.lower() for arg in sys.argv][1:]
log('DIARY cli args', sys.argv, '->', cliargs)
try:
controller = DiaryController(filename, typespeed=typespeed)
controller.main(*cliargs)
except Exception as e:
print(
'An error crashed the diary :', str(e),
'please report issues to https://github.com/madhaven/Diary/issues',
sep='\n', end='\n\n'
)
if TESTING: print_exc()