Completed
Push — master ( c4a23e...ad91b2 )
by John
03:13
created

certchecker_family()   A

Complexity

Conditions 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
c 1
b 1
f 0
dl 0
loc 15
rs 9.4285
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
    datafile = jsonutils.load_json('devices')
23
    if len(sys.argv) > 1:
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, datafile)
62
    else:
63
        device = scriptutils.questionnaire_device("DEVICE (XXX100-#/FCCID/HWID): ")
64
        print(" ")
65
        certchecker_main(device, datafile)
66
        decorators.enter_to_exit(True)
67
68
69
def execute_args(args, datafile):
70
    """
71
    Get args and decide what to do with them.
72
73
    :param args: Arguments.
74
    :type args: argparse.Namespace
75
76
    :param datafile: List of device entries.
77
    :type datafile: list(dict)
78
    """
79
    if args.database:
80
        jsonutils.list_devices(datafile)
81
    elif args.certs:
82
        jsonutils.list_available_certs(datafile)
83
    elif args.list:
84
        jsonutils.list_family(datafile)
85
    else:
86
        execute_args_end(args, datafile)
87
88
89
def execute_args_end(args, datafile):
90
    """
91
    Continue the first half.
92
93
    :param args: Arguments.
94
    :type args: argparse.Namespace
95
96
    :param datafile: List of device entries.
97
    :type datafile: list(dict)
98
    """
99
    if args.family:
100
        certchecker_family(args, datafile)
101
    elif args.device is None:
102
        print("NO DEVICE SPECIFIED!")
103
        raise SystemExit
104
    else:
105
        certchecker_main(args.device, datafile)
106
107
108
def certchecker_family(args, datafile):
109
    """
110
    Output all devices in a family.
111
112
    :param args: Arguments.
113
    :type args: argparse.Namespace
114
115
    :param datafile: List of device entries.
116
    :type datafile: list(dict)
117
    """
118
    family = jsonutils.read_family(datafile, args.device.upper())
119
    for ptcrbid in family:
120
        certchecker_main(ptcrbid, datafile)
121
        if ptcrbid != family[-1]:
122
            print("")
123
124
125
def certchecker_main(device, data):
126
    """
127
    Wrap around :mod:`bbarchivist.networkutils` certification checking.
128
129
    :param device: Hardware ID, PTCRB ID, FCC ID or model number.
130
    :type device: str
131
132
    :param data: List of device entries.
133
    :type data: list(dict)
134
    """
135
    device = device.upper()
136
    name, ptcrbid, hwid, fccid = jsonutils.extract_cert(data, device)
137
    scriptutils.slim_preamble("CERTCHECKER")
138
    print("DEVICE: {0}".format(device.upper()))
139
    print("VARIANT: {0}".format(name.upper()))
140
    if hwid:
141
        print("HARDWARE ID: {0}".format(hwid.upper()))
142
    if fccid:
143
        print("FCC ID: {0}".format(fccid.upper()))
144
    print("\nCHECKING CERTIFICATIONS...\n")
145
    certlist = networkutils.ptcrb_scraper(ptcrbid)
146
    utilities.lprint(sorted(certlist, reverse=True))
147
148
149
if __name__ == "__main__":
150
    grab_args()
151