Source code for jitxlib.gerber.format.compat

"""Opt-in output adjustments for readers that deviate from the specification.

Every adjustment is off by default, and the default output is the one the
specification describes. Adjustments are named for what they change in the
output, never for a fabricator: which house needs which is that house's
library's business.
"""

from __future__ import annotations

from collections.abc import Iterable
from dataclasses import dataclass, fields, replace


[docs] @dataclass(frozen=True) class Compat: """The set of adjustments in effect for one export. Args: bake_macro_rotation: write rotated pads as macros with the rotation applied to the primitive coordinates and no rotation parameter, for readers that mishandle the macro rotation argument. Each distinct angle of a shape becomes its own macro. """ bake_macro_rotation: bool = False
[docs] @classmethod def from_names(cls, names: Iterable[str]) -> Compat: """The adjustments named, as on the command line (`bake-macro-rotation`). Raises: ValueError: a name is not a known adjustment, listing the known ones. """ known = {_name(field.name): field.name for field in fields(cls)} compat = cls() for name in names: if name not in known: listed = ", ".join(sorted(known)) raise ValueError( f"Unknown compatibility adjustment {name!r}; known: {listed}" ) compat = replace(compat, **{known[name]: True}) return compat
@property def names(self) -> tuple[str, ...]: """The command-line names of the adjustments in effect, sorted.""" return tuple( sorted( _name(field.name) for field in fields(self) if getattr(self, field.name) ) )
DEFAULT = Compat() """No adjustments: the output the specification describes.""" def _name(field: str) -> str: return field.replace("_", "-")