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

add 'noop()' and 'identity()' functions

This commit is contained in:
Mike Fährmann 2021-05-04 18:00:38 +02:00
parent 755164b36a
commit c5ca7905ce
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
4 changed files with 20 additions and 4 deletions

View File

@ -10,7 +10,7 @@
from .booru import BooruExtractor
from .common import Message
from .. import text, exception
from .. import text, util, exception
from ..cache import cache
import collections
@ -206,7 +206,7 @@ class SankakuAPI():
self.username, self.password = self.extractor._get_auth_info()
if not self.username:
self.authenticate = lambda: None
self.authenticate = util.noop
def pools(self, pool_id):
params = {"lang": "en"}

View File

@ -638,7 +638,7 @@ class DataJob(Job):
self.ascii = config.get(("output",), "ascii", ensure_ascii)
private = config.get(("output",), "private")
self.filter = (lambda x: x) if private else util.filter_dict
self.filter = util.identity if private else util.filter_dict
def run(self):
sleep = self.extractor.config("sleep-extractor")

View File

@ -81,6 +81,15 @@ def raises(cls):
return wrap
def identity(x):
"""Returns its argument"""
return x
def noop():
"""Does nothing"""
def generate_token(size=16):
"""Generate a random token with hexadecimal digits"""
data = random.getrandbits(size * 8).to_bytes(size, "big")
@ -804,7 +813,7 @@ class PathFormat():
@staticmethod
def _build_cleanfunc(chars, repl):
if not chars:
return lambda x: x
return identity
elif isinstance(chars, dict):
def func(x, table=str.maketrans(chars)):
return x.translate(table)

View File

@ -484,6 +484,13 @@ class TestOther(unittest.TestCase):
with self.assertRaises(ValueError):
func(3)
def test_identity(self):
for value in (123, "foo", [1, 2, 3], (1, 2, 3), {1: 2}, None):
self.assertIs(util.identity(value), value)
def test_noop(self):
self.assertEqual(util.noop(), None)
def test_generate_token(self):
tokens = set()
for _ in range(100):