Passed
Push — master ( 33fcc4...1be489 )
by Alexander
01:49
created

things3.things3_cli.Things3CLI.get_parser()   B

Complexity

Conditions 1

Size

Total Lines 93
Code Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 87
nop 1
dl 0
loc 93
rs 7.4254
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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