-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.py
More file actions
157 lines (124 loc) · 3.4 KB
/
todo.py
File metadata and controls
157 lines (124 loc) · 3.4 KB
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
150
151
152
153
154
155
156
157
from os import environ
from functools import partial
from pymlconf import DeferredRoot
from easycli import Root, Argument, SubCommand
__version__ = '0.1.0'
HOME = environ['HOME']
BUILTINSETTINGS = f'''
database:
filename: {HOME}/.cache/todo.csv
'''
settings = DeferredRoot()
ItemArgument = partial(
Argument,
'item',
help='item name'
)
ListArgument = partial(
Argument,
'description',
nargs='?',
default='',
help='description name'
)
class DB:
def __init__(self):
with open(self.filename) as dbfile:
self.items = [line.strip() for line in dbfile]
@property
def filename(self):
return settings.database.filename
def append(self, item):
self.items.append(item)
with open(self.filename, 'w') as f:
for i in self.items:
f.write(i)
f.write('\n')
def delete(self, item):
data = []
for line in self.items:
if item != line.strip().split(':', 1)[0]:
data.append(line)
with open(self.filename, 'w') as f:
for i in data:
f.write(i)
f.write('\n')
def initialize_settings(configfile=None):
settings.initialize(BUILTINSETTINGS)
if configfile is not None:
settings.loadfile(configfile)
class Search(SubCommand):
__command__ = 'search'
__aliases__ = ['s']
__help__ = 'To print searched items(no argument print the whole list).'
__arguments__ = [
ItemArgument(
nargs='?',
default=''
)
]
def __call__(self, args):
db = DB()
if args.item == '':
for i in db.items:
print(i)
else:
for i in db.items:
if i.strip().split(':', 1)[0] == args.item:
print(i)
class Delete(SubCommand):
__command__ = 'delete'
__aliases__ = ['d', 'remove', 'r']
__help__ = 'To delete an item and its desctiption.'
__arguments__ = [ItemArgument()]
def __call__(self, args):
db = DB()
deleteitem = args.item.split(':', 1)[0]
db.delete(deleteitem)
class Append(SubCommand):
__command__ = 'append'
__aliases__ = ['add', 'a']
__help__ = 'Append item and description to the todo list.'
__arguments__ = [
ItemArgument(),
ListArgument()
]
def __call__(self, args):
db = DB()
appendline = f'{args.item.strip()}:\t{args.description.strip()}'
db.append(appendline)
class List(SubCommand):
__command__ = 'list'
__aliases__ = ['l']
__help__ = 'Print the todo list.'
def __call__(self, args):
db = DB()
for i in db.items:
print(i)
class Todo(Root):
__help__ = 'Simple todo list'
__completion__ = True
__arguments__ = [
Argument(
'-v', '--version',
action='store_true',
help='Show version'
),
Argument(
'-c', '--configfile',
metavar='FILENAME',
help='Configuration file to load.'
),
List,
Append,
Delete,
Search
]
def _execute_subcommand(self, args):
initialize_settings(args.configfile)
return super()._execute_subcommand(args)
def __call__(self, args):
if args.version:
print(__version__)
return
return super().__call__(args)