Completed
Push — master ( 396e01...42b6b4 )
by John
02:55
created

app_individual_writer()   A

Complexity

Conditions 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/env python3
2 4
"""This module is used for generation of URLs and related text files."""
3
4 4
from bbarchivist.networkutils import get_length  # network
5 4
from bbarchivist.utilities import create_bar_url, fsizer, generate_urls, stripper  # utils
6
7 4
__author__ = "Thurask"
8 4
__license__ = "WTFPL v2"
9 4
__copyright__ = "2015-2017 Thurask"
10
11
12 4
def system_link_writer(target, urls, avlty=False):
13
    """
14
    Write OS/radio links to file.
15
16
    :param target: File to write to.
17
    :type target: file
18
19
    :param urls: Dictionary of URLs; name: URL
20
    :type urls: dict(str: str)
21
22
    :param avlty: If this OS release is available. Default is false.
23
    :type avlty: bool
24
    """
25 4
    for key, val in urls.items():
26 4
        if avlty:
27 4
            fsize = get_length(val)
28
        else:
29 4
            fsize = None
30 4
        system_individual_writer(key, val, target, fsize)
31
32
33 4
def get_fnone(fsize):
34
    """
35
    Get sentinel value for filesize when writing OS/radio links.
36
37
    :param fsize: Filesize.
38
    :type fsize: int
39
    """
40 4
    return True if fsize is None else False
41
42
43 4
def system_individual_writer(appname, appurl, target, fsize):
44
    """
45
    Write individual OS/radio link to file.
46
47
    :param appname: App name.
48
    :type appname: str
49
50
    :param appurl: App URL.
51
    :type appurl: str
52
53
    :param target: File to write to.
54
    :type target: file
55
56
    :param fsize: Filesize.
57
    :type fsize: int
58
    """
59 4
    fnone = get_fnone(fsize)
60 4
    if fnone:
61 4
        target.write("{0} [{1}] {2}\n".format(appname, fsizer(0), appurl))
62 4
    elif fsize > 0:
63 4
        target.write("{0} [{1}] {2}\n".format(appname, fsizer(fsize), appurl))
64
65
66 4
def app_individual_writer(app, target):
67
    """
68
    Write individual app link to file.
69
70
    :param app: App URL.
71
    :type app: str
72
73
    :param target: File to write to.
74
    :type target: file
75
    """
76 4
    fsize = get_length(app)
77 4
    base = app.split('/')[-1]
78 4
    base = stripper(base)
79 4
    if fsize > 0:
80 4
        target.write("{0} [{1}] {2}\n".format(base, fsizer(fsize), app))
81
82
83 4
def app_link_writer(target, urls):
84
    """
85
    Write app links to file.
86
87
    :param target: File to write to.
88
    :type target: file
89
90
    :param urls: Dictionary of URLs; name: URL
91
    :type urls: dict(str: str)
92
    """
93 4
    stoppers = ["8960", "8930", "8974", "m5730", "winchester"]
94 4
    for app in urls:
95 4
        if all(word not in app for word in stoppers):
96 4
            app_individual_writer(app, target)
97
98
99 4
def dev_link_writer(target, finals):
100
    """
101
    Write Dev Alpha autooloader links to file.
102
103
    :param target: File to write to.
104
    :type target: file
105
106
    :param finals: Dict of URL:content-length pairs.
107
    :type finals: dict(str: str)
108
    """
109 4
    for key, val in finals.items():
110 4
        target.write("{0} [{1}]\n".format(key, fsizer(int(val))))
111
112
113 4
def prep_thename(softwareversion, appendbars=False, temp=False):
114
    """
115
    Generate name for output file.
116
117
    :param softwareversion: Software release version.
118
    :type softwareversion: str
119
120
    :param appendbars: Whether to add app bars to file. Default is false.
121
    :type appendbars: bool
122
123
    :param temp: If file we write to is temporary. Default is false.
124
    :type temp: bool
125
    """
126 4
    thename = softwareversion
127 4
    if appendbars:
128 4
        thename += "plusapps"
129 4
    if temp:
130 4
        thename = "TEMPFILE"
131 4
    return thename
132
133
134 4
def write_altsw(target, altsw):
135
    """
136
    Write alternate software release to file.
137
138
    :param target: File to write to.
139
    :type target: file
140
141
    :param altsw: Radio software release version, if different.
142
    :type altsw: str
143
    """
144 4
    if altsw is not None:
145 4
        target.write("RADIO SOFTWARE RELEASE: {0}\n".format(altsw))
146
147
148 4
def write_disclaimer(target, avlty):
149
    """
150
    Write availability disclaimer to file.
151
152
    :param target: File to write to.
153
    :type target: file
154
155
    :param avlty: Availability of links to download. Default is false.
156
    :type avlty: bool
157
    """
158 4
    if not avlty:
159 4
        target.write("\n!!EXISTENCE NOT GUARANTEED!!\n")
160
161
162 4
def write_appbars(target, appendbars, appurls):
163
    """
164
    Write app bar links to file.
165
166
    :param target: File to write to.
167
    :type target: file
168
169
    :param appendbars: Whether to add app bars to file. Default is false.
170
    :type appendbars: bool
171
172
    :param appurls: App bar URLs to add.
173
    :type softwareversion: list
174
    """
175 4
    if appendbars:
176 4
        target.write("\nAPP URLS:\n")
177 4
        app_link_writer(target, appurls)
178
179
180 4
def write_signedbars(target, urllist, avlty, message):
181
    """
182
    Write debrick/core/radio URLs to file.
183
184
    :param target: File to write to.
185
    :type target: file
186
187
    :param urllist: List of URLs to write.
188
    :type urllist: list(str)
189
190
    :param avlty: Availability of links to download. Default is false.
191
    :type avlty: bool
192
193
    :param message: Header for this section: debrick, core or radio.
194
    :type message: str
195
    """
196 4
    target.write("\n{0}:\n".format(message))
197 4
    system_link_writer(target, urllist, avlty)
198
199
200 4
def write_header(target, softwareversion, osversion, radioversion):
201
    """
202
    Write header for file.
203
204
    :param target: File to write to.
205
    :type target: file
206
207
    :param softwareversion: Software release version.
208
    :type softwareversion: str
209
210
    :param osversion: OS version.
211
    :type osversion: str
212
213
    :param radioversion: Radio version.
214
    :type radioversion: str
215
    """
216 4
    target.write("OS VERSION: {0}\n".format(osversion))
217 4
    target.write("RADIO VERSION: {0}\n".format(radioversion))
218 4
    target.write("SOFTWARE RELEASE: {0}\n".format(softwareversion))
219
220
221 4
def write_links(softwareversion, osversion, radioversion, osurls, coreurls, radiourls,
222
                avlty=False, appendbars=False, appurls=None, temp=False, altsw=None):
223
    """
224
    Write lookup links to file. Check for availability, can include app bars.
225
226
    :param softwareversion: Software release version.
227
    :type softwareversion: str
228
229
    :param osversion: OS version.
230
    :type osversion: str
231
232
    :param radioversion: Radio version.
233
    :type radioversion: str
234
235
    :param osurls: Pre-formed debrick OS URLs.
236
    :type osurls: dict{str:str}
237
238
    :param coreurls: Pre-formed core OS URLs.
239
    :type coreurls: dict{str:str}
240
241
    :param radiourls: Pre-formed radio URLs.
242
    :type radiourls: dict{str:str}
243
244
    :param avlty: Availability of links to download. Default is false.
245
    :type avlty: bool
246
247
    :param appendbars: Whether to add app bars to file. Default is false.
248
    :type appendbars: bool
249
250
    :param appurls: App bar URLs to add.
251
    :type softwareversion: list
252
253
    :param temp: If file we write to is temporary. Default is false.
254
    :type temp: bool
255
256
    :param altsw: Radio software release version, if different.
257
    :type altsw: str
258
    """
259 4
    thename = prep_thename(softwareversion, appendbars, temp)
260 4
    with open("{0}.txt".format(thename), "w") as target:
261 4
        write_header(target, softwareversion, osversion, radioversion)
262 4
        write_altsw(target, altsw)
263 4
        write_disclaimer(target, avlty)
264 4
        write_signedbars(target, osurls, avlty, "DEBRICK URLS")
265 4
        write_signedbars(target, coreurls, avlty, "CORE URLS")
266 4
        write_signedbars(target, radiourls, avlty, "RADIO URLS")
267 4
        write_appbars(target, appendbars, appurls)
268
269
270 4
def export_devloader(osversion, finals):
271
    """
272
    Export Dev Alpha URLs to file.
273
274
    :param osversion: OS version.
275
    :type osversion: str
276
277
    :param finals: Dict of URL:content-length pairs.
278
    :type finals: dict(str: str)
279
    """
280 4
    with open("DevAlpha-{0}.txt".format(osversion), "w") as target:
281 4
        dev_link_writer(target, finals)
282
283
284 4
def url_gen(osversion, radioversion, softwareversion):
285
    """
286
    Return all debrick, core and radio URLs from given OS, radio software.
287
288
    :param softwareversion: Software release version.
289
    :type softwareversion: str
290
291
    :param osversion: OS version.
292
    :type osversion: str
293
294
    :param radioversion: Radio version.
295
    :type radioversion: str
296
    """
297 4
    radlist = [
298
        "STL100-1",
299
        "STL100-X/P9982",
300
        "STL100-4",
301
        "Q10/Q5/P9983",
302
        "Z30/LEAP/CLASSIC",
303
        "Z3",
304
        "PASSPORT"
305
    ]
306 4
    oslist = [
307
        "STL100-1",
308
        "QC8960",
309
        "VERIZON QC8960",
310
        "Z3",
311
        "PASSPORT"
312
    ]
313 4
    oses, radios, cores = generate_urls(softwareversion, osversion, radioversion, True)
314 4
    vzw = create_bar_url(softwareversion, "qc8960.verizon_sfi.desktop", osversion)
315 4
    oses.insert(2, vzw)
316 4
    cores.insert(2, oses[2].replace(".desktop", ""))
317 4
    ospairs = {}
318 4
    for title, url in zip(oslist, oses):
319 4
        ospairs[title] = url
320 4
    corepairs = {}
321 4
    for title, url in zip(oslist, cores):
322 4
        corepairs[title] = url
323 4
    radiopairs = {}
324 4
    for title, url in zip(radlist, radios):
325 4
        radiopairs[title] = url
326
    return ospairs, corepairs, radiopairs
327