<commit_before>import todoist
from base import dlog
import elo
import os
import argparse
import sys
from datetime import datetime
from planner import PriorityPlanner
import time
import yaml


def get_config(args):
    fs = open(args.config)
    cfg = yaml.load(fs)
    fs.close()
    return cfg

def get_app(cfg):

    app = todoist.Todoist()
    app.init(cfg)

    return app

def show(app, args, cfg):
    if args.show_cmd == "api_token":
        print app.user.api_token
    elif args.show_cmd == "stats":
        print app.get_stats()
    elif args.show_cmd == "config":
        print yaml.dump(cfg)

def rank(app, args, cfg):
    projects = app.get_projects()

    for p in projects:
        if p.name in cfg["rank_skip_projects"]:
            dlog("Skip project: " + p.name)
            continue
        if args.project is not None and p.name != args.project:
            dlog("Skip project: " + p.name)
            continue
        print p.name, "[", len(p.tasks), "]"
        tasks = elo.sort(p.tasks)
        p.tasks = tasks
        #app.update_project(p)

        i = 0
        for t in p.tasks:
            app.update_task(t, "item_order", i)
            i += 1
        app.update()

    print("OK")

def choose_func(planner_item):
    tasks = planner_item.item.tasks
    if len(tasks) > 0:
        r = tasks[0]
        planner_item.item.tasks = tasks[1:]
        return r
    else:
        return None

def schedule(app, res, offset=0):
    """ offset is the position that we should start plan for today's taks. That
        means if today we've finished n tasks, we skip the first n finished
        tasks and start to plan the n+1 task.
    """
    tasks_per_day = 10
    if offset > 10:
        print("offset too large: ", offset)
        offset = 7 # 10 - 7 = 3, plan 3 tasks for today if we've already done more than 10 today

    j = offset
    #print("offset is %d" % offset)
    for task in res:
        # schedule t for today
        #print "schedule for today: %d" % j
        seq = j
        #tp.schedule_for(t.id, j)
        day = seq / tasks_per_day
        minute = seq % tasks_per_day
        app.update_task(task, "date_string", "{day} days at 22:{minute:02}".format(day=day, minute=minute))
        #t: Pytodoist Task
        app.mark_as_planned(task)
        j += 1

def plan(app, args, cfg):
    def adjust_for_completed_tasks(stats):
        total = 0
        done = 0
        # completed tasks are sorted from new to old
        tasks = app.get_completed_tasks()
        now = datetime.now()
        for t in tasks:
            if (now - t.ts_done).total_seconds() > 3 * 24 * 3600:
                # larger than 3 days
                break
            pid = t.pid
            if stats.has_key(pid):
                stats[pid] += 1
            else:
                stats[pid] = 1
            total += 1

            if t.done:
                done += 1
        stats['total'] = total
        stats['done'] = done

    projects = app.get_projects()
    project_blacklist = cfg["plan_skip_projects"]
    for p in projects[:]:
        if p.name in project_blacklist:
            print("discard project:", p.name)
            projects.remove(p)
    planner = PriorityPlanner(cfg, preprocess=adjust_for_completed_tasks)

    n = 0
    res = []
    for t in planner.plan(projects, choose_func):
        res.append(t)
        n += 1
        if n >= args.limit:
            break
    offset = app.num_tasks_completed_today()
    print(res)
    #planner.run(projects)
    schedule(app, res, offset=offset)
    app.update()


def check_positive_int(val):
    ival = int(val)<commit_msg>More friendly messages<commit_after>import todoist
from base import dlog
import elo
import os
import argparse
import sys
from datetime import datetime
from planner import PriorityPlanner
import time
import yaml


def get_config(args):
    fs = open(args.config)
    cfg = yaml.load(fs)
    fs.close()
    return cfg

def get_app(cfg):

    app = todoist.Todoist()
    app.init(cfg)

    return app

def show(app, args, cfg):
    if args.show_cmd == "api_token":
        print app.user.api_token
    elif args.show_cmd == "stats":
        print app.get_stats()
    elif args.show_cmd == "config":
        print yaml.dump(cfg)

def rank(app, args, cfg):
    projects = app.get_projects()

    for p in projects:
        if p.name in cfg["rank_skip_projects"]:
            dlog("Skip project: " + p.name)
            continue
        if args.project is not None and p.name != args.project:
            dlog("Skip project: " + p.name)
            continue
        print p.name, "[", len(p.tasks), "]"
        tasks = elo.sort(p.tasks)
        p.tasks = tasks
        #app.update_project(p)

        i = 0
        for t in p.tasks:
            app.update_task(t, "item_order", i)
            i += 1
        app.update()

    print("OK")

def choose_func(planner_item):
    tasks = planner_item.item.tasks
    if len(tasks) > 0:
        r = tasks[0]
        planner_item.item.tasks = tasks[1:]
        return r
    else:
        return None

def schedule(app, res, offset=0):
    """ offset is the position that we should start plan for today's taks. That
        means if today we've finished n tasks, we skip the first n finished
        tasks and start to plan the n+1 task.
    """
    tasks_per_day = 10
    if offset > 10:
        print("offset too large: ", offset)
        offset = 7 # 10 - 7 = 3, plan 3 tasks for today if we've already done more than 10 today

    j = offset
    #print("offset is %d" % offset)
    for task in res:
        # schedule t for today
        #print "schedule for today: %d" % j
        seq = j
        #tp.schedule_for(t.id, j)
        day = seq / tasks_per_day
        minute = seq % tasks_per_day
        app.update_task(task, "date_string", "{day} days at 22:{minute:02}".format(day=day, minute=minute))
        #t: Pytodoist Task
        app.mark_as_planned(task)
        j += 1

def plan(app, args, cfg):
    def adjust_for_completed_tasks(stats):
        total = 0
        done = 0
        # completed tasks are sorted from new to old
        tasks = app.get_completed_tasks()
        now = datetime.now()
        for t in tasks:
            if (now - t.ts_done).total_seconds() > 3 * 24 * 3600:
                # larger than 3 days
                break
            pid = t.pid
            if stats.has_key(pid):
                stats[pid] += 1
            else:
                stats[pid] = 1
            total += 1

            if t.done:
                done += 1
        stats['total'] = total
        stats['done'] = done

    projects = app.get_projects()
    project_blacklist = cfg["plan_skip_projects"]
    for p in projects[:]:
        if p.name in project_blacklist:
            print("skip project when planning:", p.name)
            projects.remove(p)
    planner = PriorityPlanner(cfg, preprocess=adjust_for_completed_tasks)

    n = 0
    res = []
    for t in planner.plan(projects, choose_func):
        res.append(t)
        n += 1
        if n >= args.limit:
            break
    offset = app.num_tasks_completed_today()
    print(res)
    #planner.run(projects)
    schedule(app, res, offset=offset)
    app.update()


def check_positive_int(val):
    ival = int(val)
{'fraction_non_alphanumeric': 0.10469613259668509, 'fraction_numerical': 0.011049723756906077, 'mean_word_length': 1.9446929646197642, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2829487', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2224, 'n_words': 679}