Package: parse-stages
Parse a mini-language for selecting objects by tag or name[Home|GitLab|PyPI|ReadTheDocs]This library is mostly useful for command-line parsing by other tools liketox-stagesandnox-stages. It may be used to parse e.g. a command-line
specification like@check and not pylintor@tests or ruffand then
match it against a list of objects that have names and lists of tags.Parse stage specificationsTheparse_spec()function parses a string specification into
aBoolExprobject that may later be used to select matching objects
(e.g. test environments).The specification mini-language may roughly be described as:expr ::= and_expr ["or" and_expr...]
and_expr ::= not_expr ["and" not_expr...]
not_expr ::= ["not"] atom
atom ::= tag | keyword
tag ::= "@" characters
keyword ::= characters
characters ::= [A-Za-z0-9_-]+Thus, all of the following:@check@check and @quick@tests and not examplesnot @testspep8 or not @quick and @check...are valid expressions,
with the "not", "and", and "or" keywords having their usual precedence
(pep8 or not @quick and @checkis parsed aspep8 or ((@not quick) and @check)).Check whether an object matches a parsed specificationTheparse-stageslibrary provides two base dataclasses for objects that
may be matched against parsed expressions:TaggedFrozenandTagged.
Both classes have the same members:name: a stringtags: a list of stringsget_keyword_haystacks(): a method that returns a list of strings,[self.name]unless overriddenWhen aBoolExprobject'sevaluate()method is called for a specificTaggedFrozenorTaggedobject, it checks whether the specification
matches the tags and keywords defined for this object. Tags are matched
exactly, while a keyword is considered to match if it is contained in
the checked string; e.g.pepwould match bothpep8andexp_pep563,
while@blackwould not match ablack-reformattag.Theget_keyword_haystacks()method returns the strings to look in for
matching keywords. By default, it only returns thenamefield;
however, it may be extended, e.g. for Nox sessions it may also return
the name of the Python function that implements the session, for test
classes with methods it may return the class name and the method name, etc.ExamplesParse a list of stage specifications into expressions that may later be
matched against test environment definitions:e_check = parse_stages.parse_spec("@check")
e_check_quick = parse_stages.parse_spec("@check and @quick")
e_check_no_ruff = parse_stages.parse_spec("@check and not ruff")

specs = [(spec, parse_stages.parse_spec(spec)) for spec in args.stage_specs]Select the test environments that match the specification:# Obtain a list (okay, a dictionary) of test environments in some way
tox_envs = get_tox_environments()  # {"ruff": {"tags": ["check", "quick"]}, ...}

# Convert them to objects that the parsed expressions can match
all_envs = [
    parse_stages.TaggedFrozen(name, env["tags"])
    for name, env in tox_envs.items()
]

# Or define our own class that may hold additional information
@dataclasses.dataclass(frozen=True)
class TestEnv(parse_stages.TaggedFrozen):
    """A single test environment: name, tags, etc."""
    ...

all_envs = [TestEnv(name, env["tags"], ...) for name, env in tox_envs.items()]

# Select the ones that match the "@check" expression
matched = [env for env in all_envs if e_check.evaluate(env)]

# Or if we only care about the names...
quick_names = [env.name for env in all_envs if e_check_quick.evaluate(env)]ContactTheparse-stageslibrary was written byPeter Pentchev.
It is developed ina GitLab repository. This documentation is
hosted atRingletwith a copy atReadTheDocs.