Completed
Pull Request — develop (#212)
by
unknown
24:09 queued 09:09
created

_publish()   B

Complexity

Conditions 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 1
rs 8.9713
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3 1
"""Command-line interface for Doorstop."""
4
5 1
import os
6 1
import sys
7 1
import argparse
8
9 1
from doorstop import common
10 1
from doorstop.cli import utilities, commands
11
12 1
log = common.logger(__name__)
13
14
15 1
def main(args=None):  # pylint: disable=R0915
16
    """Process command-line arguments and run the program."""
17 1
    from doorstop import CLI, VERSION, DESCRIPTION
18
19
    # Shared options
20 1
    project = argparse.ArgumentParser(add_help=False)
21 1
    project.add_argument('-j', '--project', metavar='PATH',
22
                         help="path to the root of the project")
23 1
    project.add_argument('--no-cache', action='store_true',
24
                         help=argparse.SUPPRESS)
25 1
    server = argparse.ArgumentParser(add_help=False)
26 1
    server.add_argument('--server', metavar='HOST',
27
                        help="IP address or hostname for a running server")
28 1
    server.add_argument('--port', metavar='NUMBER', type=int,
29
                        help="use a custom port for the server")
30 1
    server.add_argument('-f', '--force', action='store_true',
31
                        help="perform the action without the server")
32 1
    debug = argparse.ArgumentParser(add_help=False)
33 1
    debug.add_argument('-V', '--version', action='version', version=VERSION)
34 1
    group = debug.add_mutually_exclusive_group()
35 1
    group.add_argument('-v', '--verbose', action='count', default=0,
36
                       help="enable verbose logging")
37 1
    group.add_argument('-q', '--quiet', action='store_const', const=-1,
38
                       dest='verbose', help="only display errors and prompts")
39 1
    shared = {'formatter_class': common.HelpFormatter,
40
              'parents': [project, server, debug]}
41
42
    # Build main parser
43 1
    parser = argparse.ArgumentParser(prog=CLI, description=DESCRIPTION,
44
                                     **shared)
45 1
    parser.add_argument('-F', '--no-reformat', action='store_true',
46
                        help="do not reformat item files during validation")
47 1
    parser.add_argument('-r', '--reorder', action='store_true',
48
                        help="reorder document levels during validation")
49 1
    parser.add_argument('-L', '--no-level-check', action='store_true',
50
                        help="do not validate document levels")
51 1
    parser.add_argument('-R', '--no-ref-check', action='store_true',
52
                        help="do not validate external file references")
53 1
    parser.add_argument('-C', '--no-child-check', action='store_true',
54
                        help="do not validate child (reverse) links")
55 1
    parser.add_argument('-Z', '--strict-child-check', action='store_true',
56
                        help="require child (reverse) links from every document")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (81/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
57 1
    parser.add_argument('-S', '--no-suspect-check', action='store_true',
58
                        help="do not check for suspect links")
59 1
    parser.add_argument('-W', '--no-review-check', action='store_true',
60
                        help="do not check item review status")
61 1
    parser.add_argument('-s', '--skip', metavar='PREFIX', action='append',
62
                        help="skip a document during validation")
63 1
    parser.add_argument('-w', '--warn-all', action='store_true',
64
                        help="display all info-level issues as warnings")
65 1
    parser.add_argument('-e', '--error-all', action='store_true',
66
                        help="display all warning-level issues as errors")
67
68
    # Build sub-parsers
69 1
    subs = parser.add_subparsers(help="", dest='command', metavar="<command>")
70 1
    _create(subs, shared)
71 1
    _delete(subs, shared)
72 1
    _add(subs, shared)
73 1
    _remove(subs, shared)
74 1
    _edit(subs, shared)
75 1
    _reorder(subs, shared)
76 1
    _link(subs, shared)
77 1
    _unlink(subs, shared)
78 1
    _clear(subs, shared)
79 1
    _review(subs, shared)
80 1
    _import(subs, shared)
81 1
    _export(subs, shared)
82 1
    _publish(subs, shared)
83
84
    # Parse arguments
85 1
    args = parser.parse_args(args=args)
86
87
    # Configure logging
88 1
    utilities.configure_logging(args.verbose)
89
90
    # Configure settings
91 1
    utilities.configure_settings(args)
92
93
    # Run the program
94 1
    function = commands.get(args.command)
95 1
    try:
96 1
        success = function(args, os.getcwd(), parser.error)
97 1
    except common.DoorstopFileError as exc:
98 1
        log.error(exc)
99 1
        success = False
100 1
    except KeyboardInterrupt:
101 1
        log.debug("command cancelled")
102 1
        success = False
103 1
    if success:
104 1
        log.debug("command succeeded")
105
    else:
106 1
        log.debug("command failed")
107 1
        sys.exit(1)
108
109
110 1
def _create(subs, shared):
111
    """Configure the `doorstop create` subparser."""
112 1
    info = "create a new document directory"
113 1
    sub = subs.add_parser('create', description=info.capitalize() + '.',
114
                          help=info, **shared)
115 1
    sub.add_argument('prefix', help="document prefix for new item UIDs")
116 1
    sub.add_argument('path', help="path to a directory for item files")
117 1
    sub.add_argument('-p', '--parent', help="prefix of parent document")
118 1
    sub.add_argument('-d', '--digits', help="number of digits in item UIDs")
119
120
121 1
def _delete(subs, shared):
122
    """Configure the `doorstop delete` subparser."""
123 1
    info = "delete a document directory"
124 1
    sub = subs.add_parser('delete', description=info.capitalize() + '.',
125
                          help=info, **shared)
126 1
    sub.add_argument('prefix', help="prefix of document to delete")
127
128
129 1
def _add(subs, shared):
130
    """Configure the `doorstop add` subparser."""
131 1
    info = "create an item file in a document directory"
132 1
    sub = subs.add_parser('add', description=info.capitalize() + '.',
133
                          help=info, **shared)
134 1
    sub.add_argument('prefix',
135
                     help="document prefix for the new item")
136 1
    sub.add_argument('-l', '--level', help="desired item level (e.g. 1.2.3)")
137 1
    sub.add_argument('-c', '--count', default=1, type=utilities.positive_int,
138
                     help="number of items to create (default: 1)")
139
140
141 1
def _remove(subs, shared):
142
    """Configure the `doorstop remove` subparser."""
143 1
    info = "remove an item file from a document directory"
144 1
    sub = subs.add_parser('remove', description=info.capitalize() + '.',
145
                          help=info, **shared)
146 1
    sub.add_argument('uid', help="item UID to remove from its document")
147
148
149 1 View Code Duplication
def _edit(subs, shared):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
150
    """Configure the `doorstop edit` subparser."""
151 1
    info = "open an existing item or document for editing"
152 1
    sub = subs.add_parser('edit', description=info.capitalize() + '.',
153
                          help=info, **shared)
154 1
    sub.add_argument('label',
155
                     help="item UID or document prefix to open for editing")
156 1
    group = sub.add_mutually_exclusive_group()
157 1
    group.add_argument('-i', '--item', action='store_true',
158
                       help="indicates the 'label' is an item UID")
159 1
    group.add_argument('-d', '--document', action='store_true',
160
                       help="indicates the 'label' is a document prefix")
161 1
    group = sub.add_mutually_exclusive_group()
162 1
    group.add_argument('-y', '--yaml', action='store_true',
163
                       help="edit document as exported YAML (default)")
164 1
    group.add_argument('-c', '--csv', action='store_true',
165
                       help="edit document as exported CSV")
166 1
    group.add_argument('-t', '--tsv', action='store_true',
167
                       help="edit document as exported TSV")
168 1
    group.add_argument('-x', '--xlsx', action='store_true',
169
                       help="edit document as exported XLSX")
170 1
    sub.add_argument('-T', '--tool', metavar='PROGRAM',
171
                     help="text editor to open the document item")
172
173
174 1
def _reorder(subs, shared):
175
    """Configure the `doorstop reorder` subparser."""
176 1
    info = "organize the outline structure of a document"
177 1
    sub = subs.add_parser('reorder', description=info.capitalize() + '.',
178
                          help=info, **shared)
179 1
    sub.add_argument('prefix', help="prefix of document to reorder")
180 1
    group = sub.add_mutually_exclusive_group()
181 1
    group.add_argument('-a', '--auto', action='store_true',
182
                       help="only perform automatic item reordering")
183 1
    group.add_argument('-m', '--manual', action='store_true',
184
                       help="do not automatically reorder the items")
185 1
    sub.add_argument('-T', '--tool', metavar='PROGRAM',
186
                     help="text editor to open the document index")
187
188
189 1
def _link(subs, shared):
190
    """Configure the `doorstop link` subparser."""
191 1
    info = "add a new link between two items"
192 1
    sub = subs.add_parser('link', description=info.capitalize() + '.',
193
                          help=info, **shared)
194 1
    sub.add_argument('child',
195
                     help="child item UID to link to the parent")
196 1
    sub.add_argument('parent',
197
                     help="parent item UID to link from the child")
198
199
200 1
def _unlink(subs, shared):
201
    """Configure the `doorstop unlink` subparser."""
202 1
    info = "remove a link between two items"
203 1
    sub = subs.add_parser('unlink', description=info.capitalize() + '.',
204
                          help=info, **shared)
205 1
    sub.add_argument('child',
206
                     help="child item UID to unlink from parent")
207 1
    sub.add_argument('parent',
208
                     help="parent item UID child is linked to")
209
210
211 1
def _clear(subs, shared):
212
    """Configure the `doorstop clear` subparser."""
213 1
    info = "absolve items of their suspect link status"
214 1
    sub = subs.add_parser('clear', description=info.capitalize() + '.',
215
                          help=info, **shared)
216 1
    sub.add_argument('label', help="item UID, document prefix, or 'all'")
217 1
    group = sub.add_mutually_exclusive_group()
218 1
    group.add_argument('-i', '--item', action='store_true',
219
                       help="indicates the 'label' is an item UID")
220 1
    group.add_argument('-d', '--document', action='store_true',
221
                       help="indicates the 'label' is a document prefix")
222
223
224 1
def _review(subs, shared):
225
    """Configure the `doorstop review` subparser."""
226 1
    info = "absolve items of their unreviewed status"
227 1
    sub = subs.add_parser('review', description=info.capitalize() + '.',
228
                          help=info, **shared)
229 1
    sub.add_argument('label', help="item UID, document prefix, or 'all'")
230 1
    group = sub.add_mutually_exclusive_group()
231 1
    group.add_argument('-i', '--item', action='store_true',
232
                       help="indicates the 'label' is an item UID")
233 1
    group.add_argument('-d', '--document', action='store_true',
234
                       help="indicates the 'label' is a document prefix")
235
236
237 1 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...
238
    """Configure the `doorstop import` subparser."""
239 1
    info = "import an existing document or item"
240 1
    sub = subs.add_parser('import', description=info.capitalize() + '.',
241
                          help=info, **shared)
242 1
    sub.add_argument('path', nargs='?',
243
                     help="path to previously exported document file")
244 1
    sub.add_argument('prefix', nargs='?', help="prefix of document for import")
245 1
    group = sub.add_mutually_exclusive_group()
246 1
    group.add_argument('-d', '--document', nargs=2, metavar='ARG',
247
                       help="import an existing document by: PREFIX PATH")
248 1
    group.add_argument('-i', '--item', nargs=2, metavar='ARG',
249
                       help="import an existing item by: PREFIX UID")
250 1
    sub.add_argument('-p', '--parent', metavar='PREFIX',
251
                     help="parent document prefix for imported document")
252 1
    sub.add_argument('-a', '--attrs', metavar='DICT',
253
                     help="dictionary of item attributes to import")
254 1
    sub.add_argument('-m', '--map', metavar='DICT',
255
                     help="dictionary of custom item attribute names")
256
257
258 1 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...
259
    """Configure the `doorstop export` subparser."""
260 1
    info = "export a document as YAML or another format"
261 1
    sub = subs.add_parser('export', description=info.capitalize() + '.',
262
                          help=info, **shared)
263 1
    sub.add_argument('prefix', help="prefix of document to export or 'all'")
264 1
    sub.add_argument('path', nargs='?',
265
                     help="path to exported file or directory for 'all'")
266 1
    group = sub.add_mutually_exclusive_group()
267 1
    group.add_argument('-y', '--yaml', action='store_true',
268
                       help="output YAML (default when no path)")
269 1
    group.add_argument('-c', '--csv', action='store_true',
270
                       help="output CSV (default for 'all')")
271 1
    group.add_argument('-t', '--tsv', action='store_true',
272
                       help="output TSV")
273 1
    group.add_argument('-x', '--xlsx', action='store_true',
274
                       help="output XLSX")
275 1
    sub.add_argument('-w', '--width', type=int,
276
                     help="limit line width on text output")
277
278
279 1
def _publish(subs, shared):
280
    """Configure the `doorstop publish` subparser."""
281 1
    info = "publish a document as text or another format"
282 1
    sub = subs.add_parser('publish', description=info.capitalize() + '.',
283
                          help=info, **shared)
284 1
    sub.add_argument('prefix', help="prefix of document to publish or 'all'")
285 1
    sub.add_argument('path', nargs='?',
286
                     help="path to published file or directory for 'all'")
287 1
    group = sub.add_mutually_exclusive_group()
288 1
    group.add_argument('-t', '--text', action='store_true',
289
                       help="output text (default when no path)")
290 1
    group.add_argument('-m', '--markdown', action='store_true',
291
                       help="output Markdown")
292 1
    group.add_argument('-H', '--html', action='store_true',
293
                       help="output HTML (default for 'all')")
294 1
    sub.add_argument('-w', '--width', type=int,
295
                     help="limit line width on text output")
296 1
    sub.add_argument('-C', '--no-child-links', action='store_true',
297
                     help="do not include child links on items")
298 1
    sub.add_argument('-L', dest='no_body_levels', action='store_true',
299
                     default=False,
300
                     help="do not include levels on non-heading items")
301
    sub.add_argument('--no-levels', choices=['all', 'body'],
302
                     help="do not include levels on heading and non-heading or non-heading items")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (98/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
303
304
305
if __name__ == '__main__':  # pragma: no cover (manual test)
306
    main()
307