Skip to main content

Python Parsing Hook

This package adds support for easily adding parsing hooks to Python, using a special comment at or near the top of a file.

Usage

After this package has been installed, it is activated by placing the following at the top of any module you wish to use an alternate parser for:

# -*- parsing: <parsing-module> -*-

Note that this will not work for code executed directly from the command line (e.g. python3 foo.py), but will work if you execute the code as a module (e.g. python3 -m foo).

Technical Information

Parsing Hooks

Operation

When a parsing hook module is found, that module is imported, and the parse_module() function in that module is invoked. The expected return of the parse_module() function is an AST tree.

The parse_module() function MUST accept at least a file object and filename string as arguments, and MUST also accept arbitrary keyword arguments after those two arguments.

Existing Parsing Hooks

Known existing parsing hooks include:

  • parsinghook.dumpast (from this package)
  • pep505

Modifying the Wheel / Installing from Source

Note that after using python3 -m build to build the wheel, it is necessary to manually add the parsinghook.pth file to the wheel, with (for example):

cd src
zip -g ../dist/parsinghook-*-py3-none-any.whl parsinghook.pth
cd ..

If installing from source, the src/parsinghook.pth file must be placed in your site-packages directory.

Future Development

Parser Options

In the future, the parsing comment might be updated to support optional arguments to parsers, e.g.:

# -*- parsing: parsinghook.dumpast colorize=True -*-

Mutators

In the future, additional magic comments might be supported such as:

# -*- mutator: <mutation-module> -*-

Which would allow other modules to modify the AST after the initial parsing.

Thanks and Credits

This package stands on the shoulders of giants.

The parsinghook.hook.activate() function is based on André Roberge's ideas package.

Metadata

Release files for parsinghook 0.9.1

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

Source distribution (sdist)

Source distribution for parsinghook 0.9.1
File Size Uploaded
parsinghook-0.9.1.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parsinghook 0.9.1
File Interpreter ABI Platform
parsinghook-0.9.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / parsinghook-0.9.1.tar.gz

Download URL parsinghook-0.9.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a017015e59caa1f7fcf3201c04788666e635df4c44c540d04b8ef834b0861aeb
BLAKE2b-256 checksum
How to use checksums
23aef06bfeb9d00af26c59035e0c63c47223261462bb56a128915cd1b6f17aa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.6.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0

Release files / parsinghook-0.9.1-py3-none-any.whl

Download URL parsinghook-0.9.1-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ba36c85d71f43e7bd28ac7ed6e2249e552edcf370921968844d449047166579
BLAKE2b-256 checksum
How to use checksums
a5c56a2bdb3bfa844aed84530baac58155791e396a3a98062dc71a7290cfe9d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.6.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0

Release history Release notifications | RSS feed

This release

0.9.1 This release

2 release files

0.9.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