Commit Graph

33 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 656ded9d6c chore: fix PT0{01,03,22,23} ruff rules 2023-02-15 17:29:22 -08:00
bastimeyer 4622c9728e chore: add "ISC" rules to ruff config 2023-02-09 11:48:40 -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 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 d09112ab1f plugin.api.validate: rewrite tests
Completely rewrite tests using pytest, with full coverage
2022-05-08 09:21:19 +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
bastimeyer 1008d56989 plugin.api.validate: fix xml_element
Add missing tail attribute and clone child nodes
2022-05-08 09:21:19 +02:00
bastimeyer f23870caeb plugin.api.validate: refactor get
Turn into validation schema with a singledispatch function.
This is required for the upcoming module split.
2022-05-08 09:21:19 +02:00
bastimeyer 40b604e69c plugin.api.validate: add parse_{json,html,xml,qsd}
- add parse_{json,html,xml,qsd} validate.transform wrapper methods
- refactor utils module and move parse* methods into utils.parse
- refactor parse* methods and use common parsing + exception handling
- reformat all-export list in utils
2021-08-31 19:17:59 -07:00
bastimeyer b1dc0c1d8f plugin.api.validate: add args+kwargs to transform 2021-08-31 19:17:59 -07:00
bastimeyer 6fea0ce4a2 plugin.api.validate: switch to lxml.etree
- replace xml.etree from standard library with lxml.etree in
  plugin.api.validate, utils.parse_xml, plugins and tests
- add validate.xml_xpath() and validate.xml_xpath_string() methods
- fix validate.get() and return attribute value for given attribute key
- refactor validate.xml_element() validation method
- fix and add tests
2021-08-31 11:54:22 +02:00
bastimeyer d235dfc1e9 plugin.api.validate: implement union_get() 2021-06-04 21:45:23 +02:00
bastimeyer 012d53da68 plugin.api.validate: add nested lookups to get() 2021-06-04 21:45:23 +02:00
bastimeyer 2ad48cb0c8 chore: set literals and dict comprehensions 2020-11-27 16:04:45 +01:00
bastimeyer fcda5b6814 chore: remove u-strings 2020-10-31 18:04:08 +01:00
beardypig a585e88297 chore: sort imports, fix a dependency cycle and use absolute imports
Co-authored-by: bastimeyer <mail@bastimeyer.de>
2020-10-27 17:17:49 +01:00
bastimeyer 18b026b3d8 chore: remove file encoding header comments 2020-10-19 17:48:57 +02:00
bastimeyer 69fafd3fb8 PEP263: use consistent utf-8 coding header comment 2020-02-25 05:35:16 +01:00
bastimeyer 2d7d15aa87 cleanup: remove unnecessary unittest.main() calls 2020-02-25 05:35:16 +01:00
back-to f7f13dc2b3 travis-ci: Fixed Python 3.8 error's
- logger: ValueError: Invalid format '[{name}][{levelname}] {message}' 
for '%' style
- api/utils: SyntaxWarning: invalid escape sequence
- hds: SyntaxWarning: invalid escape sequence and Flake8 E127
- test_api_validate: SyntaxWarning: invalid escape sequence
- test_plugins: ResourceWarning: unclosed file
2018-12-09 20:22:38 +01:00
beardypig c9fff48e9a Test coverage increase (#1646)
* tests: logger tests

* test: file stream

* test: missing sessions tests

* test: missing util tests

* tests: call can_handle_url for each plugin to ensure it won't error

* tests: speed up by 2 seconds, mocking sleep

* test for streamname lookups

* refactor test structure

* tests: rebase plugins tests

* tests: no need for Python 3.6 support

* tests: remove 2.6 from the travis build

* tests: rebase on master

* tests: rename plugin.api tests

* tests: add mock http resource

* move new plugin tests

* rebase master

* tests: coverage for ConsoleOutput
2018-06-29 11:31:37 -07:00