Completed
Push — master ( d75233...e013eb )
by John
02:04
created

execute_args_end()   A

Complexity

Conditions 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
dl 0
loc 17
rs 9.2
1
#!/usr/bin/env python3
2
"""Checks certifications for a given device."""
3
4
import sys  # load arguments
5
from bbarchivist import decorators  # enter to exit
6
from bbarchivist import networkutils  # check function
7
from bbarchivist import jsonutils  # json
8
from bbarchivist import scriptutils  # default parser
9
from bbarchivist import utilities  # lprint
10
11
__author__ = "Thurask"
12
__license__ = "WTFPL v2"
13
__copyright__ = "2015-2017 Thurask"
14
15
16
def grab_args():
17
    """
18
    Parse arguments from argparse/questionnaire.
19
20
    Invoke :func:`certchecker.certchecker_main` with arguments.
21
    """
22
    if len(sys.argv) > 1:
23
        datafile = jsonutils.load_json('devices')
0 ignored issues
show
Unused Code introduced by
The variable datafile seems to be unused.
Loading history...
24
        parser = scriptutils.default_parser("bb-certchecker", "Certification scraper")
25
        parser.add_argument(
26
            "device",
27
            help="FCCID/HWID/model #, or family",
28
            nargs="?",
29
            default=None)
30
        parser.add_argument(
31
            "-f",
32
            "--family",
33
            dest="family",
34
            help="Return all certs of a device family",
35
            action="store_true",
36
            default=False)
37
        fgroup = parser.add_mutually_exclusive_group()
38
        fgroup.add_argument(
39
            "-d",
40
            "--database",
41
            dest="database",
42
            help="List all devices in database",
43
            action="store_true",
44
            default=False)
45
        fgroup.add_argument(
46
            "-c",
47
            "--certs",
48
            dest="certs",
49
            help="List certified devices in database",
50
            action="store_true",
51
            default=False)
52
        fgroup.add_argument(
53
            "-l",
54
            "--list",
55
            dest="list",
56
            help="List families in database",
57
            action="store_true",
58
            default=False)
59
        parser.set_defaults()
60
        args = parser.parse_args(sys.argv[1:])
61
        execute_args(args)
62
    else:
63
        device = scriptutils.questionnaire_device("DEVICE (XXX100-#/FCCID/HWID): ")
64
        print(" ")
65
        certchecker_main(device)
66
        decorators.enter_to_exit(True)
67
68
69
def execute_args(args):
70
    """
71
    Get args and decide what to do with them.
72
73
    :param args: Arguments.
74
    :type args: argparse.Namespace
75
    """
76
    if args.database:
77
        jsonutils.list_devices(datafile)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'datafile'
Loading history...
78
    elif args.certs:
79
        jsonutils.list_available_certs(datafile)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'datafile'
Loading history...
80
    elif args.list:
81
        jsonutils.list_family(datafile)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'datafile'
Loading history...
82
    else:
83
        execute_args_end(args)
84
85
86
def execute_args_end(args):
87
    """
88
    Continue the first half.
89
90
    :param args: Arguments.
91
    :type args: argparse.Namespace
92
    """
93
    if args.family:
94
        family = jsonutils.read_family(datafile, args.device.upper())
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'datafile'
Loading history...
95
        for ptcrbid in family:
96
            certchecker_main(ptcrbid)
97
            print("")
98
    elif args.device is None:
99
        print("NO DEVICE SPECIFIED!")
100
        raise SystemExit
101
    else:
102
        certchecker_main(args.device)
103
104
105
def certchecker_main(device):
106
    """
107
    Wrap around :mod:`bbarchivist.networkutils` certification checking.
108
109
    :param device: Hardware ID, PTCRB ID, FCC ID or model number.
110
    :type device: str
111
    """
112
    data = jsonutils.load_json('devices')
113
    device = device.upper()
114
    name, ptcrbid, hwid, fccid = jsonutils.extract_cert(data, device)
115
    scriptutils.slim_preamble("CERTCHECKER")
116
    print("DEVICE: {0}".format(device.upper()))
117
    print("VARIANT: {0}".format(name.upper()))
118
    if hwid:
119
        print("HARDWARE ID: {0}".format(hwid.upper()))
120
    if fccid:
121
        print("FCC ID: {0}".format(fccid.upper()))
122
    print("\nCHECKING CERTIFICATIONS...\n")
123
    certlist = networkutils.ptcrb_scraper(ptcrbid)
124
    utilities.lprint(certlist)
125
126
if __name__ == "__main__":
127
    grab_args()
128