bbarchivist.scripts.tclnewprd.grab_args()   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 57
Code Lines 51

Duplication

Lines 57
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 51
nop 0
dl 57
loc 57
rs 8.6036
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
"""Check for new PRDs for TCL API devices."""
3
4
import sys  # load arguments
5
6
from bbarchivist import argutils  # arguments
7
from bbarchivist import decorators  # enter to exit
8
from bbarchivist import jsonutils  # json
9
from bbarchivist import scriptutilstcl  # script frontends
10
from bbarchivist import utilities  # bool
11
12
__author__ = "Thurask"
13
__license__ = "WTFPL v2"
14
__copyright__ = "2017-2019 Thurask"
15
16
17 View Code Duplication
def grab_args():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
18
    """
19
    Parse arguments from argparse/questionnaire.
20
21
    Invoke a function with those arguments.
22
    """
23
    if getattr(sys, "frozen", False) and len(sys.argv) == 1:
24
        questionnaire()
25
    else:
26
        parser = argutils.default_parser("bb-tclnewprd", "Check for new PRDs for TCL devices")
27
        parser.add_argument(
28
            "prds",
29
            help="Only scan space separated list of PRDs",
30
            default=None,
31
            nargs="*")
32
        parser.add_argument(
33
            "-f",
34
            "--floor",
35
            dest="floor",
36
            help="When to start, default=1",
37
            default=1,
38
            type=int,
39
            choices=range(0, 998),
40
            metavar="INT")
41
        parser.add_argument(
42
            "-c",
43
            "--ceiling",
44
            dest="ceiling",
45
            help="When to stop, default=60",
46
            default=None,
47
            type=int,
48
            choices=range(1, 999),
49
            metavar="INT")
50
        parser.add_argument(
51
            "-x",
52
            "--export",
53
            dest="export",
54
            help="Write XML to logs folder",
55
            action="store_true",
56
            default=False)
57
        parser.add_argument(
58
            "-np",
59
            "--no-prefix",
60
            dest="noprefix",
61
            help="Don't add PRD- prefix",
62
            action="store_true",
63
            default=False)
64
        parser.add_argument(
65
            "-k",
66
            "--key2",
67
            dest="key2mode",
68
            help="Use KEY2 syntax",
69
            action="store_true",
70
            default=False)
71
        args = parser.parse_args(sys.argv[1:])
72
        parser.set_defaults()
73
        execute_args(args)
74
75
76
def questionnaire():
77
    """
78
    Questions to ask if no arguments given.
79
    """
80
    prds = questionnaire_prds()
81
    tclnewprd_main(prds=prds, floor=1, ceiling=61)
82
    decorators.enter_to_exit(True)
83
84
85
def questionnaire_prds():
86
    """
87
    Ask about individual versus full scanning.
88
    """
89
    selectbool = utilities.i2b("SCAN SELECTED PRDS (Y/N)?: ")
90
    if selectbool:
91
        prdin = input("ENTER PRD(S) TO SCAN (ex. 63116 63734 63737): ")
92
        prds = prdin.split(" ")
93
    else:
94
        prds = None
95
    return prds
96
97
98
def execute_args(args):
99
    """
100
    Get args and decide what to do with them.
101
102
    :param args: Arguments.
103
    :type args: argparse.Namespace
104
    """
105
    if not args.prds:
106
        args.prds = None
107
    if args.ceiling is None:
108
        args.ceiling = args.floor + 59  # default range
109
    if args.ceiling < args.floor:
110
        print("INVALID RANGE!")
111
        raise SystemExit
112
    args.ceiling += 1  # because range() is a half-open interval
113
    tclnewprd_main(args.prds, args.floor, args.ceiling, args.export, args.noprefix, args.key2mode)
114
115
116
def tclnewprd_main(prds=None, floor=1, ceiling=60, export=False, noprefix=False, key2mode=False):
117
    """
118
    Scan every PRD and produce latest versions.
119
120
    :param prds: Specific PRD(s) to check, None if all will be checked. Default is None.
121
    :type prds: list(str)
122
123
    :param floor: When to start. Default is 1.
124
    :type floor: int
125
126
    :param ceiling: When to stop. Default is 60.
127
    :type ceiling: int
128
129
    :param export: Whether to export XML response to file. Default is False.
130
    :type export: bool
131
132
    :param noprefix: Whether to skip adding "PRD-" prefix. Default is False.
133
    :type noprefix: bool
134
135
    :param key2mode: Whether to use new-style prefix. Default is False.
136
    :type key2mode: bool
137
    """
138
    argutils.slim_preamble("TCLNEWPRD")
139
    prdbase = jsonutils.load_json("prds")
140
    prddict = scriptutilstcl.tcl_findprd_prepdict(prdbase)
141
    prddict = scriptutilstcl.tcl_findprd_checkfilter(prddict, prds)
142
    scriptutilstcl.tcl_findprd(prddict, floor, ceiling, export, noprefix, key2mode)
143
144
145
if __name__ == "__main__":
146
    grab_args()
147