Code generator reference

The API behind the atp CLI. Reach for this when generation is a step in a build script rather than something you type, or when you want to know exactly what a flag does.

Everything is driven by one object. CodegenConfig is frozen and hashable, which is what lets the parsed lexicon databases be cached per run instead of globally, so two runs with different lexicons can happen in the same process.

from pathlib import Path

from atproto_codegen.config import CodegenConfig

config = CodegenConfig(
    emit_lexicon_dirs=(Path('./lexicons'),),
    output_dir=Path('./my_pkg'),
    package='my_pkg',
)

Configuration

class atproto_codegen.config.CodegenConfig(emit_lexicon_dirs: Tuple[Path, ...] = (PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/atp/checkouts/latest/lexicons'),), package: str = 'atproto_client', output_dir: Path = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/atp/checkouts/latest/packages/atproto_client'), base_package: str = 'atproto_client')

Bases: object

Inputs and outputs of a single codegen run.

Frozen and hashable so that parsed lexicon databases can be cached per run instead of globally, which is what allows two runs with different lexicons in one process.

emit_lexicon_dirs: Tuple[Path, ...] = (PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/atp/checkouts/latest/lexicons'),)

Lexicons to generate code for.

package: str = 'atproto_client'

Import name of the generated package.

output_dir: Path = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/atp/checkouts/latest/packages/atproto_client')

Filesystem root of the generated package.

base_package: str = 'atproto_client'

Package providing the hand-written base classes the generated code builds on.

References out of the emitted lexicons resolve into its models, so they must name something it defines.

property models_output_dir: Path
property namespaces_output_dir: Path
property models_package: str
property is_self_gen: bool

Whether the run targets the SDK’s own package.

with_overrides(**kwargs: Any) β†’ CodegenConfig
module_import_path(models_subdir: Path) β†’ str

Return the dotted import path of a directory inside the generated models package.

nsid_segments(models_subdir: Path) β†’ List[str]

Return the NSID segments a directory inside the generated models package stands for.

atproto_codegen.config.get_config() β†’ CodegenConfig

Return the config of the run in progress, or the SDK’s own defaults outside a run.

atproto_codegen.config.use_config(config: CodegenConfig) β†’ Generator[CodegenConfig, None, None]

Activate a config for the duration of a run.

Generators

Each of these takes a config and writes part of the package. atp gen custom calls all three in order.

atproto_codegen.models.generator.generate_models(config: CodegenConfig | None = None) β†’ None
atproto_codegen.namespaces.generator.generate_namespaces(config: CodegenConfig | None = None, output_dir: Path | None = None, async_filename: str | None = None, sync_filename: str | None = None, with_client: bool = True) β†’ None
atproto_codegen.subscriptions.generator.generate_subscriptions(config: CodegenConfig | None = None) β†’ None

Generate the message models and clients of every emitted subscription.

Formatting

Generated code is formatted by shelling out to Ruff with a config the generator owns, so the output does not inherit the style of whatever project the output directory sits in. Ruff is deliberately not a declared dependency of the SDK.

exception atproto_codegen.utils.RuffNotFoundError

Bases: FileNotFoundError

Ruff is needed to format generated code but is not installed.

atproto_codegen.utils.find_ruff() β†’ str

Return the path to the Ruff binary.

Looks in the running interpreter’s script directories before falling back to PATH, so a Ruff installed into the active virtual environment wins over an unrelated global one.

Raises:

RuffNotFoundError – Ruff is not installed.

atproto_codegen.utils.format_code(path: Path, quiet: bool = True, root: Path | None = None) β†’ None

Format generated code under the generator’s own Ruff settings.

Ruff resolves the per-file-ignores of RUFF_CONFIG_PATH against the working directory, so it runs from the generated package root rather than from wherever codegen was invoked.

Parameters:
  • path – File or directory to format.

  • quiet – Suppress Ruff’s own output.

  • root – Generated package root. Defaults to the directory being formatted.

Raises:

RuffNotFoundError – Ruff is not installed.

atproto_codegen.utils.append_code(filepath: Path, code: str) β†’ None
atproto_codegen.utils.write_code(filepath: Path, code: str) β†’ None
atproto_codegen.utils.get_file_path_parts(nsid: NSID) β†’ List[str]
atproto_codegen.utils.get_import_path_old(nsid: NSID) β†’ str
atproto_codegen.utils.get_import_path(nsid: NSID) β†’ str
atproto_codegen.utils.convert_camel_case_to_snake_case(string: str) β†’ str
atproto_codegen.utils.camel_case_split(string: str) β†’ List[str]
atproto_codegen.utils.gen_description_by_camel_case_name(name: str) β†’ str
atproto_codegen.utils.sort_dict_by_key(d: dict) β†’ dict
atproto_codegen.utils.get_code_intent(level: int) β†’ str
atproto_codegen.utils.join_code(lines: List[str]) β†’ str
atproto_codegen.utils.get_sync_async_keywords(*, sync: bool) β†’ Tuple[str, str]
atproto_codegen.utils.capitalize_first_symbol(string: str) β†’ str
atproto_codegen.utils.get_def_model_name(method_name: str) β†’ str
atproto_codegen.utils.get_record_model_name(_: str | None = None) β†’ str
atproto_codegen.utils.get_model_path(nsid: NSID, method_name: str) β†’ str