1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

allow specifying sleep-* options as string

either as single value or as range: "3.5", "2.1 - 5.0"
This commit is contained in:
Mike Fährmann 2021-12-18 23:04:33 +01:00
parent 0d02a7861e
commit 64cf26eaf4
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
3 changed files with 43 additions and 6 deletions

View File

@ -3365,9 +3365,11 @@ Duration
Type
* ``float``
* ``list`` with 2 ``floats``
* ``string``
Example
* ``2.85``
* ``[1.5, 3.0]``
* ``"2.85"``, ``"1.5-3.0"``
Description
A |Duration|_ represents a span of time in seconds.
@ -3375,6 +3377,8 @@ Description
* If given as a ``list`` with 2 floating-point numbers ``a`` & ``b`` ,
it will be randomly chosen with uniform distribution such that ``a <= N <=b``.
(see `random.uniform() <https://docs.python.org/3/library/random.html#random.uniform>`_)
* If given as a ``string``, it can either represent a single ``float``
value (``"2.85"``) or a range (``"1.5-3.0"``).
Path

View File

@ -428,18 +428,26 @@ def build_duration_func(duration, min=0.0):
if not duration:
return None
try:
lower, upper = duration
except TypeError:
pass
if isinstance(duration, str):
lower, _, upper = duration.partition("-")
lower = float(lower)
else:
try:
lower, upper = duration
except TypeError:
lower, upper = duration, None
if upper:
upper = float(upper)
return functools.partial(
random.uniform,
lower if lower > min else min,
upper if upper > min else min,
)
return functools.partial(identity, duration if duration > min else min)
else:
if lower < min:
lower = min
return lambda: lower
def build_extractor_filter(categories, negate=True, special=None):

View File

@ -357,6 +357,31 @@ class TestOther(unittest.TestCase):
with self.assertRaises(exception.StopExtraction):
expr()
def test_build_duration_func(self, f=util.build_duration_func):
for v in (0, 0.0, "", None, (), []):
self.assertIsNone(f(v))
def test_single(df, v):
for _ in range(10):
self.assertEqual(df(), v)
def test_range(df, lower, upper):
for __ in range(10):
v = df()
self.assertGreaterEqual(v, lower)
self.assertLessEqual(v, upper)
test_single(f(3), 3)
test_single(f(3.0), 3.0)
test_single(f("3"), 3)
test_single(f("3.0-"), 3)
test_single(f(" 3 -"), 3)
test_range(f((2, 4)), 2, 4)
test_range(f([2, 4]), 2, 4)
test_range(f("2-4"), 2, 4)
test_range(f(" 2.0 - 4 "), 2, 4)
def test_extractor_filter(self):
# empty
func = util.build_extractor_filter("")