Skip to content

Commit 55811fc

Browse files
committed
[PATCH] plugin.api.validate: turn module into package (streamlink#4514)
Turn module into package with multiple logical sub-modules: - Define a public interface in the package's `__init__` module - Split validation schemas, validators and validate logic - schemas: classes which register attributes used by their respective `validate` implementations - validators: functions which can internally call `validate` and which return something that can be validated - validate: singledispatch functions which implement the validation logic for schemas and various other types - Rename validation schemas for better internal references - Rename singledispatch methods Other clean-up work: - Update comments and fix grammar - Add type annotations - Use f-strings - Use `str` instead of the `text` alias - Simplify some code blocks - Rearrange classes and functions - Rephrase certain error messages - Add a few more tests for better code coverage - Implement `ValidationError` - Inherit from `ValueError` to preserve backwards compatiblity - Allow collecting multiple errors (AnySchema) - Keep an error stack of parent `ValidationError`s or other exceptions - Format error stack when converting error to string - Raise `ValidationError` instead of `ValueError` - Add error contexts where it makes sense - Add schema names to error instances - Add and update tests
1 parent 6d5ad75 commit 55811fc

File tree

8 files changed

+1192
-619
lines changed

8 files changed

+1192
-619
lines changed

src/streamlink/compat.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,31 @@ def devnull():
2323
range = xrange
2424
from collections import Mapping
2525
from itertools import izip
26+
from singledispatch import singledispatch
27+
from typing import Callable, Match
2628

2729
def bytes(b, enc="ascii"):
2830
return _str(b)
2931

32+
def indent(text, prefix, predicate=None):
33+
if predicate is None:
34+
def predicate(line):
35+
return line.strip()
36+
37+
def prefixed_lines():
38+
for line in text.splitlines(True):
39+
yield (prefix + line if predicate(line) else line)
40+
return ''.join(prefixed_lines())
41+
3042
elif is_py3:
3143
bytes = bytes
3244
str = str
3345
range = range
3446
izip = zip
35-
from collections.abc import Mapping
47+
from collections.abc import Callable, Mapping
48+
from functools import singledispatch
49+
from re import Match
50+
from textwrap import indent
3651

3752
try:
3853
from urllib.parse import (
@@ -64,7 +79,7 @@ def bytes(b, enc="ascii"):
6479
getargspec = getattr(inspect, "getfullargspec", inspect.getargspec)
6580

6681

67-
__all__ = ["Mapping", "is_py2", "is_py3", "is_py33", "is_win32", "str", "bytes",
82+
__all__ = ["Callable", "Mapping", "Match", "indent", "is_py2", "is_py3", "is_py33", "is_win32", "str", "bytes",
6883
"urlparse", "urlunparse", "urljoin", "parse_qs", "parse_qsl", "quote", "quote_plus",
69-
"unquote", "unquote_plus", "queue", "range", "urlencode", "devnull", "which",
84+
"unquote", "unquote_plus", "queue", "range", "singledispatch", "urlencode", "devnull", "which",
7085
"izip", "urlsplit", "urlunsplit", "getargspec", "html_unescape", "lru_cache"]

0 commit comments

Comments
 (0)