Completed
Push — master ( de6c4c...cc1262 )
by John
03:54
created

extract_cert_check()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 16
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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_secret(key, device):
54
    """
55
    Check if device is marked as secret.
56
57
    :param key: Device entry.
58
    :type key: dict
59
60
    :param device: HWID, FCCID or name of device.
61
    :param device: str
62
    """
63 5
    not_secret = device in key['name'] and 'secret' not in key
64 5
    return not_secret
65
66
67 5
def extract_cert_check(key, device, not_secret):
68
    """
69
    Check function for extracting PTCRB info.
70
71
    :param key: Device entry.
72
    :type key: dict
73
74
    :param device: HWID, FCCID or name of device.
75
    :type device: str
76
77
    :param not_secret: If device is not market as secret.
78
    :type not_secret: bool
79
    """
80 5
    keylist = key['hwid'], key['fccid'], key['ptcrbid']
81 5
    goforit = (device in keylist or not_secret) and key['ptcrbid']
82 5
    return goforit
83
84
85 5
def extract_cert(table, device):
86
    """
87
    Extract PTCRB info from a list of dicts.
88
89
    :param table: List of device entries.
90
    :type table: list(dict)
91
92
    :param device: HWID, FCCID or name of device.
93
    :type device: str
94
    """
95 5
    for key in table:
96 5
        not_secret = extract_cert_secret(key, device)
97 5
        goforit = extract_cert_check(key, device, not_secret)
98 5
        if goforit:
99 5
            device = key['device']
100 5
            name = key['name']
101 5
            ptcrbid = key['ptcrbid']
102 5
            hwid = key['hwid']
103 5
            fccid = key['fccid']
104 5
            break
105
    else:
106 5
        fubar("NO PTCRBID!")
107 5
    return name, ptcrbid, hwid, fccid
108
109
110 5
def list_available_certs(table):
111
    """
112
    List all certified devices in a device table.
113
114
    :param table: List of device entries.
115
    :type table: list(dict)
116
    """
117 5
    for key in table:
118 5
        if key['ptcrbid']:
119 5
            hwid = "NO HWID" if not key['hwid'] else key['hwid']
120 5
            print("{0} {1} - {2} - {3}".format(key['device'], key['name'], hwid, key['fccid']))
121
122
123 5
def list_devices(table):
124
    """
125
    List all devices, certified or not, in a device table.
126
127
    :param table: List of device entries.
128
    :type table: list(dict)
129
    """
130 5
    for key in table:
131 5
        hwid = "NO HWID" if not key['hwid'] else key['hwid']
132 5
        fccid = "NO FCCID" if not key['fccid'] else key['fccid']
133 5
        print("{0} {1} - {2} - {3}".format(key['device'], key['name'], hwid, fccid))
134
135
136 5
def list_prds(table):
137
    """
138
    List all PRDs in a PRD table.
139
140
    :param table: Dictionary of device : PRD list pairs.
141
    :type table: dict(str: list)
142
    """
143 5
    for key in table.keys():
144 5
        print("~{0}~".format(key))
145 5
        for prd in table[key]:
146 5
            print(prd)
147
148
149 5
def certchecker_prep(table, device):
150
    """
151
    Extract model, family and HWID from a device table.
152
153
    :param table: List of device entries.
154
    :type table: list(dict)
155
156
    :param device: HWID, FCCID or name of device.
157
    :type device: str
158
    """
159 5
    for key in table:
160 5
        if 'secret' not in key and key['name'] == device:
161 5
            model = key['device']
162 5
            family = key['family']
163 5
            hwid = key['hwid']
164 5
            break
165
    else:
166 5
        fubar("INVALID DEVICE!")
167 5
    return model, family, hwid
168
169
170 5
def read_family(table, device):
171
    """
172
    Get all devices of a given family in a device table.
173
174
    :param table: List of device entries.
175
    :type table: list(dict)
176
177
    :param device: HWID, FCCID or name of device.
178
    :type device: str
179
    """
180 5
    famlist = [key['fccid'] for key in table if key['ptcrbid'] and key['device'] == device]
181 5
    return famlist
182
183
184 5
def list_family(table):
185
    """
186
    List all valid (certified) families in a device table.
187
188
    :param table: List of device entries.
189
    :type table: list(dict)
190
    """
191 5
    famlist = list({key['device'] for key in table if 'secret' not in key and key['ptcrbid']})
192 5
    utilities.lprint(famlist)
193
194
195 5
def fubar(message):
196
    """
197
    What to do when things go bad.
198
199
    :param message: Error message.
200
    :type message: str
201
    """
202 5
    print(message)
203 5
    decorators.enter_to_exit(True)
204 5
    if not getattr(sys, 'frozen', False):
205
        raise SystemExit
206