Skip to main content

Python parsing hook

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parsinghook-0.9.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

parsinghook-0.9.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file parsinghook-0.9.1.tar.gz.

File metadata

  • Download URL: parsinghook-0.9.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for parsinghook-0.9.1.tar.gz
Algorithm Hash digest
SHA256 a017015e59caa1f7fcf3201c04788666e635df4c44c540d04b8ef834b0861aeb
MD5 68e4ab8d5ca68433d8377200a8a1893f
BLAKE2b-256 23aef06bfeb9d00af26c59035e0c63c47223261462bb56a128915cd1b6f17aa7

See more details on using hashes here.

File details

Details for the file parsinghook-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: parsinghook-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for parsinghook-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ba36c85d71f43e7bd28ac7ed6e2249e552edcf370921968844d449047166579
MD5 8059f03061685ee01df4ff1f66254ff1
BLAKE2b-256 a5c56a2bdb3bfa844aed84530baac58155791e396a3a98062dc71a7290cfe9d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page