-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiary.py
executable file
·408 lines (298 loc) · 11.9 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
#!/usr/bin/env python
# Python script that makes timekeeping and note entry conveniently simple.
# Copyright (C) 2014 Charlie Thanh Le
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import argparse
import cmd
import datetime
import json
import logging
import os
import sys
import re
import socket
from subprocess import Popen
from datetime import datetime, timedelta
from cryptography.fernet import Fernet, InvalidToken
__app_name__ = 'Diary'
__version__ = '0.1.2'
actiondict = {'b': "Begin ", 's': "Stop ", 'm': "Mark "}
actionregex = '' + '|'.join([v for k, v in actiondict.items()])
fs_delim = '/'
if os.name == 'nt':
fs_delim = '\\'
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().name = __app_name__
logging.basicConfig(format='%(levelname)s [%(name)s]: %(message)s')
logger = logging.getLogger()
default_config = {
'key': Fernet.generate_key(),
'editor_path': '/usr/bin/open',
'editor_args': ['-W', '--new'],
'timestamp_format': '%H:%M:%S',
'diary_base': '{}/.diary'.format(os.environ['HOME'])
}
# Global cipher suite
cipher_suite = None
class DiaryShell(cmd.Cmd):
intro = 'Welcome to the Diary shell. Type help or ? to list commands.\n'
prompt = '(Diary) > '
def __init__(self, config):
cmd.Cmd.__init__(self)
self.config = config
def do_b(self, arg):
"""Append your diary with Begin [Timestamp] [Computer Name]."""
write_to_diary(self.config, 'b')
def do_s(self, arg):
"""Append your diary with Stop [Timestamp] [Computer Name]."""
write_to_diary(self.config, 's')
def do_m(self, arg):
"""Append your diary with Mark [Timestamp] [Computer Name]."""
write_to_diary(self.config, 'm')
def do_o(self, arg):
"""Open your diary in a text editor."""
# Seems to pass in empty string if no arg was passed in.
if len(arg):
open_diary(self.config, arg)
else:
open_diary(self.config, 0)
def do_timestamps(self, arg):
"""Print out the past x number of days of Diary entries."""
if arg:
timestamps(self.config, days=int(arg))
else:
timestamps(self.config)
@staticmethod
def do_q(arg):
'Quit out of the Diary Shell.'
bye()
return True
def encrypt_string(config, val):
"""
Using the key from the configuration, encrypt the given string
:param config:
:param val:
:return:
"""
global cipher_suite
if not cipher_suite:
cipher_suite = Fernet(config['key'].encode('utf8'))
return cipher_suite.encrypt(val)
def decrypt_string(config, val):
"""
Using the key from the configuration, decrypt an encrypted string
:param config:
:param val:
:return:
"""
global cipher_suite
if not cipher_suite:
cipher_suite = Fernet(config['key'].encode('utf8'))
try:
return cipher_suite.decrypt(val)
except InvalidToken as e:
logger.warn('Failed to decrypt, perhaps it was not encrypted to begin with? {}'.format(e))
def decrypt_diary(config, filename):
"""
Decrypt a diary file in-place
"""
logger.debug('decrypting diary "{}"'.format(filename))
try:
with open(filename, 'r') as f:
raw = f.read()
if raw:
decrypted = decrypt_string(config, raw)
if decrypted:
with open(filename, 'w') as f:
f.writelines(decrypted)
except IOError as e:
logger.critical('Failed to decrypt diary file {}: {}'.format(filename, e))
def encrypt_diary(config, filename):
"""
Encrypt a diary file in-place
"""
logger.debug('encrypting diary "{}"'.format(filename))
try:
with open(filename, 'r') as f:
raw = f.read()
if raw:
encrypted = encrypt_string(config, raw)
if encrypted:
with open(filename, 'w') as f:
f.writelines(encrypted)
except IOError as e:
logger.critical('Failed to encrypt diary file {}: {}'.format(filename, e))
def diary_file(config, deltadays=0):
diary_date = datetime.today() + timedelta(days=-int(deltadays))
return '{}{}{}.txt'.format(config['diary_base'], fs_delim, diary_date.strftime('%Y-%m-%d'))
def write_to_diary(config, command):
filename = diary_file(config)
if os.path.isfile(filename):
# First, decrypt it
decrypt_diary(config, filename)
try:
with open(filename, mode='a') as diary:
# Append diary with a newline if content exists.
entry = ''
if diary.tell() is not 0:
entry = '\n'
entry += actiondict[command]
diary.write('{}{} {}\n'.format(entry, datetime.today().strftime(config['timestamp_format']), socket.gethostname()))
diary.close()
# Now, encrypt it
encrypt_diary(config, filename)
except IOError as e:
logger.critical('Failed to open diary file "{}": {}'.format(filename, e))
sys.exit(1)
def open_diary(config, deltadays=0):
filename = diary_file(config, deltadays)
if not os.path.isfile(filename):
open(filename, 'a').close()
# decrypt it
decrypt_diary(config, filename)
logger.info('Opening diary: {}'.format(filename))
print('\n\t -- You must completely close the application before this script will be able to continue --\n'),
args = [config['editor_path']] + config['editor_args'] + [filename]
try:
Popen(args).wait()
except KeyboardInterrupt:
logger.debug("Canceled, hopefully you've saved your changes!")
encrypt_diary(config, filename)
def bye():
logger.info('Diary is exiting...')
def timestamps(config, days=14):
"""
Prints out lines beginning with action words in the last x amount of days
:param config:
:param days: number of days to print lines for
:return:
"""
last_diary_file = None
for days_ago in range(days, -1, -1):
try:
filename = diary_file(config, deltadays=days_ago)
with open(filename, "r") as diary:
if last_diary_file:
logger.warning("Diary entries between {} through {} could not be found.".format(last_diary_file, filename.split(fs_delim)[-1]))
last_diary_file = None
decrypt_diary(config, filename)
for line in diary:
if re.search(actionregex, line):
logger.info("{}: {}".format(filename.split(fs_delim)[-1], line))
encrypt_diary(config, filename)
except IOError as e:
last_diary_file = filename.split(fs_delim)[-1]
pass
def ensure_base_path(config):
"""
Ensures the base path for saving diary files exists
:param config:
:return:
"""
if not os.path.exists(config['diary_base']):
try:
os.makedirs(config['diary_base'], mode=0o700)
except OSError as e:
logger.critical('Failed to create diary base directory "{}": {}'.format(config['diary_base'], e))
def create_config(config_path):
"""
Generates and saves a default configuration json file
"""
perms = 0o600
try:
if os.path.exists(config_path):
logger.warning('Another config exists at: "{}"'.format(config_path))
return False
with open(config_path, mode='w') as f:
f.writelines(json.dumps(default_config, indent=4))
f.close()
os.chmod(config_path, perms)
except IOError as e:
logger.critical('Failed to save configuration file "{}": {}'.format(config_path, e))
sys.exit(1)
except OSError as e:
logger.critical('Failed to set file permissions on configuration file "{}": {}'.format(config_path, e))
sys.exit(1)
logger.info('Successfully created configuration file "{}"'.format(config_path))
return True
def get_config(config_path):
if not config_path or not os.path.exists(config_path):
example_conf = json.dumps(default_config, indent=4)
logger.critical('Configuration file missing, please create one ("{}")\n'.format(config_path))
print('Example configuration:\n{}\nYou may also run with the "create_config" option to generate one\n'
.format(example_conf))
sys.exit(1)
try:
with open(config_path, 'r') as f:
config = json.loads(''.join(f.readlines()))
f.close()
except IOError as e:
logger.critical('Failed to open configuration file "{}": {}'.format(config_path, e))
sys.exit(1)
except ValueError as e:
logger.critical('Failed to parse configuration file "{}": {}'.format(config_path, e))
sys.exit(1)
# Must have the following values
for field in ['key', 'editor_path', 'timestamp_format', 'diary_base']:
if field not in config:
logger.critical('Configuration value "{}" missing from {}'.format(field, config_path))
sys.exit(1)
return config
def get_args():
parser = argparse.ArgumentParser(description="Appends time stamps to a text file named after the current day.",
formatter_class=argparse.RawTextHelpFormatter)
default_config_path = os.environ['HOME'] + '/.diary.conf.json'
parser.add_argument('--config', '-c', dest='config_path', type=str, default=default_config_path,
help='path to diary configuration json\ndefault: {}'.format(default_config_path))
parser.add_argument('--open', '-o', action="store_true", help="open file appending time stamp.")
commands_parser = parser.add_subparsers(dest='command', help='Sub commands')
commands_parser.add_parser('create_config', help='Generate default configuration file')
commands_parser.add_parser('version', help='Displays the version number')
commands_parser.add_parser('help', help='Displays full help message')
commands_parser.add_parser('b', help='Appends Begin and the timestamp to the EOF')
commands_parser.add_parser('s', help='Appends Stop and the timestamp to the EOF')
commands_parser.add_parser('m', help='Appends Mark and the timestamp to the EOF')
commands_parser.add_parser('o', help="Opens today's diary for editing")
commands_parser.add_parser('c', help='Open up the cli version of Diary')
timestamps_parser = commands_parser.add_parser('timestamps', help='Prints out timestamps from the previous diary entries')
timestamps_parser.add_argument('since_days_ago', nargs='?', default=14, help='How many days ago since the present to print timestamps from.')
return parser, parser.parse_args()
def main():
"""
Main application entry point
"""
parser, args= get_args()
if args.command == 'help':
parser.print_help()
return
if args.command == 'version':
print('{}: Version {}'.format(__app_name__, __version__))
# General configuration file
if args.command == 'create_config':
create_config(args.config_path)
return
config = get_config(args.config_path)
# ensure base path exists
ensure_base_path(config)
if args.command in actiondict.keys():
write_to_diary(config, args.command)
if args.open is True or args.command == "o":
open_diary(config)
if args.command == 'timestamps':
timestamps(config, int(args.since_days_ago))
if args.command == 'c':
try:
DiaryShell(config).cmdloop()
except KeyboardInterrupt:
bye()
if __name__ == '__main__':
main()