Skip to main content

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 like tox-stages and nox-stages. It may be used to parse e.g. a command-line specification like @check and not pylint or @tests or ruff and then match it against a list of objects that have names and lists of tags.

Parse stage specifications

The parse_spec() function parses a string specification into a BoolExpr object 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 examples
  • not @tests
  • pep8 or not @quick and @check

...are valid expressions, with the "not", "and", and "or" keywords having their usual precedence (pep8 or not @quick and @check is parsed as pep8 or ((@not quick) and @check)).

Check whether an object matches a parsed specification

The parse-stages library provides two base dataclasses for objects that may be matched against parsed expressions: TaggedFrozen and Tagged. Both classes have the same members:

  • name: a string
  • tags: a list of strings
  • get_keyword_haystacks(): a method that returns a list of strings, [self.name] unless overridden

When a BoolExpr object's evaluate() method is called for a specific TaggedFrozen or Tagged object, 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. pep would match both pep8 and exp_pep563, while @black would not match a black-reformat tag.

The get_keyword_haystacks() method returns the strings to look in for matching keywords. By default, it only returns the name field; 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.

Examples

Parse 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)]

Contact

The parse-stages library was written by Peter Pentchev. It is developed in a GitLab repository. This documentation is hosted at Ringlet with a copy at ReadTheDocs.

Metadata

Release files for parse-stages 0.1.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parse-stages 0.1.12
File Size Uploaded
parse_stages-0.1.12.tar.gz 86.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parse-stages 0.1.12
File Interpreter ABI Platform
parse_stages-0.1.12-py3-none-any.whl Python 3 none any Details

Total release size: 96.2 kB

Release files / parse_stages-0.1.12.tar.gz

Download URL parse_stages-0.1.12.tar.gz
Size 86.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0584afd35c84b12bd0f262a2f40a7362b6818382a02fd278ac2a46c491356172
BLAKE2b-256 checksum
How to use checksums
d284a4c7cbd96c4a4017b41a395a59a40beff17f2190dccc63e7c347a10fe6e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / parse_stages-0.1.12-py3-none-any.whl

Download URL parse_stages-0.1.12-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99761a0d1e5a7bdf8f5efa05d4825d9cb683a6f91b4ea71c2fc4fb5615e8b6ff
BLAKE2b-256 checksum
How to use checksums
d5d352d1b8a0e5d1e7010f78446e3134d59fa1e0e129fed89e9d1a6110bfa75a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

0.1.12 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page