Skip to content

API usage

The Gerber Formatter can be used as a library. The main entry point is the pygerber.gerber.formatter module.

API

The basic usage of the Gerber Formatter is to utilize format and formats functions available in pygerber.gerber.formatter module. Please have look at the following example:

format_example.py
from pathlib import Path
from pygerber.gerber.parser import parse
from pygerber.gerber.formatter import format
from pygerber.examples import ExamplesEnum, load_example

gerber_source_code = load_example(ExamplesEnum.UCAMCO_2_11_2)

ast = parse(gerber_source_code)

with Path("output.formatted.gbr").open("w") as output:
    format(ast, output)

Please note that to format the code, we first had to parse it, and only then could the abstract syntax tree be passed to the formatter. format function writes formatted code directly to file-like object, while formats function returns formatted code as a string.

Configuration

The formatter can be configured using Options object. The object can be passed to format and formats functions as a optional options argument. You can find full option reference here.

options_example.py
from pathlib import Path
from pygerber.gerber.parser import parse
from pygerber.gerber.formatter import format, Options, MacroSplitMode
from pygerber.examples import ExamplesEnum, load_example

gerber_source_code = load_example(ExamplesEnum.UCAMCO_2_11_2)

ast = parse(gerber_source_code)

with Path("output.formatted.gbr").open("w") as output:
    format(
        ast,
        output,
        options=Options(
            indent_character="\t",
            macro_split_mode=MacroSplitMode.NoSplit,
        ),
    )