Commit Graph

19 Commits

Author SHA1 Message Date
bastimeyer 45e515eb5a session/plugin: fix DeprecationWarning stacklevel 2023-03-24 09:41:23 -07:00
bastimeyer 5e6f03c3cd chore: add "B" rules to ruff config 2023-03-24 09:41:23 -07:00
bastimeyer 85ea235d13 chore: add "A" rules to ruff config 2023-02-20 14:10:06 -08:00
bastimeyer 57fa6f80e8 chore: add "COM" rules to ruff config 2023-02-09 10:26:50 -08:00
bastimeyer faab9200c7 chore: add "I" and "TID" rules to ruff config 2023-02-09 10:26:50 -08:00
bastimeyer fe9be00832 chore: ignore coverage in TYPE_CHECKING blocks 2023-01-14 07:47:08 +01:00
bastimeyer c211e5866e plugin.api.validate: deprecate text alias
- Deprecate text alias and raise StreamlinkDeprecationWarning on access
- Add deprecation to docs
- Replace validate.text with str in remaining plugins
- Update test
2023-01-13 11:27:11 -08:00
bastimeyer b7b0353b51 plugin.api.validate: refactor ValidationError
- Remove `ValidationError` from package's `__init__` module.
  It was never meant to be a public export. Update tests accordingly.
- Remove `context` argument from `ValidationError` and use the
  exception's `__cause__` property instead, and add the "raise from"
  statements to schema validations and validators where a context was
  set previously. Explicitly "raise from None" where a context should
  be suppressed.
- Fix unneeded type cast when printing `ValidationError` template string
- Fix length comparison in `length` validator
2022-08-27 13:56:29 +02:00
bastimeyer 1ea5663971 plugin.api.validate: rework XML validators
- Add supported `Element.xpath()` keywords to `validate.xml_xpath()`
  - `namespaces` for defining prefix-namespace mappings
  - `extensions` for defining custom XPath functions in Python
  - `smart_strings` for disabling element tree references on any string
     return values that will be kept in memory
  - `**variables` for being able to define custom XPath variables
    instead of having to embed strings in the XPath query itself
- Add `namespaces`, `extensions` and `**variables` keywords to
  `validate.xml_xpath_string()` and always disable `smart_strings`
- Add `namespaces` to `validate.xml_find()`, `validate.xml_findall()`
  and `validate.xml_findtext()`
- Handle XPath evaluation errors via `lxml.etree.XPathError`
  and ElementPath syntax errors via `SyntaxError`
- Update error messages of ElementPath-based validators
- Remove unneeded `iselement` validation at the end of `xml_find()`
- Fix docstrings of both XPath- and ElementPath-based validators
- Add tests for the newly added keywords and XPath evaluation errors
2022-08-13 12:50:55 -07:00
bastimeyer cd3645a033 plugin.api.validate: add RegexSchema 2022-08-05 12:27:46 -07:00
bastimeyer 49fa080aa5 plugin.api.validate: fix pattern input type 2022-08-05 12:25:56 -07:00
bastimeyer cb56e063c9 plugin.api.validate: add NoneOrAllSchema
This helps avoiding the validation pattern
```py
validate.any(None, validate.all(...))
```

and is useful for `re.Pattern` and `xml_xpath_string` validations,
where the validation result can be `None` and is usually used for
gracefully returning no validation results instead of raising an error.
2022-08-04 11:06:32 -07:00
bastimeyer b083eb3709 plugin.api.validate: add re.Pattern validation
Use the `search()` method and return `None` or a `re.Match` instance.

This avoids having to explicitly define the validation pattern
```py
validate.transform(re.compile(...).search)
```
2022-08-04 11:06:32 -07:00
bastimeyer 047bc8e0c9 plugin.api.validate: add ListSchema
In contrast to the `list` instance validation where sequence subsets
get validated, the `ListSchema` validates that the input list has the
same length and that all list items validate successfully according to
the defined schemas.
2022-08-04 11:06:32 -07:00
bastimeyer 0c49caa27d plugin.api.validate: more DRY schema definitions 2022-08-04 11:06:32 -07:00
bastimeyer 17697f0bc0 plugin.api: fix typing issues 2022-05-22 19:30:15 +02:00
bastimeyer 94ac8f63a3 plugin.api.validate: truncate error messages
- Change signature of `ValidationError` to be able to pass template
  strings and template variables which can get truncated individually.
- Make error messages consistent by using string representations for
  template variables where it makes sense
2022-05-08 09:21:19 +02:00
bastimeyer 3d44da082b plugin.api.validate: implement ValidationError
- 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
2022-05-08 09:21:19 +02:00
bastimeyer 120c103023 plugin.api.validate: turn module into package
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
2022-05-08 09:21:19 +02:00