Beispiel #1
0
def validator_parse_qsd(*args, **kwargs):
    # type: () -> TransformSchema
    """
    Parse a query string via the :func:`streamlink.utils.parse.parse_qsd` utility function.
    """

    return TransformSchema(_parse_qsd, exception=ValidationError, schema=None, *args, **kwargs)
Beispiel #2
0
def validator_parse_xml(*args, **kwargs):
    # type: () -> TransformSchema
    """
    Parse XML data via the :func:`streamlink.utils.parse.parse_xml` utility function.
    """

    return TransformSchema(_parse_xml, exception=ValidationError, schema=None, *args, **kwargs)
Beispiel #3
0
def validator_xml_xpath(xpath) -> TransformSchema:
    """
    Query XML elements via xpath (:meth:`Element.xpath`) and return None if the result is falsy.
    """

    def transform_xpath(value):
        validate(iselement, value)
        return value.xpath(xpath) or None

    return TransformSchema(transform_xpath)
Beispiel #4
0
def validator_xml_findall(xpath) -> TransformSchema:
    """
    Find a list of XML elements via xpath.
    """

    def xpath_findall(value):
        validate(iselement, value)
        return value.findall(xpath)

    return TransformSchema(xpath_findall)
Beispiel #5
0
def validator_getattr(attr: Any, default: Any = None) -> TransformSchema:
    """
    Get a named attribute from the input object.

    If a default is set, it is returned when the attribute doesn't exist.
    """

    def getter(value):
        return getattr(value, attr, default)

    return TransformSchema(getter)
Beispiel #6
0
def validator_xml_find(xpath: str) -> TransformSchema:
    """
    Find an XML element via xpath (:meth:`Element.find`).
    """

    def xpath_find(value):
        validate(iselement, value)
        value = value.find(xpath)
        if value is None:
            raise ValidationError(
                "XPath {xpath} did not return an element",
                xpath=repr(xpath),
                schema="xml_find",
            )

        return validate(iselement, value)

    return TransformSchema(xpath_find)
Beispiel #7
0
def validator_map(func: Callable[..., Any]) -> TransformSchema:
    """
    Transform items from the input using the specified function.

    Supports both dicts and sequences. key/value pairs are expanded when applied to a dict.
    """

    def expand_kv(kv):
        return func(*kv)

    def map_values(value):
        cls = type(value)
        if isinstance(value, dict):
            return cls(map(expand_kv, value.items()))
        else:
            return cls(map(func, value))

    return TransformSchema(map_values)
Beispiel #8
0
def validator_filter(func):
    # type: (Callable[[Any], bool]) -> TransformSchema
    """
    Filter out unwanted items from the input using the specified function.

    Supports both dicts and sequences. key/value pairs are expanded when applied to a dict.
    """

    def expand_kv(kv):
        return func(*kv)

    def filter_values(value):
        cls = type(value)
        if isinstance(value, dict):
            return cls(filter(expand_kv, value.items()))
        else:
            return cls(filter(func, value))

    return TransformSchema(filter_values)
Beispiel #9
0
def validator_parse_html(*args, **kwargs) -> TransformSchema:
    """
    Parse HTML data via the :func:`streamlink.utils.parse.parse_html` utility function.
    """

    return TransformSchema(_parse_html, *args, **kwargs, exception=ValidationError, schema=None)
Beispiel #10
0
def _validate_transformschema(schema: TransformSchema, value):
    validate(abc.Callable, schema.func)
    return schema.func(value, *schema.args, **schema.kwargs)