Created
September 21, 2020 04:30
-
-
Save ZoomTen/50353ecf175819908358bb5f1ad28d36 to your computer and use it in GitHub Desktop.
lol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import json | |
import re | |
from argparse import ArgumentParser | |
from datetime import datetime | |
ACTIVITY_FILE = 'activity.json' | |
def make_activity(args): | |
try: | |
new_index = activities[-1]['activity']['index']+1 | |
except: | |
new_index = 0 | |
log_string = args.string | |
new_activity = { | |
'activity':{ | |
'index':new_index, | |
'description':log_string, | |
'time': datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
} | |
} | |
return new_activity | |
def add_todo(args, todolist): | |
try: | |
new_index = todolist[-1]['todo']['index']+1 | |
except: | |
new_index = 0 | |
log_string = args.string | |
new_activity = { | |
'todo':{ | |
'index':new_index, | |
'description':log_string | |
} | |
} | |
todolist.append(new_activity) | |
print('Add to-do: {}'.format(log_string)) | |
def list_activities(activities): | |
for activity in activities: | |
a = activity['activity'] | |
t = activity['links'] | |
print('A#{:04d} ({}): {}'.format(a['index'], a['time'], a['description'])) | |
for item, number in t.items(): | |
print(' (marks {} #{} as done)'.format(item,number)) | |
def list_todo(activities, todolist): | |
for todo in todolist: | |
done = None | |
a = todo['todo'] | |
for activity in activities: | |
x = activity['activity'] | |
t = activity['links'] | |
for item, number in t.items(): | |
if item == 'todo': | |
if int(number) == int(a['index']): | |
done = int(x['index']) | |
if done == None: | |
print('T#{:04d}: {}'.format(a['index'], a['description'])) | |
else: | |
print('T#{:04d}: {} (done by A#{:04d})'.format(a['index'], a['description'], done)) | |
if __name__ == '__main__': | |
if not os.path.exists(ACTIVITY_FILE): | |
# create defs | |
activities = [] | |
todolist = [] | |
else: | |
with open(ACTIVITY_FILE, 'r') as activity_file: | |
j = json.load(activity_file) | |
activities = j['activities'] | |
todolist = j['todolist'] | |
ap = ArgumentParser(description='JSON-based minimal activity logger') | |
sp = ap.add_subparsers(dest='action', help='sub-command help', required=True) | |
sp_ = sp.add_parser('log', help="Log an activity.", description="Log an activity.") | |
sp_.add_argument('string') | |
sp_ = sp.add_parser('todo', help="Add a to-do.", description="Add a to-do.") | |
sp_.add_argument('string') | |
sp_ = sp.add_parser('list', help="View activities.", description="View activities.") | |
sp_ = sp.add_parser('todolist', help="View todo list.", description="View todo list.") | |
args = ap.parse_args() | |
def scan_todo(match, has_match): | |
num = match.groups()[0] | |
is_valid = False | |
for i in todolist: | |
index = i['todo']['index'] | |
if int(index) == int(num): | |
is_valid = True | |
has_match[0] = True | |
has_match[1] = int(num) | |
return '' if is_valid else match.group() | |
if args.action == 'log': | |
new_activity = make_activity(args) | |
has_match = [False, 0] | |
new_activity['activity']['description'] = re.sub('\[todo#(\d+)\]', lambda x: scan_todo(x, has_match), new_activity['activity']['description']).strip() | |
activities.append(new_activity) | |
if (has_match[0]): | |
new_activity['links'] = {'todo': has_match[1]} | |
print('Done todo #{}'.format(has_match[1])) | |
else: | |
new_activity['links'] = {} | |
print('Log activity: {}'.format(new_activity['activity']['description'])) | |
elif args.action == 'todo': | |
add_todo(args, todolist) | |
elif args.action == 'list': | |
list_activities(activities) | |
elif args.action == 'todolist': | |
list_todo(activities, todolist) | |
# create new object and write it down | |
a_log = { # hi | |
'activities': activities, | |
'todolist': todolist | |
} | |
with open(ACTIVITY_FILE, 'w') as activity_file: | |
json.dump(a_log, activity_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment