Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 42b6b41

Browse files
committed
add zero-length filter to textgenerator
1 parent 396e01f commit 42b6b41

File tree

3 files changed

+84
-32
lines changed

3 files changed

+84
-32
lines changed

bbarchivist/textgenerator.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,57 @@ def system_link_writer(target, urls, avlty=False):
2727
fsize = get_length(val)
2828
else:
2929
fsize = None
30-
target.write("{0} [{1}] {2}\n".format(key, fsizer(fsize), val))
30+
system_individual_writer(key, val, target, fsize)
31+
32+
33+
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+
return True if fsize is None else False
41+
42+
43+
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+
fnone = get_fnone(fsize)
60+
if fnone:
61+
target.write("{0} [{1}] {2}\n".format(appname, fsizer(0), appurl))
62+
elif fsize > 0:
63+
target.write("{0} [{1}] {2}\n".format(appname, fsizer(fsize), appurl))
64+
65+
66+
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+
fsize = get_length(app)
77+
base = app.split('/')[-1]
78+
base = stripper(base)
79+
if fsize > 0:
80+
target.write("{0} [{1}] {2}\n".format(base, fsizer(fsize), app))
3181

3282

3383
def app_link_writer(target, urls):
@@ -40,13 +90,10 @@ def app_link_writer(target, urls):
4090
:param urls: Dictionary of URLs; name: URL
4191
:type urls: dict(str: str)
4292
"""
93+
stoppers = ["8960", "8930", "8974", "m5730", "winchester"]
4394
for app in urls:
44-
stoppers = ["8960", "8930", "8974", "m5730", "winchester"]
4595
if all(word not in app for word in stoppers):
46-
fsize = get_length(app)
47-
base = app.split('/')[-1]
48-
base = stripper(base)
49-
target.write("{0} [{1}] {2}\n".format(base, fsizer(fsize), app))
96+
app_individual_writer(app, target)
5097

5198

5299
def dev_link_writer(target, finals):

tests/test_scriptutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_cchecker_export_upgrade(self):
108108
"""
109109
Test exporting links, with upgrade files.
110110
"""
111-
with mock.patch('bbarchivist.networkutils.get_length', mock.MagicMock(return_value=12345)):
111+
with mock.patch('bbarchivist.textgenerator.get_fnone', mock.MagicMock(return_value=True)):
112112
bs.export_cchecker(
113113
["http://sn.ek"],
114114
None,
@@ -119,7 +119,7 @@ def test_cchecker_export_upgrade(self):
119119
True,
120120
None)
121121
with open("10.3.3.3333plusapps.txt", "r") as afile:
122-
assert len(afile.read()) == 2926
122+
assert len(afile.read()) == 2899
123123

124124
def test_cchecker_export_debrick(self):
125125
"""

tests/test_textgenerator.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
from shutil import rmtree
55
import os
6+
try:
7+
import unittest.mock as mock
8+
except ImportError:
9+
import mock
610
import httmock
711
import bbarchivist.textgenerator as bt
812

@@ -44,7 +48,7 @@ def setup_class(cls):
4448
"""
4549
Create necessary links.
4650
"""
47-
deb, cor, rad = bt.url_gen("10.1.1000", "10.2.2000", "10.3.3000")
51+
deb, cor, rad = bt.url_gen("10.1.1.1000", "10.2.2.2000", "10.3.3.3000")
4852
cls.deb = deb
4953
cls.cor = cor
5054
cls.rad = rad
@@ -73,31 +77,32 @@ def test_write_links(self):
7377
"""
7478
Test writing URLs to file.
7579
"""
76-
bt.write_links(
77-
"10.3.3000",
78-
"10.1.1000",
79-
"10.2.2000",
80-
self.deb,
81-
self.cor,
82-
self.rad,
83-
True,
84-
False,
85-
None,
86-
False,
87-
None)
88-
with open("10.3.3000.txt", 'rb') as file:
80+
with mock.patch('bbarchivist.textgenerator.get_fnone', mock.MagicMock(return_value=True)):
81+
bt.write_links(
82+
"10.3.3.3000",
83+
"10.1.1.1000",
84+
"10.2.2.2000",
85+
self.deb,
86+
self.cor,
87+
self.rad,
88+
True,
89+
False,
90+
None,
91+
False,
92+
None)
93+
with open("10.3.3.3000.txt", 'rb') as file:
8994
data = file.read()
9095
data = data.replace(b"\r", b"")
91-
assert len(data) == 2848
96+
assert len(data) == 2888
9297

9398
def test_write_links_unavailable(self):
9499
"""
95100
Test writing URLs, to file, if file existence not guaranteed.
96101
"""
97102
bt.write_links(
98-
"10.3.3000",
99-
"10.1.1000",
100-
"10.2.2000",
103+
"10.3.3.3000",
104+
"10.1.1.1000",
105+
"10.2.2.2000",
101106
self.deb,
102107
self.cor,
103108
self.rad,
@@ -106,10 +111,10 @@ def test_write_links_unavailable(self):
106111
None,
107112
False,
108113
None)
109-
with open("10.3.3000.txt", 'rb') as file:
114+
with open("10.3.3.3000.txt", 'rb') as file:
110115
data = file.read()
111116
data = data.replace(b"\r", b"")
112-
assert len(data) == 2878
117+
assert len(data) == 2918
113118

114119
def test_write_links_withapps(self):
115120
"""
@@ -118,9 +123,9 @@ def test_write_links_withapps(self):
118123
apps = ["http://APP#1.bar", "http://APP#2.bar", "http://APP#3.bar"]
119124
with httmock.HTTMock(cl_good_mock):
120125
bt.write_links(
121-
"10.3.3000",
122-
"10.1.1000",
123-
"10.2.2000",
126+
"10.3.3.3000",
127+
"10.1.1.1000",
128+
"10.2.2.2000",
124129
self.deb,
125130
self.cor,
126131
self.rad,
@@ -132,7 +137,7 @@ def test_write_links_withapps(self):
132137
with open("TEMPFILE.txt", 'rb') as file:
133138
data = file.read()
134139
data = data.replace(b"\r", b"")
135-
assert len(data) == 3060
140+
assert len(data) == 3100
136141

137142
def test_export_devloader(self):
138143
"""

0 commit comments

Comments
 (0)