Passed
Pull Request — develop (#458)
by
unknown
02:49
created

doorstop.cli.main._import()   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 18

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nop 2
dl 19
loc 19
rs 9.5
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
"""Command-line interface for Doorstop."""
4
5
import os
6
import sys
7
import argparse
8
9
from doorstop import common
10
from doorstop.cli import utilities, commands
11
12
log = common.logger(__name__)
13
14
15
def main(args=None):  # pylint: disable=R0915
16
    """Process command-line arguments and run the program."""
17
    from doorstop import CLI, VERSION, DESCRIPTION
0 ignored issues
show
introduced by
Import outside toplevel (doorstop)
Loading history...
18
19
    # Shared options
20
    project = argparse.ArgumentParser(add_help=False)
21
    project.add_argument('-j', '--project', metavar='PATH',
22
                         help="path to the root of the project")
23
    project.add_argument('--no-cache', action='store_true',
24
                         help=argparse.SUPPRESS)
25
    server = argparse.ArgumentParser(add_help=False)
26
    server.add_argument('--server', metavar='HOST',
27
                        help="IP address or hostname for a running server")
28
    server.add_argument('--port', metavar='NUM', type=int,
29
                        help="use a custom port for the server")
30
    server.add_argument('-f', '--force', action='store_true',
31
                        help="perform the action without the server")
32
    debug = argparse.ArgumentParser(add_help=False)
33
    debug.add_argument('-V', '--version', action='version', version=VERSION)
34
    group = debug.add_mutually_exclusive_group()
35
    group.add_argument('-v', '--verbose', action='count', default=0,
36
                       help="enable verbose logging")
37
    group.add_argument('-q', '--quiet', action='store_const', const=-1,
38
                       dest='verbose', help="only display errors and prompts")
39
    shared = {'formatter_class': common.HelpFormatter,
40
              'parents': [project, server, debug]}
41
42
    # Build main parser
43
    parser = argparse.ArgumentParser(prog=CLI, description=DESCRIPTION,
44
                                     **shared)
45
    parser.add_argument('-F', '--no-reformat', action='store_true',
46
                        help="do not reformat item files during validation")
47
    parser.add_argument('-r', '--reorder', action='store_true',
48
                        help="reorder document levels during validation")
49
    parser.add_argument('-L', '--no-level-check', action='store_true',
50
                        help="do not validate document levels")
51
    parser.add_argument('-R', '--no-ref-check', action='store_true',
52
                        help="do not validate external file references")
53
    parser.add_argument('-C', '--no-child-check', action='store_true',
54
                        help="do not validate child (reverse) links")
55
    parser.add_argument('-S', '--no-suspect-check', action='store_true',
56
                        help="do not check for suspect links")
57
    parser.add_argument('-W', '--no-review-check', action='store_true',
58
                        help="do not check item review status")
59
    parser.add_argument('-w', '--warn-all', action='store_true',
60
                        help="display all info-level issues as warnings")
61
    parser.add_argument('-e', '--error-all', action='store_true',
62
                        help="display all warning-level issues as errors")
63
64
    # Build sub-parsers
65
    subs = parser.add_subparsers(help="", dest='command', metavar="<command>")
66
    _create(subs, shared)
67
    _delete(subs, shared)
68
    _add(subs, shared)
69
    _remove(subs, shared)
70
    _edit(subs, shared)
71
    _reorder(subs, shared)
72
    _link(subs, shared)
73
    _unlink(subs, shared)
74
    _clear(subs, shared)
75
    _review(subs, shared)
76
    _import(subs, shared)
77
    _export(subs, shared)
78
    _publish(subs, shared)
79
                                        #add 27.11.2019/ 08.12.2019 / 11.12.2019 / 13.12.2019 15.12.2019
80
    _valMatrix(subs, shared)
81
    _verMatrix(subs, shared)
82
    _findRef(subs, shared)
83
    _ref(subs, shared)
84
    _clearRef(subs, shared)
85
                                        # end add 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
86
87
    # Parse arguments
88
    args = parser.parse_args(args=args)
89
90
    # Configure logging
91
    utilities.configure_logging(args.verbose)
92
93
    # Configure settings
94
    utilities.configure_settings(args)
95
96
    # Run the program
97
    function = commands.get(args.command)
98
    try:
99
        success = function(args, os.getcwd(), parser.error)
100
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
101
    except KeyboardInterrupt:
102
        log.debug("command cancelled")
103
        success = False
104
    if success:
105
        log.debug("command succeeded")
106
    else:
107
        log.debug("command failed")
108
        sys.exit(1)
109
110
111
def _create(subs, shared):
112
    """Configure the `doorstop create` subparser."""
113
    info = "create a new document directory"
114
    sub = subs.add_parser('create', description=info.capitalize() + '.',
115
                          help=info, **shared)
116
    sub.add_argument('prefix', help="document prefix for new item UIDs")
117
    sub.add_argument('path', help="path to a directory for item files")
118
    sub.add_argument('-p', '--parent', help="prefix of parent document")
119
    sub.add_argument('-d', '--digits', help="number of digits in item UIDs")
120
121
122
def _delete(subs, shared):
123
    """Configure the `doorstop delete` subparser."""
124
    info = "delete a document directory"
125
    sub = subs.add_parser('delete', description=info.capitalize() + '.',
126
                          help=info, **shared)
127
    sub.add_argument('prefix', help="prefix of document to delete")
128
129
130
def _add(subs, shared):
131
    """Configure the `doorstop add` subparser."""
132
    info = "create an item file in a document directory"
133
    sub = subs.add_parser('add', description=info.capitalize() + '.',
134
                          help=info, **shared)
135
    sub.add_argument('prefix',
136
                     help="document prefix for the new item")
137
    sub.add_argument('-l', '--level', help="desired item level (e.g. 1.2.3)")
138
    sub.add_argument('-c', '--count', default=1, type=utilities.positive_int,
139
                     help="number of items to create (default: 1)")
140
141
142
def _remove(subs, shared):
143
    """Configure the `doorstop remove` subparser."""
144
    info = "remove an item file from a document directory"
145
    sub = subs.add_parser('remove', description=info.capitalize() + '.',
146
                          help=info, **shared)
147
    sub.add_argument('uid', help="item UID to remove from its document")
148
149
150
def _edit(subs, shared):
151
    """Configure the `doorstop edit` subparser."""
152
    info = "open an existing item or document for editing"
153
    sub = subs.add_parser('edit', description=info.capitalize() + '.',
154
                          help=info, **shared)
155
    sub.add_argument('label',
156
                     help="item UID or document prefix to open for editing")
157
    group = sub.add_mutually_exclusive_group()
158
    group.add_argument('-i', '--item', action='store_true',
159
                       help="indicates the 'label' is an item UID")
160
    group.add_argument('-d', '--document', action='store_true',
161
                       help="indicates the 'label' is a document prefix")
162
    group = sub.add_mutually_exclusive_group()
163
    group.add_argument('-y', '--yaml', action='store_true',
164
                       help="edit document as exported YAML (default)")
165
    group.add_argument('-c', '--csv', action='store_true',
166
                       help="edit document as exported CSV")
167
    group.add_argument('-t', '--tsv', action='store_true',
168
                       help="edit document as exported TSV")
169
    group.add_argument('-x', '--xlsx', action='store_true',
170
                       help="edit document as exported XLSX")
171
    sub.add_argument('-T', '--tool', metavar='PROGRAM',
172
                     help="text editor to open the document item")
173
174
175
def _reorder(subs, shared):
176
    """Configure the `doorstop reorder` subparser."""
177
    info = "organize the outline structure of a document"
178
    sub = subs.add_parser('reorder', description=info.capitalize() + '.',
179
                          help=info, **shared)
180
    sub.add_argument('prefix', help="prefix of document to reorder")
181
    group = sub.add_mutually_exclusive_group()
182
    group.add_argument('-a', '--auto', action='store_true',
183
                       help="only perform automatic item reordering")
184
    group.add_argument('-m', '--manual', action='store_true',
185
                       help="do not automatically reorder the items")
186
    sub.add_argument('-T', '--tool', metavar='PROGRAM',
187
                     help="text editor to open the document index")
188
189
190
def _link(subs, shared):
191
    """Configure the `doorstop link` subparser."""
192
    info = "add a new link between two items"
193
    sub = subs.add_parser('link', description=info.capitalize() + '.',
194
                          help=info, **shared)
195
    sub.add_argument('child',
196
                     help="child item UID to link to the parent")
197
    sub.add_argument('parent',
198
                     help="parent item UID to link from the child")
199
200
201
def _unlink(subs, shared):
202
    """Configure the `doorstop unlink` subparser."""
203
    info = "remove a link between two items"
204
    sub = subs.add_parser('unlink', description=info.capitalize() + '.',
205
                          help=info, **shared)
206
    sub.add_argument('child',
207
                     help="child item UID to unlink from parent")
208
    sub.add_argument('parent',
209
                     help="parent item UID child is linked to")
210
211
212
def _clear(subs, shared):
213
    """Configure the `doorstop clear` subparser."""
214
    info = "absolve items of their suspect link status"
215
    sub = subs.add_parser('clear', description=info.capitalize() + '.',
216
                          help=info, **shared)
217
    sub.add_argument('label', help="item UID, document prefix, or 'all'")
218
    group = sub.add_mutually_exclusive_group()
219
    group.add_argument('-i', '--item', action='store_true',
220
                       help="indicates the 'label' is an item UID")
221
    group.add_argument('-d', '--document', action='store_true',
222
                       help="indicates the 'label' is a document prefix")
223
224
225
def _review(subs, shared):
226
    """Configure the `doorstop review` subparser."""
227
    info = "absolve items of their unreviewed status"
228
    sub = subs.add_parser('review', description=info.capitalize() + '.',
229
                          help=info, **shared)
230
    sub.add_argument('label', help="item UID, document prefix, or 'all'")
231
    group = sub.add_mutually_exclusive_group()
232
    group.add_argument('-i', '--item', action='store_true',
233
                       help="indicates the 'label' is an item UID")
234
    group.add_argument('-d', '--document', action='store_true',
235
                       help="indicates the 'label' is a document prefix")
236
237
238 View Code Duplication
def _import(subs, shared):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
239
    """Configure the `doorstop import` subparser."""
240
    info = "import an existing document or item"
241
    sub = subs.add_parser('import', description=info.capitalize() + '.',
242
                          help=info, **shared)
243
    sub.add_argument('path', nargs='?',
244
                     help="path to previously exported document file")
245
    sub.add_argument('prefix', nargs='?', help="prefix of document for import")
246
    group = sub.add_mutually_exclusive_group()
247
    group.add_argument('-d', '--document', nargs=2, metavar='ARG',
248
                       help="import an existing document by: PREFIX PATH")
249
    group.add_argument('-i', '--item', nargs=2, metavar='ARG',
250
                       help="import an existing item by: PREFIX UID")
251
    sub.add_argument('-p', '--parent', metavar='PREFIX',
252
                     help="parent document prefix for imported document")
253
    sub.add_argument('-a', '--attrs', metavar='DICT',
254
                     help="dictionary of item attributes to import")
255
    sub.add_argument('-m', '--map', metavar='DICT',
256
                     help="dictionary of custom item attribute names")
257
258
259 View Code Duplication
def _export(subs, shared):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
260
    """Configure the `doorstop export` subparser."""
261
    info = "export a document as YAML or another format"
262
    sub = subs.add_parser('export', description=info.capitalize() + '.',
263
                          help=info, **shared)
264
    sub.add_argument('prefix', help="prefix of document to export or 'all'")
265
    sub.add_argument('path', nargs='?',
266
                     help="path to exported file or directory for 'all'")
267
    group = sub.add_mutually_exclusive_group()
268
    group.add_argument('-y', '--yaml', action='store_true',
269
                       help="output YAML (default when no path)")
270
    group.add_argument('-c', '--csv', action='store_true',
271
                       help="output CSV (default for 'all')")
272
    group.add_argument('-t', '--tsv', action='store_true',
273
                       help="output TSV")
274
    group.add_argument('-x', '--xlsx', action='store_true',
275
                       help="output XLSX")
276
    sub.add_argument('-w', '--width', type=int,
277
                     help="limit line width on text output")
278
279
280 View Code Duplication
def _publish(subs, shared):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
281
    """Configure the `doorstop publish` subparser."""
282
    info = "publish a document as text or another format"
283
    sub = subs.add_parser('publish', description=info.capitalize() + '.',
284
                          help=info, **shared)
285
    sub.add_argument('prefix', help="prefix of document to publish or 'all'")
286
    sub.add_argument('path', nargs='?',
287
                     help="path to published file or directory for 'all'")
288
    group = sub.add_mutually_exclusive_group()
289
    group.add_argument('-t', '--text', action='store_true',
290
                       help="output text (default when no path)")
291
    group.add_argument('-m', '--markdown', action='store_true',
292
                       help="output Markdown")
293
    group.add_argument('-H', '--html', action='store_true',
294
                       help="output HTML (default for 'all')")
295
    sub.add_argument('-w', '--width', type=int,
296
                     help="limit line width on text output")
297
    sub.add_argument('-C', '--no-child-links', action='store_true',
298
                     help="do not include child links on items")
299
    sub.add_argument('-L', '--no-body-levels', action='store_true',
300
                     help="do not include levels on non-heading items")
301
302
                                                                           # add 27.11.2019 / 11.12.2019
303
def _valMatrix(subs, shared):
304
    """Configure the `doorstop val_Matrix` subparser."""
305
    info = "create a validation matrix for the given or all documents"
306
    sub = subs.add_parser('valMatrix', description=info.capitalize() + '.',
307
                          help=info, **shared)
308
    sub.add_argument('prefix', help="prefix of document to publish  or 'all'")
309
    sub.add_argument('path', nargs='?',
310
                     help="path to published file")
311
312
def _verMatrix(subs, shared):
313
    """Configure the `doorstop _verMatrix` subparser."""
314
    info = "create a verification matrix for the given or all documents"
315
    sub = subs.add_parser('verMatrix', description=info.capitalize() + '.',
316
                          help=info, **shared)
317
    sub.add_argument('prefix', help="prefix of document to publish  or 'all'")
318
    sub.add_argument('path', nargs='?',
319
                     help="path to published file")
320
321
322
def _findRef(subs, shared):
323
    """Find requirements referneces in code"""
324
    info = "get file and line of requirment references"
325
    subs = subs.add_parser('findRef', description = info.capitalize()+'.',
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
326
                            help=info, **shared)
327
    subs.add_argument('UID', help="UID of file to find references")
328
    subs.add_argument('directory', help="Directory to search for references")
329
330
331
def _ref(subs, shared):
332
    """Set new code reference to requirement"""
333
    info= "add new code reference to requirement"
0 ignored issues
show
Coding Style introduced by
Exactly one space required before assignment
Loading history...
334
    subs = subs.add_parser('ref', description= info.capitalize()+'.',
0 ignored issues
show
Coding Style introduced by
No space allowed after keyword argument assignment
Loading history...
335
                            help=info, **shared)
336
    subs.add_argument('UID', help="UID file to add new reference")
337
    subs.add_argument('reference', help="String that is used as reference in code")
338
339
340
def _clearRef(subs, shared):
341
    """Clear suspect references"""
342
    info = "clear suspect references"
343
    subs = subs.add_parser('clearRef', description= info.capitalize()+'.',
0 ignored issues
show
Coding Style introduced by
No space allowed after keyword argument assignment
Loading history...
344
                            help=info, **shared)
345
    subs.add_argument('label', help="item UID, document prefix, or 'all'")
346
    subs.add_argument('referenceID', help="String that is used as reference or 'all'")
347
    group = subs.add_mutually_exclusive_group()
348
    group.add_argument('-i', '--item', action='store_true',
349
                       help="indicates the 'label' is an item UID")
350
    group.add_argument('-d', '--document', action='store_true',
351
                       help="indicates the 'label' is a document prefix")
352
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
353
                                                                             # end add 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
354
355
if __name__ == '__main__':  # pragma: no cover (manual test)
356
    main()
357