Ejemplo n.º 1
0
def cmd_delay(tl, args):
    """delays the due date of one or more todo items
    """
    with ColorRenderer() as cr:
        item = tl.get_item_by_index(args.item)
        if not item:
            print(u"Could not find item '{item_id}'".format(item_id = args.item))
            return
        if item.due_date:
            new_date = to_date(args.date, item.due_date)
            if isinstance(new_date, basestring):
                # remove first character, as it is "?" with a non-parsable date
                print(u"The given relative date could not be parsed: {date}".format(date = new_date[1:]))
            else:
                # ask for confirmation
                if not args.force:
                    print(" ", cr.render(item))
                    if not confirm_action(u"Delaying the preceding item's date from {from_date} to {to_date} (y/N)?".format(
                        from_date = from_date(item.due_date), to_date = from_date(new_date))):
                        return
                # do the actual replacement
                tl.replace_or_add_prop(item, conf.DUE, from_date(new_date), new_date)
        else:
            new_date = to_date(args.date)
            if not args.force:
                print(u" ", cr.render(item))
                if not confirm_action(u"The preceding item has no due date set, set to {date} (y/N)?".format(date = from_date(new_date))):
                    return
                tl.replace_or_add_prop(item, conf.DUE, from_date(new_date), new_date)
        suppress_if_quiet(u"  {item}".format(item = cr.render(item)), args)
Ejemplo n.º 2
0
def create_config_wizard():
    config = ConfigParser.ConfigParser()
    # ask for creation of configuration
    if not confirm_action(
            "No configuration found, do you want to create a new configuration (y/N)?"
    ):
        print("So, next time perhaps...")
        quit(0)
    # set standard todo file name
    todo_filename = "todo.txt"
    if len(sys.argv) > 1:
        # another configuration file has been given via the command line
        todo_filename = sys.argv[1]
    todo_filename = os.path.abspath(todo_filename)
    # ask for confirmation if the file should be created
    if not confirm_action(
            "Do you want to create your todo file with the standard name '{fn}' (y/N)?"
            .format(fn=todo_filename)):
        # choose an own name
        if not confirm_action(
                "Do you want to choose another file name (y/N)?"):
            return None
        todo_filename = os.path.abspath(
            raw_input(
                "Please enter the path/filename of your todo file: ").strip())
    if os.path.exists(todo_filename):
        print("* Todo file {fn} already exists...".format(fn=todo_filename))
    else:
        # create a new todo file
        print("* Creating todo file {fn}".format(fn=todo_filename))
        with codecs.open(todo_filename, "w", "utf-8") as fp:
            fp.write(
                "(A) Check out https://github.com/philScholl/todo.next-proto")
    # copy the config template file and change the todo file location
    print("* Creating a new config file at '{fn}'".format(fn=config_file))
    with codecs.open(
            os.path.join(os.path.dirname(os.path.abspath(__file__)),
                         "config.template"), "r", "utf-8") as fromfp:
        config.readfp(fromfp)
        config.set("todo", "todofile", todo_filename)
        with codecs.open(config_file, "w", "utf-8") as tofp:
            # save
            config.write(tofp)
    print(
        "Please have a look at your configuration file (\"t config\") and change according to your preferences!"
    )
    # finish here
    return todo_filename
Ejemplo n.º 3
0
def cmd_backup(tl, args):  #@UnusedVariable
    """backups the current todo file to a timestamped file
    
    Required fields of :param:`args`:
    * filename: the name of the backup file [optional]
    """
    template = os.path.basename(conf.todo_file)
    filename = datetime.datetime.now().strftime("%Y-%m-%d_%H%M_" + template)
    # backup directory can be relative to todo file directory
    backup_folder = os.path.join(os.path.dirname(conf.todo_file),
                                 conf.backup_dir)
    # if not existing, create it
    if not os.path.exists(backup_folder):
        os.makedirs(backup_folder)
    # a filename has been specified by command line arguments
    if args.filename:
        filename = args.filename
    # destination file
    dst_fn = os.path.join(backup_folder, filename)
    if os.path.exists(dst_fn):
        # file already exists: what to do?
        if not confirm_action(
                u"File {fn} already exists. Overwrite (y/N) ".format(
                    fn=dst_fn)):
            print(u"  Aborting...")
            quit(0)
        else:
            print(u"  Overwriting {fn}...".format(fn=dst_fn))
    # copying the todo file to the destination
    with codecs.open(conf.todo_file, "r", "utf-8") as src:
        suppress_if_quiet(u"  Copying todo file to {fn}...".format(fn=dst_fn),
                          args)
        with codecs.open(dst_fn, "w", "utf-8") as dst:
            dst.write(src.read())
    suppress_if_quiet(u"Successfully backed up todo file.", args)
Ejemplo n.º 4
0
def cmd_remove(tl, args):
    """removes one or more items from the todo list
    
    Required fields of :param:`args`:
    * items: the index number of the items to remove
    * force: if given, confirmation is not requested
    """
    with ColorRenderer() as cr:
        item_list = tl.get_items_by_index_list(args.items)
        if not item_list:
            msg = "Could not find item(s) {item_ids}".format(
                item_ids=", ".join(args.items))
            print(msg)
            logger.info(msg)
            return
        if not args.force:
            print("Do you really want to remove the following item(s):")
            for item in item_list:
                print(" ", cr.render(item))
            if confirm_action("Please confirm (y/N): "):
                for item in item_list:
                    tl.remove_item(item)
            else:
                print("Removing aborted")
                return
        else:
            for item in item_list:
                tl.remove_item(item)
        msg = u"{nr} todo items ({item_ids}) have been removed.".format(
            nr=len(item_list),
            item_ids=",".join(
                [cr.wrap_id(item.tid, reset=True) for item in item_list]))
        suppress_if_quiet(msg, args)
        logger.info(msg)
Ejemplo n.º 5
0
def cmd_remove(tl, args):
    """removes one or more items from the todo list
    """
    with ColorRenderer() as cr:
        item_list = tl.get_items_by_index_list(args.items)
        if not item_list:
            msg = "Could not find item(s) {item_ids}".format(item_ids = ", ".join(args.items))
            print(msg)
            logger.info(msg)
            return
        if not args.force:
            print("Do you really want to remove the following item(s):")
            for item in item_list:
                print(" ", cr.render(item))
            if confirm_action("Please confirm (y/N): "):
                for item in item_list:
                    tl.remove_item(item)
            else:
                print("Removing aborted")
                return
        else:
            for item in item_list:
                tl.remove_item(item)
        msg = u"{nr} todo items ({item_ids}) have been removed.".format(nr = len(item_list), item_ids = ",".join([cr.wrap_id(item.tid, reset=True) for item in item_list]))
        suppress_if_quiet(msg, args)
        logger.info(msg)
Ejemplo n.º 6
0
def cmd_backup(tl, args): #@UnusedVariable
    """backups the current todo file to a timestamped file
    """
    template = os.path.basename(conf.todo_file)
    filename = datetime.datetime.now().strftime("%Y-%m-%d_%H%M_" + template)
    # backup directory can be relative to todo file directory
    backup_folder = os.path.join(os.path.dirname(conf.todo_file), conf.backup_dir)
    # if not existing, create it
    if not os.path.exists(backup_folder):
        os.makedirs(backup_folder)
    # a filename has been specified by command line arguments
    if args.filename:
        filename = args.filename
    # destination file
    dst_fn = os.path.join(backup_folder, filename)
    if os.path.exists(dst_fn):
        # file already exists: what to do?
        if not confirm_action(u"File {fn} already exists. Overwrite (y/N) ".format(fn = dst_fn)):
            print(u"  Aborting...")
            quit(0)
        else:
            print(u"  Overwriting {fn}...".format(fn = dst_fn))
    # copying the todo file to the destination
    with codecs.open(conf.todo_file, "r", "utf-8") as src:
        suppress_if_quiet(u"  Copying todo file to {fn}...".format(fn = dst_fn), args)
        with codecs.open(dst_fn, "w", "utf-8") as dst:
            dst.write(src.read())
    suppress_if_quiet(u"Successfully backed up todo file.", args)
Ejemplo n.º 7
0
def cmd_delay(tl, args):
    """delays the due date of one or more todo items
    
    Required fields of :param:`args`:
    * item: the index number of the item to delay
    * date: either a date or a string like 'tomorrow', default '1d' (delays for 1 day)
    * force: if given, confirmation is not requested
    """
    with ColorRenderer() as cr:
        item = tl.get_item_by_index(args.item)
        if not item:
            print(u"Could not find item '{item_id}'".format(item_id=args.item))
            return
        if item.due_date:
            new_date = to_date(args.date, item.due_date)
            if isinstance(new_date, basestring):
                # remove first character, as it is "?" with a non-parsable date
                print(u"The given relative date could not be parsed: {date}".
                      format(date=new_date[1:]))
            else:
                # ask for confirmation
                if not args.force:
                    print(" ", cr.render(item))
                    if not confirm_action(
                            u"Delaying the preceding item's date from {from_date} to {to_date} (y/N)?"
                            .format(from_date=from_date(item.due_date),
                                    to_date=from_date(new_date))):
                        return
                # do the actual replacement
                tl.replace_or_add_prop(item, conf.DUE, from_date(new_date),
                                       new_date)
        else:
            new_date = to_date(args.date)
            if not args.force:
                print(u" ", cr.render(item))
                if not confirm_action(
                        u"The preceding item has no due date set, set to {date} (y/N)?"
                        .format(date=from_date(new_date))):
                    return
                tl.replace_or_add_prop(item, conf.DUE, from_date(new_date),
                                       new_date)
        suppress_if_quiet(u"  {item}".format(item=cr.render(item)), args)
Ejemplo n.º 8
0
def create_config_wizard():
    config = ConfigParser.ConfigParser()
    # ask for creation of configuration
    if not confirm_action("No configuration found, do you want to create a new configuration (y/N)?"):
        print("So, next time perhaps...")
        quit(0)
    # set standard todo file name
    todo_filename = "todo.txt"
    if len(sys.argv) > 1:
        # another configuration file has been given via the command line
        todo_filename = sys.argv[1]
    todo_filename = os.path.abspath(todo_filename)
    # ask for confirmation if the file should be created
    if not confirm_action("Do you want to create your todo file with the standard name '{fn}' (y/N)?".format(fn = todo_filename)):
        # choose an own name
        if not confirm_action("Do you want to choose another file name (y/N)?"):
            return None
        todo_filename = os.path.abspath(raw_input("Please enter the path/filename of your todo file: ").strip())
    if os.path.exists(todo_filename):
        print("* Todo file {fn} already exists...".format(fn = todo_filename))
    else:
        # create a new todo file
        print("* Creating todo file {fn}".format(fn = todo_filename))
        with codecs.open(todo_filename, "w", "utf-8") as fp:
            fp.write("(A) Check out https://github.com/philScholl/todo.next-proto")
    # copy the config template file and change the todo file location
    print("* Creating a new config file at '{fn}'".format(fn = config_file))
    with codecs.open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.template"), "r", "utf-8") as fromfp:
        config.readfp(fromfp)
        config.set("todo", "todofile", todo_filename)
        with codecs.open(config_file, "w", "utf-8") as tofp:
            # save
            config.write(tofp)
    print("Please have a look at your configuration file (\"t config\") and change according to your preferences!")
    # finish here
    return todo_filename