Passed
Push — master ( d52a5c...eb2b83 )
by Alexander
01:44
created

src.things3_cli.Things3CLI.print_tasks()   A

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Simple read-only Thing 3 CLI."""
5
6
from __future__ import print_function
7
8
__author__ = "Alexander Willner"
9
__copyright__ = "2020 Alexander Willner"
10
__credits__ = ["Alexander Willner"]
11
__license__ = "Apache"
12
__version__ = "2.0.0"
13
__maintainer__ = "Alexander Willner"
14
__email__ = "[email protected]"
15
__status__ = "Development"
16
17
import argparse
18
import json
19
from things3 import Things3
20
21
22
class Things3CLI():
23
    """Simple read-only Thing 3 CLI."""
24
25
    print_json = False
26
    things3 = None
27
28
    def __init__(self, print_json, things):
29
        self.print_json = print_json
30
        self.things3 = things
31
32
    def print_tasks(self, tasks):
33
        """Print a task."""
34
        if self.print_json:
35
            print(json.dumps(self.things3.convert_tasks_to_model(tasks)))
36
        else:
37
            for task in tasks:
38
                title = task[self.things3.I_TITLE]
39
                context = task[self.things3.I_CONTEXT]
40
                print(' - ', title, ' (', context, ')')
41
42
    @classmethod
43
    def print_unimplemented(cls):
44
        """Show warning that method is not yet implemented."""
45
        print("not implemented yet")
46
47
48
def main(args):
49
    """ Main entry point of the app """
50
    things3 = Things3()
51
    things_cli = Things3CLI(args.json, things3)
52
    command = args.command
53
54
    if command == "inbox":
55
        things_cli.print_tasks(things3.get_inbox())
56
    elif command == "today":
57
        things_cli.print_tasks(things3.get_today())
58
    elif command == "next":
59
        things_cli.print_tasks(things3.get_anytime())
60
    elif command == "backlog":
61
        things_cli.print_tasks(things3.get_someday())
62
    elif command == "upcoming":
63
        things_cli.print_tasks(things3.get_upcoming())
64
    elif command == "waiting":
65
        things_cli.print_tasks(things3.get_waiting())
66
    elif command == "mit":
67
        things_cli.print_tasks(things3.get_mit())
68
    else:
69
        Things3CLI.print_unimplemented()
70
71
72
if __name__ == "__main__":
73
    PARSER = argparse.ArgumentParser(
74
        description='Simple read-only Thing 3 CLI.')
75
76
    SUBPARSERS = PARSER.add_subparsers(help='One of the following commands:',
77
                                       metavar="command",
78
                                       required=True,
79
                                       dest="command")
80
    SUBPARSERS.add_parser('inbox',
81
                          help='Shows all inbox tasks')
82
    SUBPARSERS.add_parser('today',
83
                          help='Shows all todays tasks')
84
    SUBPARSERS.add_parser('upcoming',
85
                          help='Shows all upcoming tasks')
86
    SUBPARSERS.add_parser('next',
87
                          help='Shows all next tasks')
88
    SUBPARSERS.add_parser('someday',
89
                          help='Shows all someday tasks')
90
    SUBPARSERS.add_parser('completed',
91
                          help='Shows all completed tasks')
92
    SUBPARSERS.add_parser('cancelled',
93
                          help='Shows all cancelled tasks')
94
    SUBPARSERS.add_parser('trashed',
95
                          help='Shows all trashed tasks')
96
    SUBPARSERS.add_parser('feedback',
97
                          help='Give feedback')
98
    SUBPARSERS.add_parser('all',
99
                          help='Shows all tasks')
100
    SUBPARSERS.add_parser('csv',
101
                          help='Exports all tasks as CSV')
102
    SUBPARSERS.add_parser('due',
103
                          help='Shows all tasks with due dates')
104
    SUBPARSERS.add_parser('headings',
105
                          help='Shows all headings')
106
    SUBPARSERS.add_parser('hours',
107
                          help='Shows how many hours have been planned today')
108
    SUBPARSERS.add_parser('ical',
109
                          help='Shows all tasks ordered by due date as iCal')
110
    SUBPARSERS.add_parser('logbook',
111
                          help='Shows all tasks completed today')
112
    SUBPARSERS.add_parser('mostClosed',
113
                          help='Shows days on which most tasks were closed')
114
    SUBPARSERS.add_parser('mostCancelled',
115
                          help='Shows days on which most tasks were cancelled')
116
    SUBPARSERS.add_parser('mostTrashed',
117
                          help='Shows days on which most tasks were trashed')
118
    SUBPARSERS.add_parser('mostCreated',
119
                          help='Shows days on which most tasks were created')
120
    SUBPARSERS.add_parser('mostTasks',
121
                          help='Shows projects that have most tasks')
122
    SUBPARSERS.add_parser('mostCharacters',
123
                          help='Shows tasks that have most characters')
124
    SUBPARSERS.add_parser('nextish',
125
                          help='Shows all nextish tasks')
126
    SUBPARSERS.add_parser('old',
127
                          help='Shows all old tasks')
128
    SUBPARSERS.add_parser('projects',
129
                          help='Shows all projects')
130
    SUBPARSERS.add_parser('repeating',
131
                          help='Shows all repeating tasks')
132
    SUBPARSERS.add_parser('schedule',
133
                          help='Schedules an event using a template')
134
    SUBPARSERS.add_parser('search',
135
                          help='Searches for a specific task')
136
    SUBPARSERS.add_parser('stat',
137
                          help='Provides a number of statistics')
138
    SUBPARSERS.add_parser('statcsv',
139
                          help='Exports some statistics as CSV')
140
    SUBPARSERS.add_parser('subtasks',
141
                          help='Shows all subtasks')
142
    SUBPARSERS.add_parser('tag',
143
                          help='Shows all tasks with the waiting for tag')
144
    SUBPARSERS.add_parser('tags',
145
                          help='Shows all tags ordered by their usage')
146
    SUBPARSERS.add_parser('waiting',
147
                          help='Shows all tasks with the waiting for tag')
148
149
    PARSER.add_argument("-j", "--json",
150
                        action="store_true", default=False,
151
                        help="output as JSON", dest="json")
152
    PARSER.add_argument(
153
        "--version",
154
        action="version",
155
        version="%(prog)s (version {version})".format(version=__version__))
156
157
    ARGUMENTS = PARSER.parse_args()
158
    main(ARGUMENTS)
159