Permalink
Browse files

adding in a Todo function

  • Loading branch information...
1 parent 18ca536 commit 02e581bb920622b8b973879cdfe2b60659a2f74a @zachseifts committed Nov 21, 2012
Showing with 124 additions and 71 deletions.
  1. +4 −0 bin/wid
  2. +27 −71 whatidid/commands.py
  3. +93 −0 whatidid/objects.py
View
@@ -44,6 +44,10 @@ class Main(object):
command = commands.TodoCommand(message=self.message, tags=self.tags)
command.run()
+ def command_todo_show(self):
+ command = commands.TodoShowCommand(tags=self.tags)
+ command.run()
+
def command_notfound(self):
print u'The command was not found'
exit(1);
View
@@ -1,80 +1,13 @@
-
import json
import hashlib
from sys import exit
from os import path, makedirs
-from getpass import getuser
from datetime import datetime
from time import time
-from ConfigParser import ConfigParser, RawConfigParser, NoSectionError
-
-class Command(object):
- ''' A base class for creating commands to work with the whatidid project
-
- Subclass this class to generate a command:
-
- >>> from whatidid.commands import Command
- >>> class MyCommand(Command):
- >>> def __init__(self, **kwargs):
- >>> self.thing = 'foo'
- >>> super(MyCommand, self).__init__(**kwargs)
- >>> def run(self):
- >>> print 'ran the command'
- >>> command = MyCommand()
- >>> command.run()
- ran the command
-
- '''
-
- def __init__(self, **kwargs):
- configrc = '%s/.widrc' % (path.expanduser('~'))
- config = ConfigParser()
- config.read(configrc)
- try:
- storage_path = config.get('storage', 'path', path.expanduser('~'))
- except NoSectionError:
- storage_path = path.expanduser('~')
- self.storage_path = '%s/.whatidid' % (storage_path,)
- try:
- self.update_show_format = config.get('formats', 'update-show-format', '%Y')
- except NoSectionError:
- self.update_show_format = '%Y'
-
- def get_data_path(self, key, week=None):
- ''' Get the path of the data file for a key
-
- Usage:
-
- >>> from whatidid.commands import Command
- >>> command = Command()
- >>> command.get_data_path('foo')
- /Users/user/Dropbox/.whatidid/foo/2012/34.md
-
- '''
- year, current_week, weekday = datetime.now().isocalendar()
- if week is None: week = current_week
- data_dir = '%s/%s/%d' % (self.storage_path, key, year)
- if not path.exists(data_dir):
- makedirs(data_dir)
- data_path = '%s/%d.json' % (data_dir, week)
- if not path.exists(data_path):
- with open(data_path, 'wb') as updates:
- json.dump([], updates)
- return data_path
-
- def run(self):
- raise NotImplementedError('The run() function is not implemented.')
-
-
-class BaseUpdateCommand(Command):
- ''' A base class for Update commands
- '''
-
- def __init__(self, **kwargs):
- self.type = 'updates'
- super(BaseUpdateCommand, self).__init__(**kwargs)
+from ConfigParser import RawConfigParser
+from whatidid.objects import Command, BaseUpdateCommand, BaseTodoCommand
class InitCommand(Command):
''' Implements a command for setting everything up.
@@ -158,7 +91,7 @@ def run(self):
print u'There are no messages.'
-class TodoCommand(Command):
+class TodoCommand(BaseTodoCommand):
''' Implements a class for the todo command.
'''
@@ -168,7 +101,7 @@ def __init__(self, **kwargs):
super(TodoCommand, self).__init__(**kwargs)
def run(self):
- data_path = self.get_data_path('todo')
+ data_path = self.get_data_path(self.type)
try:
with open(data_path, 'rb') as fp:
@@ -192,3 +125,26 @@ def run(self):
print u'No todo item specified'
exit(1)
+
+class TodoShowCommand(BaseTodoCommand):
+ ''' Impelments a class for the todo-show command.
+ '''
+
+ def __init__(self, **kwargs):
+ year, current_week, weekday = datetime.now().isocalendar()
+ week = kwargs.get('week', None)
+ if week is None:
+ week = current_week
+ self.week = int(week)
+ super(TodoShowCommand, self).__init__(**kwargs)
+
+ def run(self):
+ data_path = self.get_data_path(self.type, self.week)
+ try:
+ with open(data_path, 'rb') as fp:
+ existing_data = json.load(fp)
+ for update in existing_data:
+ print u'%s: %s' % (update['id'], update['message'],)
+ except ValueError:
+ print u'There are no todo items.'
+
View
@@ -0,0 +1,93 @@
+import json
+
+from os import path, makedirs
+from datetime import datetime
+from ConfigParser import ConfigParser, NoSectionError
+
+class Command(object):
+ ''' A base class for creating commands to work with the whatidid project
+
+ Subclass this class to generate a command:
+
+ >>> from whatidid.commands import Command
+ >>> class MyCommand(Command):
+ >>> def __init__(self, **kwargs):
+ >>> self.thing = 'foo'
+ >>> super(MyCommand, self).__init__(**kwargs)
+ >>> def run(self):
+ >>> print 'ran the command'
+ >>> command = MyCommand()
+ >>> command.run()
+ ran the command
+
+ '''
+
+ def __init__(self, **kwargs):
+ configrc = '%s/.widrc' % (path.expanduser('~'))
+ config = ConfigParser()
+ config.read(configrc)
+ try:
+ storage_path = config.get('storage', 'path', path.expanduser('~'))
+ except NoSectionError:
+ storage_path = path.expanduser('~')
+ self.storage_path = '%s/.whatidid' % (storage_path,)
+ try:
+ self.update_show_format = config.get('formats', 'update-show-format', '%Y')
+ except NoSectionError:
+ self.update_show_format = '%Y'
+
+ def get_data_path(self, key, week=None):
+ ''' Get the path of the data file for a key
+
+ Usage:
+
+ >>> from whatidid.commands import Command
+ >>> command = Command()
+ >>> command.get_data_path('foo')
+ /Users/user/Dropbox/.whatidid/foo/2012/34.md
+
+ '''
+ year, current_week, weekday = datetime.now().isocalendar()
+ if week is None: week = current_week
+ data_dir = '%s/%s/%d' % (self.storage_path, key, year)
+ if not path.exists(data_dir):
+ makedirs(data_dir)
+ data_path = '%s/%d.json' % (data_dir, week)
+ if not path.exists(data_path):
+ with open(data_path, 'wb') as updates:
+ json.dump([], updates)
+ return data_path
+
+ def run(self):
+ raise NotImplementedError('The run() function is not implemented.')
+
+
+class BaseUpdateCommand(Command):
+ ''' A base class for Update commands
+ '''
+
+ def __init__(self, **kwargs):
+ self.type = 'updates'
+ super(BaseUpdateCommand, self).__init__(**kwargs)
+
+
+class BaseTodoCommand(Command):
+ ''' A base class for Todo commands
+ '''
+
+ def __init__(self, **kwargs):
+ self.type = 'todo'
+ super(BaseTodoCommand, self).__init__(**kwargs)
+
+ def get_data_path(self, key, week=None):
+ ''' Overrides the default get_data_path.
+ '''
+ data_dir = '%s/%s' % (self.storage_path, key)
+ if not path.exists(data_dir):
+ makedirs(data_dir)
+ data_path = '%s/items.json' % (data_dir,)
+ if not path.exists(data_path):
+ with open(data_path, 'wb') as updates:
+ json.dump([], updates)
+ return data_path
+

0 comments on commit 02e581b

Please sign in to comment.