-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
executable file
·63 lines (53 loc) · 1.65 KB
/
tasks.py
File metadata and controls
executable file
·63 lines (53 loc) · 1.65 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
#!/usr/bin/env python3
import sys
from pathlib import Path
# Join needs a tuple
list_path = "".join( ( str(Path(__file__).parent), "/list.markdown" ) )
args = ("onetime", "weekly", "monthly", "yearly")
start = 0
end = 0
def print_usage():
"""Prints usage."""
print ("""
Incorrect call.
Arguments:
onetime - prints onetime tasks
weekly - prints weekly tasks
monthly - prints monthly tasks
yearly - prints yearly tasks""")
return "Incorrect call"
def check_args(arg, tasks_list):
"""Return position for an argument.
Takes an argument(str) and a list,
gets start and end positions for this argument.
"""
global start, end
try:
start = tasks_list.index("### " + arg + "\n") + 1
end = tasks_list.index("### /" + arg + "\n")
return True
except ValueError:
return None
def get_tasks():
"""Get tasks by position.
Write result to stdout, also return result.
Call print_usage() when wrong or no argument provided.
"""
with open(list_path, "r") as f:
tasks = list()
for row in f:
tasks.append(row)
if len(sys.argv) == 2:
if sys.argv[1] in args:
check_args(sys.argv[1], tasks)
selected_tasks = tasks[start:end]
for x in selected_tasks:
sys.stdout.write(x)
return selected_tasks
else:
return print_usage()
else:
return print_usage()
# No need to explicitly call get_tasks() because next check calls it.
if not get_tasks():
print ("Could not find any of requested tasks.")