def __init__(self, base, url, countername='todo'): self._base = base self._url = url self._todos = self._base.get(url, None) self._countername = countername self._counter = Counter(countername, self._base) self._structure = self._make_folder_structure(self._todos) self._schedule = Schedule(self._base, self.fetch_todos())
# Add 1 for the sleep at the end # FIXME Set this value, and all values, to sensible defaults and live config sched_buff = config.conf['SCHEDULE_BUFFER'] + 1 last_run = time() while True: debug("main.py ---------------", level=1) debug("main.py loop start", level=1) now = time() debug("main.py last_run: " + repr(last_run), level=1) debug("main.py now: " + repr(last_run), level=1) debug("main.py sched_buff: " + repr(sched_buff), level=1) debug("main.py last_run + sched_buff: " + repr(last_run + sched_buff), level=1) debug("main.py now > (last_run + sched_buff): " + repr(now > (last_run + sched_buff)), level=1) if now > (last_run + sched_buff): # FIXME Constantly running. May be because too short sched_buff. last_run = time() updates() debug("main.py schedule.run()", level=1) schedule = Schedule(system.attached_devices) schedule.run() # FIXME Do I have a race condition where I have an event inside this sleep? # FIXME Do I want no sleep? More? Check CPU usage. sleep(5) debug("main.py loop start", level=1)
def main(): parser = argparse.ArgumentParser( description='A Todo List Manager in your command line') # get argument parser.add_argument('-g', '--get', nargs='?', metavar="task", action='append') # filter by tag argument parser.add_argument('-t', '--tag', nargs="+", metavar='tag') # filter by folder argument parser.add_argument('-f', '--folder', nargs='?', metavar='folder', action='append') # add todo argument parser.add_argument('-a', '--add', nargs='*') # done todo argument for marking something done parser.add_argument('-d', '--done', nargs='+', metavar='task') # removing a todo item parser.add_argument('--delete', nargs='+', metavar='task') # dashboard argument/ user profile. parser.add_argument('-m', '--me', action='store_true') # dynamic argument parser.add_argument('-e', '--edit', nargs=1, metavar=('task')) # Version Info parser.add_argument('-V', '--version', action='store_true') # Add an optional rewards optional parser.add_argument('-r', '--reward', action='store_true') # Add Redeem optional argument parser.add_argument('-x', '--redeem', nargs='+', metavar=('reward', 'times')) parser.add_argument('--structure', action='store_true') parser.add_argument('--timer', action='store_true') parser.add_argument('--reset', action='store_true') parser.add_argument('--snooze', nargs=2, metavar=('task', 'snooze')) parser.add_argument('--schedule', action='store_true') parser.add_argument('--editinfo', action='store_true') parser.add_argument('--sync', action='store_true') args = parser.parse_args() if not "TODO_COUNTER_CODE" in CONFIG.keys(): CONFIG["TODO_COUNTER_CODE"] = todobase.get_unique_counter_code() config.save_config(CONFIG, config_file) if not "REWARD_COUNTER_CODE" in CONFIG.keys(): CONFIG["REWARD_COUNTER_CODE"] = todobase.get_unique_counter_code() config.save_config(CONFIG, config_file) todos = TodoCollection(todobase, '/todos', CONFIG["TODO_COUNTER_CODE"]) rewards = Rewards(todobase, '/rewards', CONFIG["REWARD_COUNTER_CODE"]) editor = TodoEditor(todos) user = Profile(todobase, '/profile', todos, rewards) print(args) if args.folder and args.folder[0] is None: args.folder = ['MAIN'] if isinstance(args.add, list): if args.reward: # Add an reward if len(args.add) == 2: reward = str(args.add[0]) bounty = int(args.add[1]) rewards.add(reward, bounty) else: if len(args.add) == 0: editor.addflow() elif len(args.add) == 2: task = str(args.add[0]) # task bounty = int(args.add[1]) todos.add(task, bounty) elif args.edit: if args.folder: # Editing Folder is equivalent to moving between folders. todos.move_to_folder(args.folder[0], args.edit[0]) elif args.timer and args.reset: todos.start_time_for_todo(args.edit[0], reset=True) elif args.timer: todos.start_time_for_todo(args.edit[0]) elif args.reset: todos.reset_timer_for_todo(args.edit[0]) else: editor.editflow(args.edit[0]) elif args.snooze: todos.handle_snooze(args.snooze[0], args.snooze[1]) elif args.get is not None: if args.reward: if args.get[0] is None: rewards.get_all() else: rewards.get(args.get[0]) else: if args.folder: todos.get_by_folder(args.folder[0]) elif args.tag: if args.folder: todos.get_by_folder(args.folder[0], args.tag) else: todos.get_all(tags=args.tag) elif args.get[0] is None: todos.get_all() else: todos.get(args.get[0]) elif args.done: if isinstance(args.done, str): todos.markdone(args.done) elif isinstance(args.done, list): for task in args.done: todos.markdone(task) elif args.delete: if args.reward: if isinstance(args.delete, str): rewards.delete(args.delete) elif isinstance(args.delete, list): for task in args.delete: rewards.delete(task) elif args.folder: if len(args.delete) == 1: todos.delete_folder(args.delete[0]) else: print("Cannot delete multiple Folders at once.\ \nDelete one by one.") else: if isinstance(args.delete, str): todos.delete(args.delete) elif isinstance(args.delete, list): for task in args.delete: todos.delete(task) elif args.redeem: if args.reward: name = args.redeem[0] if len(args.redeem) == 2: times = int(args.redeem[1]) else: times = 1 rewards.redeem(name, times) elif args.me: user.update() if args.editinfo: user.update_info_flow() else: user.show_profile() elif args.version: puts("Version: " + _version) APP_COPYRIGHT = colored.green((u'\u00a9').encode('utf-8') + " 2015 Inderjit Sidhu & Airbase IO") puts(APP_COPYRIGHT) elif args.structure: structure = todos.fetch_structure() print(structure) elif args.schedule: schedule = Schedule(todobase, todos.fetch_todos()) schedule.create_dynamic_folder_structure() schedule.print_schedule() todobase.save() if args.sync: todobase.sync()
class TodoCollection(object): def __init__(self, base, url, countername='todo'): self._base = base self._url = url self._todos = self._base.get(url, None) self._countername = countername self._counter = Counter(countername, self._base) self._structure = self._make_folder_structure(self._todos) self._schedule = Schedule(self._base, self.fetch_todos()) def _update(self): self._todos = self._base.get(self._url, None) def fetch_schedule(self): return self._schedule.create_dynamic_folder_structure() def fetch_structure(self): return self._structure def fetch_todos(self): return {key: self._todos[foldername][key] for key, foldername in self._structure.items()} def fetch_folderwise_todos(self): return self._todos def fetch_tagwise_todos(self): return self._todos_by_tag() def get_all(self, tags=None): todos = {key: self._todos[foldername][key] for key, foldername in self._structure.items()} if tags: todos = {key: todos[key] for key in todos.keys() if self._is_tagged(key, tags)} foreach(todos, print_todo_item) def get(self, name): """ get foldername from name and then look for todo with the given name inside the folder. """ foldername = self._structure[name] print_todo_item(self._todos[foldername][name], name) def get_by_folder(self, foldername, tags=None): todos = self._todos[foldername] if tags: todos = {key: todos[key] for key in todos.keys() if self._is_tagged(key, tags)} foreach(todos, print_todo_item) def add(self, task, bounty, duetime=None, tags=[], folder=None): assert isinstance(task, str) assert len(task) > 0, "Task Cannot be left empty." assert isinstance(bounty, int) assert isinstance(tags, list) N = self._counter.get() name = self._countername + str(N+1) todo = {'task': task, 'bounty': bounty, 'done': False} if duetime is not None: todo['due'] = duetime.strftime(DATE_FORMAT) if tags != []: todo['tags'] = tags if not folder or folder ==' ': folder = 'MAIN' #Our default folder, uppercase to be unique res = self._base.put(self._url + "/" + folder, name, todo) print(res) self._update() def markdone(self, name): self.edit(name, {'done': True}) def edit(self, name, newtodo): # TODO: Need to find a better way to print responses. if name in self._structure.keys(): foldername = self._structure[name] if 'task' in newtodo.keys(): assert newtodo not in ['', ' '], "Task Cannot be left empty." if 'due' in newtodo.keys(): newtodo['due'] = newtodo["due"].strftime(DATE_FORMAT) res = self._base.patch(self._url + "/" + foldername + "/" + name , newtodo) print(res) else: print("Not Found") self._update() def delete(self, name): if name in self._structure.keys(): foldername = self._structure[name] res = self._base.delete(self._url + '/' + foldername, name) print(res) else: print("Not Found. Can't Delete") self._update() def move_to_folder(self, foldername, key): src_foldername = self._structure[key] if src_foldername != foldername: todo = self._todos[src_foldername][key] self.delete(key) res = self._base.put(self._url + "/" + foldername, key, todo) print(res) self._structure[key] = foldername def delete_folder(self, foldername): if foldername == 'MAIN': print("Cannot delete default folder.") elif foldername in self._todos.keys(): keys_to_delete = [] for key, value in self._structure.items(): if value == foldername: keys_to_delete.append(key) for key in keys_to_delete: del self._structure[key] res = self._base.delete(self._url, foldername) print(res) else: print("Folder not found") def _is_tagged(self, key, tags): try: foldername = self._structure[key] item_tags = self._todos[foldername][key]['tags'] except KeyError: item_tags = [] return any(tag in item_tags for tag in tags) def _make_folder_structure(self, todos): res_structure = dict() for foldername in todos.keys(): folder = todos[foldername] for name in folder.keys(): res_structure[name] = foldername return res_structure def start_time_for_todo(self, name, reset=False): if name in self._structure.keys(): folder = self._structure[name] todo = self._todos[folder][name] if 'time_taken' in todo.keys() and not(reset): timer = Timer(todo['done'], todo['time_taken']) else: timer = Timer(todo['done']) end, done = timer.print_elapsed() newtodo = {'time_taken': end} if done is not None: newtodo['done'] = done self.edit(name, newtodo) else: print("This todo doesn't exist") def reset_timer_for_todo(self, name): if name in self._structure.keys(): folder = self._structure[name] todo = self._todos[folder][name] if 'time_taken' in todo.keys(): newtodo = {'time_taken': '0:00:00'} self.edit(name, newtodo) def handle_snooze(self, name, snooze_string): try: if name in self._structure.keys(): folder = self._structure[name] todo = self._todos[folder][name] if 'due' in todo.keys(): snooze = parse_duestring(snooze_string, todo['due']) else: snooze = parse_duestring(snooze_string) self.edit(name, {'due': snooze.strftime(DATE_FORMAT)}) else: print(colored.red("This todo doesn't exist. Please check the spelling.")) except ValueError: ValueError(colored.red("Bad Snooze Fomat: Expected Formats (due_rules or YYYY-MM-DD)")) def _todos_by_tag(self): result = {} for key in self._structure.keys(): foldername = self._structure[key] todo = self._todos[foldername][key] if 'tags' in todo.keys(): for tag in todo['tags']: if tag in result.keys(): result[tag][key] = todo else: result[tag] = {key: todo} return result
def main(): parser = argparse.ArgumentParser(description='A Todo List Manager in your command line') # get argument parser.add_argument('-g','--get', nargs='?', metavar="task", action='append') # filter by tag argument parser.add_argument('-t', '--tag', nargs="+", metavar='tag') # filter by folder argument parser.add_argument('-f', '--folder', nargs='?', metavar='folder', action='append') # add todo argument parser.add_argument('-a', '--add', nargs='*') # done todo argument for marking something done parser.add_argument('-d', '--done', nargs='+', metavar='task') # removing a todo item parser.add_argument('--delete', nargs='+', metavar='task') # dashboard argument/ user profile. parser.add_argument('-m', '--me', action='store_true') # dynamic argument parser.add_argument('-e', '--edit', nargs=1, metavar=('task') ) # Version Info parser.add_argument('-V', '--version', action='store_true') # Add an optional rewards optional parser.add_argument('-r','--reward', action='store_true') # Add Redeem optional argument parser.add_argument('-x','--redeem', nargs='+', metavar=('reward', 'times')) parser.add_argument('--structure', action='store_true') parser.add_argument('--timer', action='store_true') parser.add_argument('--reset', action='store_true') parser.add_argument('--snooze', nargs=2, metavar=('task', 'snooze')) parser.add_argument('--schedule', action='store_true') parser.add_argument('--editinfo', action='store_true') parser.add_argument('--sync', action='store_true') args = parser.parse_args() if not "TODO_COUNTER_CODE" in CONFIG.keys(): CONFIG["TODO_COUNTER_CODE"] = todobase.get_unique_counter_code() config.save_config(CONFIG, config_file) if not "REWARD_COUNTER_CODE" in CONFIG.keys(): CONFIG["REWARD_COUNTER_CODE"] = todobase.get_unique_counter_code() config.save_config(CONFIG, config_file) todos = TodoCollection(todobase, '/todos', CONFIG["TODO_COUNTER_CODE"]) rewards = Rewards(todobase, '/rewards', CONFIG["REWARD_COUNTER_CODE"]) editor = TodoEditor(todos) user = Profile(todobase, '/profile', todos, rewards) print(args) if args.folder and args.folder[0] is None: args.folder = ['MAIN'] if isinstance(args.add, list): if args.reward: # Add an reward if len(args.add) == 2: reward = str(args.add[0]) bounty = int(args.add[1]) rewards.add(reward, bounty) else: if len(args.add) == 0: editor.addflow() elif len(args.add) == 2: task = str(args.add[0]) # task bounty = int(args.add[1]) todos.add(task, bounty) elif args.edit: if args.folder: # Editing Folder is equivalent to moving between folders. todos.move_to_folder(args.folder[0], args.edit[0]) elif args.timer and args.reset: todos.start_time_for_todo(args.edit[0], reset=True) elif args.timer: todos.start_time_for_todo(args.edit[0]) elif args.reset: todos.reset_timer_for_todo(args.edit[0]) else: editor.editflow(args.edit[0]) elif args.snooze: todos.handle_snooze(args.snooze[0], args.snooze[1]) elif args.get is not None: if args.reward: if args.get[0] is None: rewards.get_all() else: rewards.get(args.get[0]) else: if args.folder: todos.get_by_folder(args.folder[0]) elif args.tag: if args.folder: todos.get_by_folder(args.folder[0], args.tag) else: todos.get_all(tags=args.tag) elif args.get[0] is None: todos.get_all() else: todos.get(args.get[0]) elif args.done: if isinstance(args.done, str): todos.markdone(args.done) elif isinstance(args.done, list): for task in args.done: todos.markdone(task) elif args.delete: if args.reward: if isinstance(args.delete, str): rewards.delete(args.delete) elif isinstance(args.delete, list): for task in args.delete: rewards.delete(task) elif args.folder: if len(args.delete) == 1: todos.delete_folder(args.delete[0]) else: print( "Cannot delete multiple Folders at once.\ \nDelete one by one.") else: if isinstance(args.delete, str): todos.delete(args.delete) elif isinstance(args.delete, list): for task in args.delete: todos.delete(task) elif args.redeem: if args.reward: name = args.redeem[0] if len(args.redeem) == 2: times = int(args.redeem[1]) else: times = 1 rewards.redeem(name, times) elif args.me: user.update() if args.editinfo: user.update_info_flow() else: user.show_profile() elif args.version: puts( "Version: " + _version) APP_COPYRIGHT = colored.green((u'\u00a9').encode('utf-8') + " 2015 Inderjit Sidhu & Airbase IO") puts(APP_COPYRIGHT) elif args.structure: structure = todos.fetch_structure() print(structure) elif args.schedule: schedule = Schedule(todobase, todos.fetch_todos()) schedule.create_dynamic_folder_structure() schedule.print_schedule() todobase.save() if args.sync: todobase.sync()
class TodoCollection(object): def __init__(self, base, url, countername='todo'): self._base = base self._url = url self._todos = self._base.get(url, None) self._countername = countername self._counter = Counter(countername, self._base) self._structure = self._make_folder_structure(self._todos) self._schedule = Schedule(self._base, self.fetch_todos()) def _update(self): self._todos = self._base.get(self._url, None) def fetch_schedule(self): return self._schedule.create_dynamic_folder_structure() def fetch_structure(self): return self._structure def fetch_todos(self): return { key: self._todos[foldername][key] for key, foldername in self._structure.items() } def fetch_folderwise_todos(self): return self._todos def fetch_tagwise_todos(self): return self._todos_by_tag() def get_all(self, tags=None): todos = { key: self._todos[foldername][key] for key, foldername in self._structure.items() } if tags: todos = { key: todos[key] for key in todos.keys() if self._is_tagged(key, tags) } foreach(todos, print_todo_item) def get(self, name): """ get foldername from name and then look for todo with the given name inside the folder. """ foldername = self._structure[name] print_todo_item(self._todos[foldername][name], name) def get_by_folder(self, foldername, tags=None): todos = self._todos[foldername] if tags: todos = { key: todos[key] for key in todos.keys() if self._is_tagged(key, tags) } foreach(todos, print_todo_item) def add(self, task, bounty, duetime=None, tags=[], folder=None): assert isinstance(task, str) assert len(task) > 0, "Task Cannot be left empty." assert isinstance(bounty, int) assert isinstance(tags, list) N = self._counter.get() name = self._countername + str(N + 1) todo = {'task': task, 'bounty': bounty, 'done': False} if duetime is not None: todo['due'] = duetime.strftime(DATE_FORMAT) if tags != []: todo['tags'] = tags if not folder or folder == ' ': folder = 'MAIN' #Our default folder, uppercase to be unique res = self._base.put(self._url + "/" + folder, name, todo) print(res) self._update() def markdone(self, name): self.edit(name, {'done': True}) def edit(self, name, newtodo): # TODO: Need to find a better way to print responses. if name in self._structure.keys(): foldername = self._structure[name] if 'task' in newtodo.keys(): assert newtodo not in ['', ' '], "Task Cannot be left empty." if 'due' in newtodo.keys(): newtodo['due'] = newtodo["due"].strftime(DATE_FORMAT) res = self._base.patch(self._url + "/" + foldername + "/" + name, newtodo) print(res) else: print("Not Found") self._update() def delete(self, name): if name in self._structure.keys(): foldername = self._structure[name] res = self._base.delete(self._url + '/' + foldername, name) print(res) else: print("Not Found. Can't Delete") self._update() def move_to_folder(self, foldername, key): src_foldername = self._structure[key] if src_foldername != foldername: todo = self._todos[src_foldername][key] self.delete(key) res = self._base.put(self._url + "/" + foldername, key, todo) print(res) self._structure[key] = foldername def delete_folder(self, foldername): if foldername == 'MAIN': print("Cannot delete default folder.") elif foldername in self._todos.keys(): keys_to_delete = [] for key, value in self._structure.items(): if value == foldername: keys_to_delete.append(key) for key in keys_to_delete: del self._structure[key] res = self._base.delete(self._url, foldername) print(res) else: print("Folder not found") def _is_tagged(self, key, tags): try: foldername = self._structure[key] item_tags = self._todos[foldername][key]['tags'] except KeyError: item_tags = [] return any(tag in item_tags for tag in tags) def _make_folder_structure(self, todos): res_structure = dict() for foldername in todos.keys(): folder = todos[foldername] for name in folder.keys(): res_structure[name] = foldername return res_structure def start_time_for_todo(self, name, reset=False): if name in self._structure.keys(): folder = self._structure[name] todo = self._todos[folder][name] if 'time_taken' in todo.keys() and not (reset): timer = Timer(todo['done'], todo['time_taken']) else: timer = Timer(todo['done']) end, done = timer.print_elapsed() newtodo = {'time_taken': end} if done is not None: newtodo['done'] = done self.edit(name, newtodo) else: print("This todo doesn't exist") def reset_timer_for_todo(self, name): if name in self._structure.keys(): folder = self._structure[name] todo = self._todos[folder][name] if 'time_taken' in todo.keys(): newtodo = {'time_taken': '0:00:00'} self.edit(name, newtodo) def handle_snooze(self, name, snooze_string): try: if name in self._structure.keys(): folder = self._structure[name] todo = self._todos[folder][name] if 'due' in todo.keys(): snooze = parse_duestring(snooze_string, todo['due']) else: snooze = parse_duestring(snooze_string) self.edit(name, {'due': snooze.strftime(DATE_FORMAT)}) else: print( colored.red( "This todo doesn't exist. Please check the spelling.")) except ValueError: ValueError( colored.red( "Bad Snooze Fomat: Expected Formats (due_rules or YYYY-MM-DD)" )) def _todos_by_tag(self): result = {} for key in self._structure.keys(): foldername = self._structure[key] todo = self._todos[foldername][key] if 'tags' in todo.keys(): for tag in todo['tags']: if tag in result.keys(): result[tag][key] = todo else: result[tag] = {key: todo} return result