Skip to content

Commit 743d21f

Browse files
author
Sahil
committed
Implement lists functionality
1 parent d16bdf6 commit 743d21f

File tree

6 files changed

+128
-15
lines changed

6 files changed

+128
-15
lines changed

todo_cli/README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,21 @@ source venv/bin/activate
3333
7. `todo complete item_id`
3434
8. `todo incomplete item_id`
3535
9. `help`
36-
10. `quit`
36+
10. `quit`
37+
38+
## JSON Files
39+
40+
### lists.json
41+
42+
- stores a list of todo lists
43+
- a todo list is a dict having title and created_at field
44+
45+
OR
46+
47+
- stores a dict of todo lists having title as key and file name and time of creation as nested dict
48+
49+
### list.json
50+
51+
- list refers to the todo list name
52+
- stores a list of todo item
53+
- each todo item is a dict having title, created_at, and completed field.

todo_cli/app.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,49 @@ def parse(command):
88
cmd_type = cmd_list[0]
99
if (cmd_type == 'help' or cmd_type == 'quit'):
1010
return cmd_type, []
11-
elif (cmd_type == 'list' or cmd_type == 'todo'):
12-
return cmd_list[1], cmd_list[2:]
11+
elif (cmd_type == 'list'):
12+
cmd_name = cmd_list[1]
13+
if (cmd_name in ['show', 'use', 'create']):
14+
return cmd_name, cmd_list[2:]
15+
else:
16+
return 'invalid', []
17+
elif (cmd_type == 'todo'):
18+
cmd_name = cmd_list[1]
19+
if (cmd_name in ['add', 'all', 'edit', 'remove', 'complete', 'incomplete']):
20+
return cmd_name, cmd_list[2:]
21+
else:
22+
return 'invalid', []
1323
else:
1424
return 'invalid', []
1525

1626
def main():
1727
print('Started the Todo application...')
18-
28+
current_list = ''
1929
while(1):
2030
# take the command as input from the user
21-
command = input()
31+
command = input('$ ')
2232
command_name, command_args = parse(command)
23-
print(command_name, command_args)
33+
# print(command_name, command_args)
34+
if (command_name == 'quit'):
35+
break
36+
elif (command_name == 'help'):
37+
with open('help.txt', 'r') as help_file:
38+
print(help_file.read())
39+
elif (command_name == 'invalid'):
40+
print('Please enter a valid command, use help command to display all!')
41+
elif (command_name == 'use'):
42+
file_name = commands_dict[command_name](command_args)
43+
if (file_name == -1):
44+
print('This is not a valid list name!')
45+
current_list = ''
46+
else:
47+
print('Successfuly chosen this list...')
48+
current_list = file_name
49+
elif (command[0] == 'todo'):
50+
command_args.insert(0, current_list)
51+
commands_dict[command_name](command_args)
52+
else:
53+
commands_dict[command_name](command_args)
2454

2555
if __name__ == '__main__':
2656
main()

todo_cli/commands/lists.py

+49-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
1+
import os.path
2+
import json
3+
from datetime import datetime
4+
5+
FILE_NAME = 'lists.json'
6+
17
def show_lists(args):
2-
print(args)
8+
with open(FILE_NAME, 'r') as lists_json:
9+
try:
10+
data = json.load(lists_json)
11+
for index, todo_list in enumerate(data.keys()):
12+
print(index + 1, data[todo_list]['title'])
13+
except:
14+
print('Some error occurred!')
315

416
def use_list(args):
5-
print(args)
17+
list_name = args[0]
18+
with open(FILE_NAME, 'r') as lists_json:
19+
try:
20+
data = json.load(lists_json)
21+
if (data.get(list_name)):
22+
return f'{list_name}.json'
23+
else:
24+
return -1
25+
except:
26+
print('Some error occurred!')
627

728
def create_list(args):
8-
print(args)
29+
list_name = args[0]
30+
# print(os.path.abspath('.'))
31+
new_list = {}
32+
with open(FILE_NAME, 'r+') as lists_json:
33+
try:
34+
data = json.load(lists_json)
35+
# print(data)
36+
# check if file already exists
37+
if (data.get(list_name)):
38+
print('List already exists! Try a different name...')
39+
else:
40+
# update the new_list dict
41+
new_list = {
42+
'title': list_name,
43+
'created_at': datetime.now().strftime("%d/%m/%Y %H:%M:%S")
44+
}
45+
data[list_name] = new_list
46+
with open(f'lists/{list_name}.json', 'w') as new_list:
47+
# empty list
48+
new_list.write('[\n]')
49+
print('Successfully created the new list!')
50+
# add to the lists.json
51+
lists_json.seek(0)
52+
json.dump(data, lists_json, sort_keys=True, indent=True)
53+
except:
54+
print('Some error occurred!')

todo_cli/commands/todos.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
def set_list(list_name):
2+
if (list_name == ''):
3+
print('Please select a given list before this action')
4+
return
5+
16
def add_item(args):
2-
print(args)
7+
set_list(args[0])
38

49
def show_items(args):
5-
print(args)
10+
set_list(args[0])
611

712
def edit_item(args):
8-
print(args)
13+
set_list(args[0])
914

1015
def remove_item(args):
11-
print(args)
16+
set_list(args[0])
1217

1318
def complete_item(args):
14-
print(args)
19+
set_list(args[0])
1520

1621
def incomplete_item(args):
17-
print(args)
22+
set_list(args[0])

todo_cli/help.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The following commands are available:
2+
1. list show => shows all the lists available
3+
2. list use list_name => use a given list
4+
3. list create list_name => create a list with given name
5+
3. todo add item_names => add a todo item to the selected list
6+
4. todo all => show all the todo items in the chosen list
7+
5. todo edit item_id new_item_name => edit the todo item providing the id and new title
8+
6. todo remove item_id => remove a todo item
9+
7. todo complete item_id => mark a todo item as completed
10+
8. todo incomplete item_id => mark a todo item as incomplete
11+
9. help => prints this...
12+
10. quit => exit the application

todo_cli/lists.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}

0 commit comments

Comments
 (0)