<s> import os <EOL> import sys <EOL> try : <EOL> from elixir import metadata , setup_all , create_all , session <EOL> from models import * <EOL> from sqlalchemy . sql import not_ <EOL> except ImportError : <EOL> """<STR_LIT>""" <EOL> from utils import get_or_create , make_list <EOL> from pdb import set_trace <EOL> from exceptions import * <EOL> __version__ = '<STR_LIT>' <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class GTD ( object ) : <EOL> def __init__ ( self , filename ) : <EOL> db_url = '<STR_LIT>' + filename <EOL> metadata . bind = db_url <EOL> setup_all ( ) <EOL> if not os . path . exists ( filename ) : <EOL> create_all ( ) <EOL> def addTask ( self , title , note = None , tags = [ ] , priority = <NUM_LIT:1> ) : <EOL> task = Task ( <EOL> title = title , <EOL> note = note , <EOL> tags = self . _createTags ( tags ) , <EOL> priority = priority ) <EOL> session . commit ( ) <EOL> return task <EOL> def getTaskById ( self , task_id ) : <EOL> return Task . query . get ( task_id ) <EOL> def getTasks ( self , tags = [ ] , without_tags = [ ] , show = '<STR_LIT>' ) : <EOL> assert ( show in ( '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> query = Task . query <EOL> tags = make_list ( tags ) <EOL> for tag in tags : <EOL> query = query . filter ( Task . tags . any ( title = tag ) ) <EOL> without_tags = make_list ( without_tags ) <EOL> for tag in without_tags : <EOL> query = query . filter ( not_ ( Task . tags . any ( title = tag ) ) ) <EOL> if show != '<STR_LIT:all>' : <EOL> query = query . filter_by ( done = ( show == '<STR_LIT>' ) ) <EOL> return query . all ( ) <EOL> def getTags ( self ) : <EOL> return Tag . query . all ( ) <EOL> def deleteTag ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( tag , basestring ) : <EOL> tag = Tag . query . filter_by ( title = tag ) . one ( ) <EOL> else : <EOL> tag = Tag . query . filter_by ( id = tag ) . one ( ) <EOL> tag . delete ( ) <EOL> session . commit ( ) <EOL> def getTagsRelated ( self , tags ) : <EOL> tags = make_list ( tags ) <EOL> tasks = session . query ( Task . id ) . filter ( <EOL> Task . tags . any ( Tag . title . in_ ( tags ) ) ) <EOL> task_ids = [ t [ <NUM_LIT:0> ] for t in tasks ] <EOL> new_tags = Tag . query . filter ( Tag . tasks . any ( Task . id . in_ ( task_ids ) ) ) . filter ( not_ ( Tag . title . in_ ( tags ) ) ) <EOL> return new_tags . all ( ) <EOL> def removeAll ( self ) : <EOL> for task in Task . query . all ( ) : <EOL> task . delete ( ) <EOL> Tag . query . delete ( ) <EOL> session . commit ( ) <EOL> def save ( self , obj ) : <EOL> '''<STR_LIT>''' <EOL> obj . update ( ) <EOL> session . commit ( ) <EOL> def closeTask ( self , task_id ) : <EOL> task = self . getTaskById ( task_id ) <EOL> if task is None : <EOL> raise TaskNotFound <EOL> task . done = True <EOL> self . save ( task ) <EOL> def deleteTask ( self , task_id ) : <EOL> task = self . getTaskById ( task_id ) <EOL> session . delete ( task ) <EOL> session . commit ( ) <EOL> def _createTags ( self , tags ) : <EOL> return [ get_or_create ( Tag , title = tag ) for tag in tags ] </s>
