Completed
Push — master ( ce03d6...32fc37 )
by John
03:28
created

list_prds()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
ccs 5
cts 5
cp 1
crap 3
1
#!/usr/bin/env python3
2 5
"""This module is used for JSON tools."""
3
4 5
import os  # path work
5 5
try:
6 5
    import simplejson as json
7 1
except ImportError:
8 1
    import json
9 5
import glob  # filenames
10 5
import sys  # frozen status
11 5
from bbarchivist import bbconstants  # file location
12 5
from bbarchivist import decorators  # enter to exit
13 5
from bbarchivist import utilities  # lprint
14
15 5
__author__ = "Thurask"
16 5
__license__ = "WTFPL v2"
17 5
__copyright__ = "2015-2017 Thurask"
18
19
20 5
def grab_json(filename):
21
    """
22
    Figure out where JSON is, local or system-supplied.
23
24
    :param filename: Desired JSON database name.
25
    :type filename: str
26
    """
27 5
    jfile = None
28 5
    try:
29 5
        jfile = glob.glob(os.path.join(os.getcwd(), "json"))[0]
30 5
    except IndexError:
31 5
        jfile = bbconstants.JSONDIR
32 5
    jfile = os.path.join(jfile, "{0}.json".format(filename))
33 5
    return os.path.abspath(jfile)
34
35
36 5
def load_json(table, jfile=None):
37
    """
38
    Load JSON file, return specific table (dict or list).
39
40
    :param table: Name of sub-structure to return.
41
    :type table: str
42
43
    :param jfile: Path to JSON file.
44
    :type jfile: str
45
    """
46 5
    if jfile is None:
47 5
        jfile = grab_json(table)
48 5
    with open(jfile) as thefile:
49 5
        data = json.load(thefile)
50 5
    return data[table]
51
52
53 5
def extract_cert(table, device):
54
    """
55
    Extract PTCRB info from a list of dicts.
56
57
    :param table: List of device entries.
58
    :type table: list(dict)
59
60
    :param device: HWID, FCCID or name of device.
61
    :type device: str
62
    """
63 5
    for key in table:
64 5
        keylist = key['hwid'], key['fccid'], key['ptcrbid']
65 5
        not_secret = device in key['name'] and 'secret' not in key
66 5
        if (device in keylist or not_secret) and key['ptcrbid']:
67 5
            device = key['device']
68 5
            name = key['name']
69 5
            ptcrbid = key['ptcrbid']
70 5
            hwid = key['hwid']
71 5
            fccid = key['fccid']
72 5
            break
73
    else:
74 5
        fubar("NO PTCRBID!")
75 5
    return name, ptcrbid, hwid, fccid
76
77
78 5
def list_available_certs(table):
79
    """
80
    List all certified devices in a device table.
81
82
    :param table: List of device entries.
83
    :type table: list(dict)
84
    """
85 5
    for key in table:
86 5
        if key['ptcrbid']:
87 5
            hwid = "NO HWID" if not key['hwid'] else key['hwid']
88 5
            print("{0} {1} - {2} - {3}".format(key['device'], key['name'], hwid, key['fccid']))
89
90
91 5
def list_devices(table):
92
    """
93
    List all devices, certified or not, in a device table.
94
95
    :param table: List of device entries.
96
    :type table: list(dict)
97
    """
98 5
    for key in table:
99 5
        hwid = "NO HWID" if not key['hwid'] else key['hwid']
100 5
        fccid = "NO FCCID" if not key['fccid'] else key['fccid']
101 5
        print("{0} {1} - {2} - {3}".format(key['device'], key['name'], hwid, fccid))
102
103
104 5
def list_prds(table):
105
    """
106
    List all devices, certified or not, in a device table.
107
108
    :param table: Dictionary of device : PRD list pairs.
109
    :type table: dict(str: list)
110
    """
111 5
    for key in table.keys():
112 5
        print("~{0}~".format(key))
113 5
        for prd in table[key]:
114 5
            print(prd)
115
116
117 5
def certchecker_prep(table, device):
118
    """
119
    Extract model, family and HWID from a device table.
120
121
    :param table: List of device entries.
122
    :type table: list(dict)
123
124
    :param device: HWID, FCCID or name of device.
125
    :type device: str
126
    """
127 5
    for key in table:
128 5
        if 'secret' not in key and key['name'] == device:
129 5
            model = key['device']
130 5
            family = key['family']
131 5
            hwid = key['hwid']
132 5
            break
133
    else:
134 5
        fubar("INVALID DEVICE!")
135 5
    return model, family, hwid
136
137
138 5
def read_family(table, device):
139
    """
140
    Get all devices of a given family in a device table.
141
142
    :param table: List of device entries.
143
    :type table: list(dict)
144
145
    :param device: HWID, FCCID or name of device.
146
    :type device: str
147
    """
148 5
    famlist = [key['fccid'] for key in table if key['ptcrbid'] and key['device'] == device]
149 5
    return famlist
150
151
152 5
def list_family(table):
153
    """
154
    List all valid (certified) families in a device table.
155
156
    :param table: List of device entries.
157
    :type table: list(dict)
158
    """
159 5
    famlist = list({key['device'] for key in table if 'secret' not in key and key['ptcrbid']})
160 5
    utilities.lprint(famlist)
161
162
163 5
def fubar(message):
164
    """
165
    What to do when things go bad.
166
167
    :param message: Error message.
168
    :type message: str
169
    """
170 5
    print(message)
171 5
    decorators.enter_to_exit(True)
172 5
    if not getattr(sys, 'frozen', False):
173
        raise SystemExit
174