class Tasks (Visitor): def __init__(self, root_folder, root_context): Visitor.__init__(self) self.root_folder = root_folder self.root_context = root_context self.project = Project (name='Tasks') self.context = Context (name='Tasks') def end_project (self, item): for child in item.children: self.project.add_child(child) item.children = [] def end_folder (self, item): if item == self.root_folder: self.root_folder.children = [] self.root_folder.add_child(self.project) def end_context (self, item): if item != self.root_context: for child in item.children: if child.type == TASK: self.context.add_child(child) item.children = [] else: self.root_context.children = [] self.root_context.add_child(self.context) def __str__ (self): return 'Tasks'
class Tasks(Visitor): def __init__(self, root_folder, root_context): Visitor.__init__(self) self.root_folder = root_folder self.root_context = root_context self.project = Project(name="Tasks") self.context = Context(name="Tasks") def end_project(self, item): for child in item.children: self.project.add_child(child) item.children = [] def end_folder(self, item): if item == self.root_folder: self.root_folder.children = [] self.root_folder.add_child(self.project) def end_context(self, item): if item != self.root_context: for child in item.children: if child.type == TASK: self.context.add_child(child) item.children = [] else: self.root_context.children = [] self.root_context.add_child(self.context) def __str__(self): return "Tasks"
def test_ProjectNameFilterVisitor_exclude(self): n1 = Project(name=u'n1') n2 = Project(name=u'n2 xxx') nodes = [n1, n2] visitor = ProjectNameFilterVisitor('xxx', include=False) traverse_list(visitor, nodes) self.assertTrue(n1.marked) self.assertFalse(n2.marked)
def test_bug_2013_04_28(self): tue = datetime.strptime('Apr 9 2013 11:33PM', '%b %d %Y %I:%M%p') expr = parse_expr(tokenise("flagged or true"), now=tue)[0] self.assertTrue(expr(Project())) expr = parse_expr(tokenise("flagged or due=today"), now=tue)[0] self.assertFalse(expr(Project())) self.assertTrue(expr(Project(flagged=True))) self.assertTrue(expr(Project(date_due=tue)))
def test_ProjectNameFilterVisitor_include_ignores_children(self): n1 = Project(name=u'n1 xxx') n2 = Task(name=u'n2') n1.add_child(n2) visitor = ProjectNameFilterVisitor('xxx') traverse(visitor, n1) self.assertTrue(n1.marked) self.assertTrue(n2.marked)
def test_ProjectNameFilterVisitor_include_ignores_children (self): n1 = Project (name=u'n1 xxx') n2 = Task (name=u'n2') n1.add_child(n2) visitor = ProjectNameFilterVisitor ('xxx') traverse (visitor, n1) self.assertTrue(n1.marked) self.assertTrue(n2.marked)
def test_bug_2013_04_24(self): expr = parse_expr(tokenise('(type="Folder") and (name!="^Misc")'))[0] self.assertFalse(expr(Folder(name="Miscellaneous"))) expr = parse_expr(tokenise('(type="Project") and (name="Misc")'))[0] self.assertTrue(expr(Project(name="Miscellaneous"))) self.assertFalse(expr(Project(name="xxx"))) expr = parse_expr(tokenise('((type="Project") and (name="Misc"))'))[0] self.assertTrue(expr(Project(name="Miscellaneous"))) self.assertFalse(expr(Project(name="xxx"))) self.assertFalse(expr(Folder(name="Misc")))
def test_Sort_in_order(self): n1 = Task(name=u'a n1') n2 = Task(name=u'b n2') root = Project(name=u'r') root.add_child(n1) root.add_child(n2) visitor = Sort([PROJECT], lambda x: x.name, 'pretty') traverse(visitor, root) self.assertIs(root.children[0], n1) self.assertIs(root.children[1], n2)
def test_Sort_out_of__order (self): n1 = Task (name=u'b n1') n2 = Task (name=u'a n2') root = Project (name=u'r') root.add_child(n1) root.add_child(n2) visitor = Sort ([PROJECT], lambda x: x.name, 'pretty') traverse (visitor, root) self.assertIs(root.children[0], n2) self.assertIs(root.children[1], n1)
def test_Sort_same_use_underlying_order_out_of_order(self): n1 = Task(name=u'aaa', order=2) n2 = Task(name=u'aaa', order=1) root = Project(name=u'r') root.add_child(n1) root.add_child(n2) visitor = Sort([PROJECT], lambda x: x.name, 'pretty') traverse(visitor, root) self.assertIs(root.children[0], n2) self.assertIs(root.children[1], n1)
def test_Sort_same_use_underlying_order_out_of_order (self): n1 = Task (name=u'aaa', order=2) n2 = Task (name=u'aaa', order=1) root = Project (name=u'r') root.add_child(n1) root.add_child(n2) visitor = Sort ([PROJECT], lambda x: x.name, 'pretty') traverse (visitor, root) self.assertIs(root.children[0], n2) self.assertIs(root.children[1], n1)
def test_traverse_project_when_not_marked(self): parent = Project(name=u'p') n1 = Task(name=u'n1') n2 = Task(name=u'n2') parent.children.append(n1) parent.children.append(n2) parent.marked = False visitor = DemoVisitor() traverse(visitor, parent) self.assertEqual(0, len(visitor.tasks_started)) self.assertEqual(0, len(visitor.projects_started)) self.assertEqual(0, len(visitor.tasks_ended)) self.assertEqual(0, len(visitor.projects_ended))
def test_traverse_project_when_not_marked (self): parent = Project (name=u'p') n1 = Task (name=u'n1') n2 = Task (name=u'n2') parent.children.append (n1) parent.children.append (n2) parent.marked = False visitor = DemoVisitor () traverse (visitor, parent) self.assertEqual(0, len(visitor.tasks_started)) self.assertEqual(0, len(visitor.projects_started)) self.assertEqual(0, len(visitor.tasks_ended)) self.assertEqual(0, len(visitor.projects_ended))
def test_traverse_list_projects(self): n1 = Project(name=u'n1') n2 = Project(name=u'n2') nodes = [n1, n2] visitor = DemoVisitor() traverse_list(visitor, nodes) self.assertEqual(2, len(visitor.projects_started)) self.assertTrue(n1 in visitor.projects_started) self.assertTrue(n2 in visitor.projects_started) self.assertEqual(2, len(visitor.projects_ended)) self.assertTrue(n1 in visitor.projects_ended) self.assertTrue(n2 in visitor.projects_ended) self.assertEqual(0, len(visitor.tasks_started)) self.assertEqual(0, len(visitor.folders_started)) self.assertEqual(0, len(visitor.contexts_started))
def test_Scenario_4(self): ''' In context mode select a single deeply nested task for exclusion ''' f1 = Folder(name='1') p1 = Project(name='2', parent=f1) t1 = Task(name='3', parent=p1) t2_selected = Task(name='xxx', parent=t1) t3 = Task(name='5', parent=t2_selected) t4 = Task(name='3', parent=t1) f2 = Folder(name='1', parent=f1) p2 = Project(name='2', parent=f2) t5 = Task(name='3', parent=p2) t6 = Task(name='3', parent=t5) t7 = Task(name='5', parent=t6) c1_on_path = Context(name='6') c2_on_path = Context(name='6', parent=c1_on_path) c2_on_path.add_child(t2_selected) c3 = Context(name='7', parent=c2_on_path) traverse_list(Filter([PROJECT, CONTEXT, TASK, FOLDER], lambda x: match_name(x, 'xxx'), False, 'pretty'), [c1_on_path], project_mode=False) self.assertTrue(f1.marked) self.assertTrue(p1.marked) self.assertTrue(t1.marked) self.assertFalse(t2_selected.marked) self.assertTrue(t3.marked) self.assertTrue(t4.marked) self.assertTrue(t5.marked) self.assertTrue(t6.marked) self.assertTrue(t7.marked) self.assertTrue(f2.marked) self.assertTrue(p2.marked) self.assertTrue(t5.marked) self.assertTrue(t6.marked) self.assertTrue(t7.marked) self.assertTrue(c1_on_path.marked) self.assertTrue(c2_on_path.marked) self.assertTrue(c3.marked)
def test_Scenario_2(self): ''' In project mode select a single deeply nested task for exclusion ''' f_on_path = Folder(name='1') p_on_path = Project(name='2', parent=f_on_path) t1_on_path = Task(name='3', parent=p_on_path) t2_selected = Task(name='xxx', parent=t1_on_path) t3_on_path = Task(name='5', parent=t2_selected) t4 = Task(name='3', parent=t1_on_path) f = Folder(name='1', parent=f_on_path) p = Project(name='2', parent=f) t1 = Task(name='3', parent=p) t2 = Task(name='3', parent=t1) t3 = Task(name='5', parent=t2) c1 = Context(name='6') c1.add_child(t2_selected) c2 = Context(name='7') c2.add_child(t3) traverse_list( Filter([PROJECT, CONTEXT, TASK, FOLDER], lambda x: match_name(x, 'xxx'), False, 'pretty'), [f_on_path]) self.assertTrue(f_on_path.marked) self.assertTrue(p_on_path.marked) self.assertTrue(t1_on_path.marked) self.assertFalse(t2_selected.marked) self.assertFalse(t3_on_path.marked) self.assertTrue(c1.marked) self.assertTrue(c2.marked) self.assertTrue(f.marked) self.assertTrue(p.marked) self.assertTrue(t1.marked) self.assertTrue(t2.marked) self.assertTrue(t3.marked) self.assertTrue(t4.marked)
def test_project_add_child(self): project = Project() child = Task() project.add_child(child) self.assertEquals(project, child.parent) self.assertEquals(project, child.project) project = Project() child = Task() grandchild = Task() child.add_child(grandchild) project.add_child(child) self.assertEquals(project, child.parent) self.assertEquals(project, child.project) self.assertEquals(project, grandchild.project)
def test_task_add_child(self): project = Project() task = Task() child = Task() project.add_child(task) task.add_child(child) self.assertEquals(task, child.parent) self.assertEquals(project, child.project) project = Project() task = Task() child = Task() grandchild = Task() project.add_child(task) task.add_child(child) child.add_child(grandchild) task.add_child(child) self.assertEquals(task, child.parent) self.assertEquals(project, child.project) self.assertEquals(child, grandchild.parent) self.assertEquals(project, grandchild.project)
def test_traverse_folder(self): parent = Folder(name=u'p') n1 = Project(name=u'n1') n2 = Folder(name=u'n2') parent.children.append(n1) parent.children.append(n2) visitor = DemoVisitor() traverse(visitor, parent) self.assertEqual(2, len(visitor.folders_started)) self.assertEqual(1, len(visitor.projects_started)) self.assertTrue(parent in visitor.folders_started) self.assertTrue(n1 in visitor.projects_started) self.assertTrue(n2 in visitor.folders_started) self.assertEqual(2, len(visitor.folders_ended)) self.assertEqual(1, len(visitor.projects_ended)) self.assertTrue(parent in visitor.folders_ended) self.assertTrue(n1 in visitor.projects_ended) self.assertTrue(n2 in visitor.folders_ended)
def test_project_add_child (self): project = Project () child = Task () project.add_child (child) self.assertEquals(project, child.parent) self.assertEquals(project, child.project) project = Project () child = Task () grandchild = Task () child.add_child (grandchild) project.add_child (child) self.assertEquals(project, child.parent) self.assertEquals(project, child.project) self.assertEquals(project, grandchild.project)
def build_model(db): conn = sqlite3.connect(db) contexts = query(conn, clazz=OFContext) no_context = Context(name='No Context') inbox = Project(name='Inbox') project_infos = query(conn, clazz=ProjectInfo) folders = query(conn, clazz=OFFolder) tasks = query(conn, clazz=OFTask) projects = transmute_projects(project_infos, tasks) wire_projects_and_folders(projects, folders, tasks) wire_task_hierarchy(tasks) wire_tasks_to_enclosing_projects(project_infos, tasks, inbox) wire_tasks_and_contexts(contexts, tasks, no_context) wire_folder_hierarchy(folders) wire_context_hierarchy(contexts) conn.close() # Find top level items project_roots = only_roots(projects.values()) folder_roots = only_roots(folders.values()) root_projects_and_folders = project_roots + folder_roots root_contexts = only_roots(contexts.values()) root_contexts.insert(0, no_context) root_projects_and_folders.insert(0, inbox) sort(root_projects_and_folders) sort(root_contexts) root_folder = Folder(name='') for child in root_projects_and_folders: root_folder.add_child(child) root_context = Context(name='', status='active') for child in root_contexts: root_context.add_child(child) return root_folder, root_context
def load_from_json(json_data, item_db): if 'ref' in json_data: item = item_db[json_data['ref']] return item item_type = json_data['type'] item_id = json_data['id'] if item_type == FOLDER: item = Folder() elif item_type == CONTEXT: item = Context() elif item_type == TASK: item = Task() elif item_type == PROJECT: item = Project() load_attrib(item, 'id', json_data, lambda x: x) load_attrib(item, 'link', json_data, lambda x: x) load_attrib(item, 'status', json_data, lambda x: x) load_attrib(item, 'name', json_data, lambda x: x) load_attrib(item, 'date_completed', json_data, lambda x: datetime.strptime(x, TIME_FMT)) load_attrib(item, 'date_to_start', json_data, lambda x: datetime.strptime(x, TIME_FMT)) load_attrib(item, 'date_due', json_data, lambda x: datetime.strptime(x, TIME_FMT)) load_attrib(item, 'flagged', json_data, lambda x: x) load_attrib(item, 'next', json_data, lambda x: x) load_attrib(item, 'note', json_data, lambda x: JSONNote(x)) load_attrib(item, 'order', json_data, lambda x: x) for child_data in json_data['children']: child = load_from_json(child_data, item_db) item.add_child(child) item_db[item_id] = item return item
def test_task_add_child (self): project = Project () task = Task () child = Task () project.add_child (task) task.add_child (child) self.assertEquals(task, child.parent) self.assertEquals(project, child.project) project = Project () task = Task () child = Task () grandchild = Task () project.add_child (task) task.add_child (child) child.add_child (grandchild) task.add_child (child) self.assertEquals(task, child.parent) self.assertEquals(project, child.project) self.assertEquals(child, grandchild.parent) self.assertEquals(project, grandchild.project)
def __init__(self, root_folder, root_context): Visitor.__init__(self) self.root_folder = root_folder self.root_context = root_context self.project = Project (name='Tasks') self.context = Context (name='Tasks')
def test_parse_expr_status(self): expr = parse_expr(tokenise('status="done"'))[0] self.assertTrue(expr(Project(status="done"))) self.assertFalse(expr(Project(status="active")))
def __init__(self, root_folder, root_context): Visitor.__init__(self) self.root_folder = root_folder self.root_context = root_context self.project = Project(name="Tasks") self.context = Context(name="Tasks")
def test_parse_expr_type(self): expr = parse_expr(tokenise('type="Task"'))[0] self.assertTrue(expr(Task(flagged=True))) self.assertFalse(expr(Project(flagged=True)))