Passed
Push — master ( 3e6a93...b6cc82 )
by Alexander
01:56
created

src.things3_cli.main()   A

Complexity

Conditions 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nop 1
dl 0
loc 16
rs 9.75
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
import csv
20
import sys
21
import webbrowser
22
from things3 import Things3
23
24
25
class Things3CLI():
26
    """Simple read-only Thing 3 CLI."""
27
28
    print_json = False
29
    print_csv = False
30
    things3 = None
31
32
    def __init__(self, args, things):
33
        self.print_json = args.json
34
        self.print_csv = args.csv
35
        self.things3 = things
36
37
    def print_tasks(self, tasks):
38
        """Print a task."""
39
        if self.print_json:
40
            print(json.dumps(self.things3.convert_tasks_to_model(tasks)))
41
        elif self.print_csv:
42
            fieldnames = ['uuid', 'title', 'context', 'context_uuid', 'due',
43
                          'created', 'modified', 'started', 'stopped']
44
            writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
45
            writer.writeheader()
46
            writer.writerows(self.things3.convert_tasks_to_model(tasks))
47
        else:
48
            for task in tasks:
49
                title = task[self.things3.I_TITLE]
50
                context = task[self.things3.I_CONTEXT]
51
                print(' - ', title, ' (', context, ')')
52
53
    @classmethod
54
    def print_unimplemented(cls):
55
        """Show warning that method is not yet implemented."""
56
        print("not implemented yet (see things.sh for a more complete CLI)")
57
58
59
def main(args):
60
    """ Main entry point of the app """
61
    things3 = Things3()
62
    things_cli = Things3CLI(args, things3)
63
    command = args.command
64
65
    if command in things3.functions:
66
        func = things3.functions[command]
67
        things_cli.print_tasks(func(things3))
68
    elif command == "csv":
69
        print("Deprecated: use --csv instead")
70
    elif command == "feedback":
71
        webbrowser.open(
72
            'https://github.com/AlexanderWillner/KanbanView/issues')
73
    else:
74
        Things3CLI.print_unimplemented()
75
76
77
if __name__ == "__main__":
78
    PARSER = argparse.ArgumentParser(
79
        description='Simple read-only Thing 3 CLI.')
80
81
    SUBPARSERS = PARSER.add_subparsers(help='One of the following commands:',
82
                                       metavar="command",
83
                                       required=True,
84
                                       dest="command")
85
    SUBPARSERS.add_parser('inbox',
86
                          help='Shows all inbox tasks')
87
    SUBPARSERS.add_parser('today',
88
                          help='Shows all todays tasks')
89
    SUBPARSERS.add_parser('upcoming',
90
                          help='Shows all upcoming tasks')
91
    SUBPARSERS.add_parser('next',
92
                          help='Shows all next tasks')
93
    SUBPARSERS.add_parser('someday',
94
                          help='Shows all someday tasks')
95
    SUBPARSERS.add_parser('completed',
96
                          help='Shows all completed tasks')
97
    SUBPARSERS.add_parser('cancelled',
98
                          help='Shows all cancelled tasks')
99
    SUBPARSERS.add_parser('trashed',
100
                          help='Shows all trashed tasks')
101
    SUBPARSERS.add_parser('feedback',
102
                          help='Give feedback')
103
    SUBPARSERS.add_parser('all',
104
                          help='Shows all tasks')
105
    SUBPARSERS.add_parser('csv',
106
                          help='Exports all tasks as CSV')
107
    SUBPARSERS.add_parser('due',
108
                          help='Shows all tasks with due dates')
109
    SUBPARSERS.add_parser('headings',
110
                          help='Shows all headings')
111
    SUBPARSERS.add_parser('hours',
112
                          help='Shows how many hours have been planned today')
113
    SUBPARSERS.add_parser('ical',
114
                          help='Shows all tasks ordered by due date as iCal')
115
    SUBPARSERS.add_parser('logbook',
116
                          help='Shows all tasks completed today')
117
    SUBPARSERS.add_parser('mostClosed',
118
                          help='Shows days on which most tasks were closed')
119
    SUBPARSERS.add_parser('mostCancelled',
120
                          help='Shows days on which most tasks were cancelled')
121
    SUBPARSERS.add_parser('mostTrashed',
122
                          help='Shows days on which most tasks were trashed')
123
    SUBPARSERS.add_parser('mostCreated',
124
                          help='Shows days on which most tasks were created')
125
    SUBPARSERS.add_parser('mostTasks',
126
                          help='Shows projects that have most tasks')
127
    SUBPARSERS.add_parser('mostCharacters',
128
                          help='Shows tasks that have most characters')
129
    SUBPARSERS.add_parser('nextish',
130
                          help='Shows all nextish tasks')
131
    SUBPARSERS.add_parser('old',
132
                          help='Shows all old tasks')
133
    SUBPARSERS.add_parser('projects',
134
                          help='Shows all projects')
135
    SUBPARSERS.add_parser('repeating',
136
                          help='Shows all repeating tasks')
137
    SUBPARSERS.add_parser('schedule',
138
                          help='Schedules an event using a template')
139
    SUBPARSERS.add_parser('search',
140
                          help='Searches for a specific task')
141
    SUBPARSERS.add_parser('stat',
142
                          help='Provides a number of statistics')
143
    SUBPARSERS.add_parser('statcsv',
144
                          help='Exports some statistics as CSV')
145
    SUBPARSERS.add_parser('subtasks',
146
                          help='Shows all subtasks')
147
    SUBPARSERS.add_parser('tag',
148
                          help='Shows all tasks with the waiting for tag')
149
    SUBPARSERS.add_parser('tags',
150
                          help='Shows all tags ordered by their usage')
151
    SUBPARSERS.add_parser('waiting',
152
                          help='Shows all tasks with the waiting for tag')
153
154
    PARSER.add_argument("-j", "--json",
155
                        action="store_true", default=False,
156
                        help="output as JSON", dest="json")
157
158
    PARSER.add_argument("-c", "--csv",
159
                        action="store_true", default=False,
160
                        help="output as CSV", dest="csv")
161
162
    PARSER.add_argument(
163
        "--version",
164
        action="version",
165
        version="%(prog)s (version {version})".format(version=__version__))
166
167
    ARGUMENTS = PARSER.parse_args()
168
    main(ARGUMENTS)
169