Commits

aodag  committed f385823

run as command

  • Participants
  • Parent commits ac6d333

Comments (0)

Files changed (6)

 *~
 .*.swp
 build
+*.egg-info
+*.old
+*.data

File aodag/tasks/__init__.py

 #
+import argparse
+from . import apis
+from . import commands
+
+from configparser import SafeConfigParser
+
+
+def make_parser():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-c', '--config', help="specify config file")
+    subparsers = parser.add_subparsers()
+    list_parser = subparsers.add_parser('list', help="list tasks")
+    list_parser.set_defaults(func=commands.list_tasks)
+    list_parser.add_argument('-s', '--state', choices=['new', 'active', 'finished', 'all'], default='all')
+    add_parser = subparsers.add_parser('add', help="add tasks")
+    add_parser.set_defaults(func=commands.add_task)
+    finish_parser = subparsers.add_parser('finish', help="finish tasks")
+    finish_parser.set_defaults(func=commands.finish_task)
+    return parser
+
+def main():
+    parser = make_parser()
+    args = parser.parse_args()
+    config = SafeConfigParser()
+    if args.config:
+        print("read config {0}".format(args.config))
+        config.read(args.config)
+    args.config = config
+    if not args.config.has_section('tasks'):
+        args.config.add_section('tasks')
+    if not args.config.has_option('tasks', 'file'):
+        args.config.set('tasks', 'file', 'tasks.data')
+
+    hasattr(args, 'func')
+    args.func(args)

File aodag/tasks/apis.py

 """
 
 import sys
+import time as ttime
 from datetime import datetime, time
 import pickle
 import functools
     True
     """
 
+    if not funcs:
+        yield True
+
     for f in funcs:
         yield f(task)
 
     [{'due_date': datetime.datetime(2012, 4, 1, 0, 0), 'state': 1, 'required_time': datetime.time(0, 25), 'name': 'たすく'}]
 
     """
-    return pickle.load(file)
+    b = file.read()
+    return pickle.loads(b)
 
 
 def input_task(in_=input):
     due_date = in_("due date [%Y-%m-%d]: ")
     due_date = datetime.strptime(due_date, '%Y-%m-%d')
     required_time = in_("required time [%H:%M]: ")
-    required_time = time.strptie(required_time, '%H:%M')
+    required_time = ttime.strptime(required_time, '%H:%M')
+    required_time = time(required_time.tm_hour, required_time.tm_min)
     return create_task(name=name, due_date=due_date, required_time=required_time, state=1)
 
 

File aodag/tasks/commands.py

+import sys
+import os
+from . import apis
+
+def list_tasks(args):
+    filename = args.config['tasks']['file']
+
+    if not os.path.exists(filename):
+        print("{0} does'nt exists".format(filename))
+        sys.exit(1)
+
+    filters = []
+    if args.state == 'new':
+        filters.append(apis.is_new_task)
+    if args.state == 'active':
+        filters.append(apis.is_active_task)
+    if args.state == 'finished':
+        filters.append(apis.is_finished_task)
+
+    with open(filename, "rb") as f:
+        tasks = apis.load_tasks(f)
+        for task in (t for t in tasks if all(apis.filter_many(filters, t))):
+            print(apis.format_task(task))
+
+def add_task(args):
+
+    filename = args.config['tasks']['file']
+    if os.path.exists(filename):
+        with open(filename, "rb") as f:
+            tasks = apis.load_tasks(f)
+    else:
+        tasks = []
+
+    task = apis.input_task()
+    apis.add_task(tasks, task)
+
+    with open(filename, 'wb') as f:
+        apis.save_tasks(tasks, f)
+
+def finish_task(args):
+    print("finish task")
 from setuptools import setup, find_packages
 
+points = {
+    "console_scripts": [
+        "tasks=aodag.tasks:main",
+    ],
+}
 setup(name="aodag.tasks",
         packages=find_packages(),
+        entry_points=points,
         )
+[tasks]
+file = tasks.data