Skip to content

confingy.cli.transpile#

transpile #

Transpile command for confingy CLI.

This module provides functionality to convert serialized confingy configurations back into Python code with proper type hints and imports.

transpile #

transpile(
    input_path: Annotated[str, Argument()],
    output: Annotated[
        Optional[str],
        Option(
            help="Output Python file path (default: stdout)"
        ),
    ] = None,
)

Transpile serialized confingy fingys to Python code.

Examples:

Transpile a JSON file to Python code

confingy transpile fingy.json

Write output to a file

confingy transpile fingy.json -o fingy.py

Transpile from stdin

echo '{"_confingy_class": "MyClass", ...}' | confingy transpile -

You can also pipe to ruff to format

confingy transpile fingy.json | ruff format -

Source code in src/confingy/cli/transpile.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def transpile(
    input_path: Annotated[str, typer.Argument()],
    output: Annotated[
        Optional[str], typer.Option(help="Output Python file path (default: stdout)")
    ] = None,
):
    """
    Transpile serialized confingy fingys to Python code.

    Examples:

      Transpile a JSON file to Python code
      ```
      confingy transpile fingy.json
      ```

      Write output to a file
      ```
      confingy transpile fingy.json -o fingy.py
      ```

      Transpile from stdin
      ```
      echo '{"_confingy_class": "MyClass", ...}' | confingy transpile -
      ```

      You can also pipe to ruff to format
      ```
      confingy transpile fingy.json | ruff format -
      ```
    """
    try:
        # Read input
        if input_path == "-":
            # Read from stdin
            input_data = sys.stdin.read()
            fingy_data = json.loads(input_data)
        else:
            # Read from file
            input_file = Path(input_path)
            if not input_file.exists():
                typer.echo(f"Error: Input file '{input_path}' does not exist", err=True)
                raise typer.Exit(1)

            with open(input_file) as f:
                fingy_data = json.load(f)

        # Transpile
        python_code = transpile_fingy(fingy_data)

        # Write output
        if output:
            output_path = Path(output)
            output_path.parent.mkdir(parents=True, exist_ok=True)
            with open(output_path, "w") as f:
                f.write(python_code)
            typer.echo(f"Transpiled fingy written to {output_path}", err=True)
        else:
            # Write to stdout
            typer.echo(python_code)
    except Exception as e:
        typer.echo(f"Error transpiling fingy: {e}", err=True)
        raise typer.Exit(1)