-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
150 lines (124 loc) · 4.75 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import json
import hashlib
from sys import exit
from os import path, makedirs
from datetime import datetime
from time import time
from ConfigParser import RawConfigParser
from whatidid.objects import Command, BaseUpdateCommand, BaseTodoCommand
class InitCommand(Command):
''' Implements a command for setting everything up.
'''
def __init__(self, **kwargs):
super(InitCommand, self).__init__(**kwargs)
def run(self):
configrc = '%s/.widrc' % (path.expanduser('~'))
if not path.exists(configrc):
print u'There is no ~/.widrc file, createing one.'
config = RawConfigParser()
config.add_section('storage')
config.set('storage', 'path', path.expanduser('~'))
config.add_section('formats')
config.set('formats', 'update-show-format', '%A')
with open(configrc, 'wb') as configfile:
config.write(configfile)
class UpdateCommand(BaseUpdateCommand):
''' Implements a class for the update command.
'''
def __init__(self, **kwargs):
self.message = kwargs.get('message', None)
self.tags = kwargs.get('tags', [])
super(UpdateCommand, self).__init__(**kwargs)
def run(self):
data_path = self.get_data_path(self.type)
try:
with open(data_path, 'rb') as fp:
existing_data = json.load(fp)
except ValueError:
existing_data = []
if self.message:
m = hashlib.md5()
created = int(time())
m.update(str(created) + self.message)
existing_data.append({
'id': m.hexdigest(),
'created': created,
'tags': self.tags,
'message': self.message}
)
with open(data_path, 'wb') as fp:
data = json.dump(existing_data, fp, sort_keys=True, indent=4)
else:
print u'No message specified'
exit(1)
class UpdateShowCommand(BaseUpdateCommand):
''' Implements a class for the update-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(UpdateShowCommand, 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:
out = u'%s' % (datetime.fromtimestamp(int(update['created'])).strftime(self.update_show_format))
if update['tags']:
print u'%s: %s: %s' % (out, ','.join(update['tags']), update['message'])
else:
print u'%s: %s' % (out, update['message'])
except ValueError:
print u'There are no messages.'
class TodoCommand(BaseTodoCommand):
''' Implements a class for the todo command.
'''
def __init__(self, **kwargs):
self.message = kwargs.get('message', None)
self.tags = kwargs.get('tags', [])
super(TodoCommand, self).__init__(**kwargs)
def run(self):
data_path = self.get_data_path(self.type)
try:
with open(data_path, 'rb') as fp:
existing_data = json.load(fp)
except ValueError:
existing_data = []
if self.message:
m = hashlib.md5()
created = int(time())
m.update(str(created) + self.message)
existing_data.append({
'id': m.hexdigest(),
'created': created,
'tags': self.tags,
'message': self.message}
)
with open(data_path, 'wb') as fp:
data = json.dump(existing_data, fp, sort_keys=True, indent=4)
else:
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.'