Passed
Push — master ( 788cba...457845 )
by John
02:18
created

bbarchivist.scripts.tclnewprd   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 137
Duplicated Lines 36.5 %

Importance

Changes 0
Metric Value
eloc 83
dl 50
loc 137
rs 10
c 0
b 0
f 0
wmc 11

5 Functions

Rating   Name   Duplication   Size   Complexity  
A questionnaire() 0 7 1
B tclnewprd_main() 0 24 1
A grab_args() 50 50 3
A questionnaire_prds() 0 11 2
A execute_args() 0 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 scriptutils  # script frontends
10
from bbarchivist import utilities  # bool
11
12
__author__ = "Thurask"
13
__license__ = "WTFPL v2"
14
__copyright__ = "2017-2018 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
        args = parser.parse_args(sys.argv[1:])
65
        parser.set_defaults()
66
        execute_args(args)
67
68
69
def questionnaire():
70
    """
71
    Questions to ask if no arguments given.
72
    """
73
    prds = questionnaire_prds()
74
    tclnewprd_main(prds=prds, floor=1, ceiling=61)
75
    decorators.enter_to_exit(True)
76
77
78
def questionnaire_prds():
79
    """
80
    Ask about individual versus full scanning.
81
    """
82
    selectbool = utilities.i2b("SCAN SELECTED PRDS (Y/N)?: ")
83
    if selectbool:
84
        prdin = input("ENTER PRD(S) TO SCAN (ex. 63116 63734 63737): ")
85
        prds = prdin.split(" ")
86
    else:
87
        prds = None
88
    return prds
89
90
91
def execute_args(args):
92
    """
93
    Get args and decide what to do with them.
94
95
    :param args: Arguments.
96
    :type args: argparse.Namespace
97
    """
98
    if not args.prds:
99
        args.prds = None
100
    if args.ceiling is None:
101
        args.ceiling = args.floor + 59  # default range
102
    if args.ceiling < args.floor:
103
        print("INVALID RANGE!")
104
        raise SystemExit
105
    args.ceiling += 1  # because range() is a half-open interval
106
    tclnewprd_main(args.prds, args.floor, args.ceiling, args.export, args.noprefix)
107
108
109
def tclnewprd_main(prds=None, floor=1, ceiling=60, export=False, noprefix=False):
110
    """
111
    Scan every PRD and produce latest versions.
112
113
    :param prds: Specific PRD(s) to check, None if all will be checked. Default is None.
114
    :type prds: list(str)
115
116
    :param floor: When to start. Default is 1.
117
    :type floor: int
118
119
    :param ceiling: When to stop. Default is 60.
120
    :type ceiling: int
121
122
    :param export: Whether to export XML response to file. Default is False.
123
    :type export: bool
124
125
    :param noprefix: Whether to skip adding "PRD-" prefix. Default is False.
126
    :type noprefix: bool
127
    """
128
    argutils.slim_preamble("TCLNEWPRD")
129
    prdbase = jsonutils.load_json("prds")
130
    prddict = scriptutils.tcl_findprd_prepdict(prdbase)
131
    prddict = scriptutils.tcl_findprd_checkfilter(prddict, prds)
132
    scriptutils.tcl_findprd(prddict, floor, ceiling, export, noprefix)
133
134
135
if __name__ == "__main__":
136
    grab_args()
137