Skip to main content

astyle_py — Python wrapper around astyle

PyPI - Version CI Python

Artistic Style (astyle) is a source code indenter, formatter, and beautifier for the C, C++, C++/CLI, Objective‑C, C# and Java programming languages.

This project wraps astyle in a Python package. The package can be used:

The main reason to use this Python wrapper, rather than native astyle binaries, is that makes it easy for developers working on a project to have exactly the same version of astyle, regardless of their operating system. This prevents formatting differences which sometimes occur between different versions of astyle.

Using with pre-commit

  1. Set up pre-commit for your project as described in https://pre-commit.com/#install.
  2. Add astyle_py to your .pre-commit-config.yaml file as follows. Note: avoid using main as the revision.
    repos:
    -   repo: https://github.com/igrr/astyle_py.git
        rev: v1.1.0
        hooks:
        -   id: astyle_py
            args: [--astyle-version=3.4.7 --style=linux]
    

Place the required astyle formatting options to the args array. See the next section for details.

Use --dry-run argument if you only want the pre-commit hook to report the formatting errors, and not fix them automatically.

If necessary, add verbose: true option to see the output.

Use files: option to configure the regex pattern used to match the files to be formatted. The default pattern is '^.*\.(c|cpp|cxx|h|hpp|inc)$'. You can exclude certain files via additional arguments, as described in the next section.

Using from the command line

Install the package from PyPI:

pip install astyle-py

Usage:

astyle_py [options] <files to format>
  • <files> — list of files to process. By default, astyle_py formats the files, modifying them in-place.
  • [options] — can be any of the formatting options, plus the following options are accepted:

Common options

  • --version — print the version and exit.
  • --astyle-version=<VER> — choose the version of Astyle to use.
  • --quiet — don't print diagnostic messages; by default, the list of files which are formatted is printed to stderr.
  • --dry-run — don't format the files, only check the formatting. Returns non-zero exit code if any file would change after formatting.

Specifying additional options and excluded files

  • --options=<file> — read more formatting options from the specified file. Empty lines and lines starting with # are ignored.
  • --exclude=<pattern> — skip files matching the given pattern. Note that patterns use the syntax of Gitlab CODEOWNERS files.
  • --exclude-list=<file> — skip files matching the list of patterns specified in a file. Empty lines and lines starting with # are ignored.

Specifying the rules file

  • --rules=<file> — read the formatting rules from the specified rules file. See Rules files section for details. This option is incompatible with --options, --exclude, --exclude-list.

Using as a library

This package can be used as a library to implement custom formatting tools. See sample.py for an example.

Formatting options

See http://astyle.sourceforge.net/astyle.html for the details on Astyle formatting options.

Note that this wrapper doesn't implement the options from "Other options" and "Command Line Only" categories, except for those listed above.

Rules files

Option --rules=<file> allows loading the formatting options from a rules file in YAML format. The rules file can specify different formatting rules for different parts of the project. This can be useful for monorepos which contain libraries written with different formatting conventions.

The rules file consists of sections (rules). For each section the following keywords may be specified:

  • version: Version of Astyle to use
  • include: List of files name patterns to include in this rule. Pattern syntax of Gitlab CODEOWNERS files is used. Required.
  • check: If set to false, the files covered by this rule will be ignored and not checked/formatted. Optional, default is true.
  • options: A string specifying the formatting options for files covered by this rule.

If the file path matches multiple rules, the latest rule is applied. If the file path doesn't match any rule, the options from the special DEFAULT rule are used.

Here is an example of a rules file:

DEFAULT:
    # These formatting options will be used by default
    options: "--style=otbs --indent=spaces=4 --convert-tabs"

thirdparty_lib_1:   # The section name is arbitrary
    # Override formatting rules for the files in a certain directory
    options: "--style=linux"
    include:
        - "/thirdparty/lib1/"

code_to_ignore_for_now:
    # Ignore files in some other directories
    check: false
    include:
        - "/src/component1/"
        - "/src/component2/"
        - "tests/"     # matches a subdirectory 'tests' anywhere in the source tree

Supported Astyle versions

This python wrapper bundles multiple copies of Astyle, you can choose which one to use:

  • In the CLI: via --astyle-version=VERSION argument
  • When using astyle_py as a library: by passing the version to Astyle() constructor

The following versions are supported:

  • 3.1 — used by default, unless a different version is specified
  • 3.4.7

Implementation notes

To simplify distribution of astyle, it is compiled to WebAssembly (astyle_py/libastyle.wasm) and executed using wasmtime runtime via its Python bindings. This package should work on all operating systems supported by wasmtime — at the time of writing these are:

  • x86_64 (amd64) Windows, Linux, macOS
  • aarch64 (arm64) Linux and macOS

Other project which wraps astyle into a Python package include:

Contributing

Please see CONTRIBUTING.md for details.

Thanks to André Simon for maintaining Astyle project!

Metadata

Release files for astyle-py 1.1.0

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

Source distribution (sdist)

Source distribution for astyle-py 1.1.0
File Size Uploaded Provenance Actions
astyle_py-1.1.0.tar.gz 274.1 kB Without provenance Details for astyle_py-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for astyle-py 1.1.0
File Interpreter ABI Platform Actions
astyle_py-1.1.0-py3-none-any.whl Python 3 none any Details for astyle_py-1.1.0-py3-none-any.whl

Total release size: 537.2 kB

Release files / astyle_py-1.1.0.tar.gz

Download URL astyle_py-1.1.0.tar.gz
Size 274.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5390779e1654a9112ad1791b29033d1991867eeb7b542707ca9ee394c2cb3fc6
BLAKE2b-256 checksum
How to use checksums
88a4b49eb5611be24d6245f69429f52e0d42c032efb1433c60e657b09daa5f1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / astyle_py-1.1.0-py3-none-any.whl

Download URL astyle_py-1.1.0-py3-none-any.whl
Size 263.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d3cdc80cfa30291ef0e235e23d63b7577048a0b018d1dd45102aba658a39165d
BLAKE2b-256 checksum
How to use checksums
c33485ed33263f0cc54f72c746d7479e2e74da4719e17b6e84278cea4e8d7d3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for astyle-py 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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