def cmd_clear(settings): """ Drop all tables and re-create. """ sa = get_session(settings.dbref, metadata=metadata) for name, table in metadata.tables.items(): print(table.delete()) sa.execute(table.delete()) sa.commit() sa = get_session(settings.dbref, initialize=True, metadata=metadata)
def cmd_find(settings, LIKE): """ Look for tag. """ sa = get_session(settings.dbref, metadata=metadata) alikes = sa.query(Tag).filter(Tag.name.like(LIKE)).all() for tag in alikes: print(tag.name)
def cmd_list(settings): """ List to root tags. """ sa = get_session(settings.dbref, metadata=metadata) roots = sa.query(Tag).filter(Tag.contexts == None).all() for root in roots: print(root.name)
def cmd_record(settings, opts, TAGS): """ Record tags/paths. Report on inconsistencies. """ sa = get_session(settings.dbref, initialize=True, metadata=metadata) assert TAGS # TODO: read from stdin for raw_tag in TAGS: Tag.record(raw_tag, sa, opts)
def cmd_insert(title, before_ID): """ todo [options] insert <title-or-ID> """ sa = get_session(settings.dbref, metadata=SqlBase.metadata) before = Task.fetch(((Task.task_id == before_ID[0]),), sa=sa) task = Task(title=title, prerequisite_id=before.task_id) sa.add(task) sa.commit()
def cmd_ungroup(ID, settings): """ todo ungroup ID """ sa = get_session(settings.dbref, metadata=SqlBase.metadata) for id_ in ID: node = Task.byKey(dict(task_id=id_), sa=sa) node.partOf_id = None sa.add(node) sa.commit()
def cmd_rm(ID, settings): """ todo rm ID... Delete tasks. """ sa = get_session(settings.dbref, metadata=SqlBase.metadata) for id_ in ID: task = Task.fetch(((Task.task_id == id_),), sa=sa) sa.delete(task) sa.commit() print_Task(task)
def cmd_update(ID, title, description, group, opts, settings): sa = get_session(settings.dbref, metadata=SqlBase.metadata) task = Task.find(( Task.title == title, ), sa=sa) if group: group = Task.find(( Task.title == group, ), sa=sa) assert group, opts.args.group # TODO: title, description # TODO: prerequisites... # requiredFor... # partOf # subtasks... pass
def cmd_import(input, settings): """ todo import -i FILE """ input = input or settings.input assert input, "Missing input file." sa = get_session(settings.dbref, metadata=SqlBase.metadata) json = js.load(open(input, 'r')) assert json['version'] == __version__, json['version'] for t in json['tasks']: task = Task(**t) sa.add(task) sa.commit()
def cmd_export(output, settings): """ todo export -o FILE """ output = output or settings.output assert settings.output, "Missing output file." sa = get_session(settings.dbref, metadata=SqlBase.metadata) tasks = sa.query(Task).all() data = { 'version': __version__, 'tasks': [ t.copy(True) for t in tasks ] } js.dump(data, open(settings.output, 'w+'))
def cmd_new(title, description, group, opts, settings): """ todo [options] new <title> <description> <group> """ sa = get_session(settings.dbref) if group: group = Task.find(( Task.title == group, ), sa=sa) assert group, opts.args.group task = Task( title = title, description = description, partOf = group ) sa.add(task) sa.commit() print_Task(task)
def cmd_depends(ID, DEPENDENCIES, settings): """ todo ID requires DEPENDENCIES... """ sa = get_session(settings.dbref, metadata=SqlBase.metadata) node = Task.byKey(dict(task_id=ID), sa=sa) for dep_ID in DEPENDENCIES: dep = Task.byKey(dict(task_id=dep_ID), sa=sa) dep.requiredFor_id = ID sa.add(dep) ID = dep_ID else: dep = Task.byKey(dict(requiredFor_id=ID), sa=sa) dep.requiredFor_id = None sa.add(dep) sa.commit()
def cmd_prerequisite(ID, PREREQUISITES, settings): """ todo ID depends PREREQUISITES... (Re)Set ID to depend on first prerequisite ID. If more prerequisites are given, apply the same to every following ID in sequence. This sets an unqualified dependency. TODO: check level """ sa = get_session(settings.dbref, metadata=SqlBase.metadata) node = Task.byKey(dict(task_id=ID), sa=sa) for prereq_ID in PREREQUISITES: node.requiredFor_id = prereq_ID sa.add(node) node = Task.byKey(dict(task_id=prereq_ID), sa=sa) else: node.requiredFor_id = None sa.add(node) sa.commit()
def cmd_info(settings): """ Verify DB connection is working. Print some settings and storage stats. """ for l, v in ( ( 'Settings Raw', pformat(settings.todict()) ), ( 'DBRef', settings.dbref ), ( "Tables in schema", ", ".join(metadata.tables.keys()) ), ( "Table lengths", "" ), ): log.std('{green}%s{default}: {bwhite}%s{default}', l, v) sa = get_session(settings.dbref, metadata=metadata) for t in metadata.tables: try: log.std(" {blue}%s{default}: {bwhite}%s{default}", t, sa.query(metadata.tables[t].count()).all()[0][0]) except Exception, e: log.err("Count failed for %s: %s", t, e)
def cmd_list(settings): sa = get_session(settings.dbref) roots = sa.query(Task).filter(Task.partOf_id == None).all() indented_tasks('', sa, settings, roots)
def cmd_find(title, settings): sa = get_session(settings.dbref) task = Task.find(( Task.title == title, ), sa=sa) print_Task(task)
def cmd_init(settings): """ Commit SQL DDL to storage schema. Creates DB file if not present. """ sa = get_session(settings.dbref, initialize=True, metadata=metadata)