| 1 |
Metadata-Version: 2.4 |
| 2 |
Name: parse_stages |
| 3 |
Version: 0.1.12 |
| 4 |
Summary: Parse an expression for selecting stages and tags |
| 5 |
Project-URL: Homepage, https://devel.ringlet.net/devel/parse-stages/ |
| 6 |
Project-URL: Changes, https://devel.ringlet.net/devel/parse-stages/changes/ |
| 7 |
Project-URL: Issue Tracker, https://gitlab.com/ppentchev/parse-stages/-/issues |
| 8 |
Project-URL: Source Code, https://gitlab.com/ppentchev/parse-stages |
| 9 |
Author-email: Peter Pentchev <[email protected]> |
| 10 |
License-Expression: BSD-2-Clause |
| 11 |
License-File: LICENSES/BSD-2-Clause.txt |
| 12 |
Classifier: Development Status :: 5 - Production/Stable |
| 13 |
Classifier: Intended Audience :: Developers |
| 14 |
Classifier: Operating System :: OS Independent |
| 15 |
Classifier: Programming Language :: Python |
| 16 |
Classifier: Programming Language :: Python :: 3 |
| 17 |
Classifier: Programming Language :: Python :: 3 :: Only |
| 18 |
Classifier: Programming Language :: Python :: 3.10 |
| 19 |
Classifier: Programming Language :: Python :: 3.11 |
| 20 |
Classifier: Programming Language :: Python :: 3.12 |
| 21 |
Classifier: Programming Language :: Python :: 3.13 |
| 22 |
Classifier: Programming Language :: Python :: 3.14 |
| 23 |
Classifier: Programming Language :: Python :: 3.15 |
| 24 |
Classifier: Topic :: Software Development :: Libraries |
| 25 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |
| 26 |
Classifier: Topic :: Software Development :: Testing |
| 27 |
Classifier: Topic :: Software Development :: Testing :: Unit |
| 28 |
Classifier: Typing :: Typed |
| 29 |
Requires-Python: >=3.10 |
| 30 |
Requires-Dist: pyparsing<4,>=3.1 |
| 31 |
Description-Content-Type: text/markdown |
| 32 |
|
| 33 |
<!-- |
| 34 |
SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 35 |
SPDX-License-Identifier: BSD-2-Clause |
| 36 |
--> |
| 37 |
|
| 38 |
# Parse a mini-language for selecting objects by tag or name |
| 39 |
|
| 40 |
\[[Home][ringlet-parse-stages] | [GitLab][gitlab] | [PyPI][pypi] | [ReadTheDocs][readthedocs]\] |
| 41 |
|
| 42 |
This library is mostly useful for command-line parsing by other tools like |
| 43 |
`tox-stages` and `nox-stages`. It may be used to parse e.g. a command-line |
| 44 |
specification like `@check and not pylint` or `@tests or ruff` and then |
| 45 |
match it against a list of objects that have names and lists of tags. |
| 46 |
|
| 47 |
## Parse stage specifications |
| 48 |
|
| 49 |
The `parse_spec()` function parses a string specification into |
| 50 |
a `BoolExpr` object that may later be used to select matching objects |
| 51 |
(e.g. test environments). |
| 52 |
|
| 53 |
The specification mini-language may roughly be described as: |
| 54 |
|
| 55 |
expr ::= and_expr ["or" and_expr...] |
| 56 |
and_expr ::= not_expr ["and" not_expr...] |
| 57 |
not_expr ::= ["not"] atom |
| 58 |
atom ::= tag | keyword |
| 59 |
tag ::= "@" characters |
| 60 |
keyword ::= characters |
| 61 |
characters ::= [A-Za-z0-9_-]+ |
| 62 |
|
| 63 |
Thus, all of the following: |
| 64 |
|
| 65 |
- `@check` |
| 66 |
- `@check and @quick` |
| 67 |
- `@tests and not examples` |
| 68 |
- `not @tests` |
| 69 |
- `pep8 or not @quick and @check` |
| 70 |
|
| 71 |
...are valid expressions, |
| 72 |
with the "not", "and", and "or" keywords having their usual precedence |
| 73 |
(`pep8 or not @quick and @check` is parsed as |
| 74 |
`pep8 or ((@not quick) and @check)`). |
| 75 |
|
| 76 |
## Check whether an object matches a parsed specification |
| 77 |
|
| 78 |
The `parse-stages` library provides two base dataclasses for objects that |
| 79 |
may be matched against parsed expressions: `TaggedFrozen` and `Tagged`. |
| 80 |
Both classes have the same members: |
| 81 |
|
| 82 |
- `name`: a string |
| 83 |
- `tags`: a list of strings |
| 84 |
- `get_keyword_haystacks()`: a method that returns a list of strings, |
| 85 |
`[self.name]` unless overridden |
| 86 |
|
| 87 |
When a `BoolExpr` object's `evaluate()` method is called for a specific |
| 88 |
`TaggedFrozen` or `Tagged` object, it checks whether the specification |
| 89 |
matches the tags and keywords defined for this object. Tags are matched |
| 90 |
exactly, while a keyword is considered to match if it is contained in |
| 91 |
the checked string; e.g. `pep` would match both `pep8` and `exp_pep563`, |
| 92 |
while `@black` would not match a `black-reformat` tag. |
| 93 |
|
| 94 |
The `get_keyword_haystacks()` method returns the strings to look in for |
| 95 |
matching keywords. By default, it only returns the `name` field; |
| 96 |
however, it may be extended, e.g. for Nox sessions it may also return |
| 97 |
the name of the Python function that implements the session, for test |
| 98 |
classes with methods it may return the class name and the method name, etc. |
| 99 |
|
| 100 |
## Examples |
| 101 |
|
| 102 |
Parse a list of stage specifications into expressions that may later be |
| 103 |
matched against test environment definitions: |
| 104 |
|
| 105 |
e_check = parse_stages.parse_spec("@check") |
| 106 |
e_check_quick = parse_stages.parse_spec("@check and @quick") |
| 107 |
e_check_no_ruff = parse_stages.parse_spec("@check and not ruff") |
| 108 |
|
| 109 |
specs = [(spec, parse_stages.parse_spec(spec)) for spec in args.stage_specs] |
| 110 |
|
| 111 |
Select the test environments that match the specification: |
| 112 |
|
| 113 |
# Obtain a list (okay, a dictionary) of test environments in some way |
| 114 |
tox_envs = get_tox_environments() # {"ruff": {"tags": ["check", "quick"]}, ...} |
| 115 |
|
| 116 |
# Convert them to objects that the parsed expressions can match |
| 117 |
all_envs = [ |
| 118 |
parse_stages.TaggedFrozen(name, env["tags"]) |
| 119 |
for name, env in tox_envs.items() |
| 120 |
] |
| 121 |
|
| 122 |
# Or define our own class that may hold additional information |
| 123 |
@dataclasses.dataclass(frozen=True) |
| 124 |
class TestEnv(parse_stages.TaggedFrozen): |
| 125 |
"""A single test environment: name, tags, etc.""" |
| 126 |
... |
| 127 |
|
| 128 |
all_envs = [TestEnv(name, env["tags"], ...) for name, env in tox_envs.items()] |
| 129 |
|
| 130 |
# Select the ones that match the "@check" expression |
| 131 |
matched = [env for env in all_envs if e_check.evaluate(env)] |
| 132 |
|
| 133 |
# Or if we only care about the names... |
| 134 |
quick_names = [env.name for env in all_envs if e_check_quick.evaluate(env)] |
| 135 |
|
| 136 |
|
| 137 |
## Contact |
| 138 |
|
| 139 |
The `parse-stages` library was written by [Peter Pentchev][roam]. |
| 140 |
It is developed in [a GitLab repository][gitlab]. This documentation is |
| 141 |
hosted at [Ringlet][ringlet-parse-stages] with a copy at [ReadTheDocs][readthedocs]. |
| 142 |
|
| 143 |
[roam]: mailto:[email protected] "Peter Pentchev" |
| 144 |
[gitlab]: https://gitlab.com/ppentchev/parse-stages "The parse-stages GitLab repository" |
| 145 |
[pypi]: https://pypi.org/project/parse-stages/ "The parse-stages Python Package Index page" |
| 146 |
[readthedocs]: https://parse-stages.readthedocs.io/ "The parse-stages ReadTheDocs page" |
| 147 |
[ringlet-parse-stages]: https://devel.ringlet.net/devel/parse-stages/ "The Ringlet parse-stages homepage" |