Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically format generated Python code. #9

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ requires-python = ">=3.9"
authors = [{ name = "overcat" }]
license = { text = "Apache-2.0" }
keywords = ["stellar", "soroban"]
dependencies = ["click>=8.1.7", "jinja2>=3.1.4", "stellar-sdk>=12.0.0"]
dependencies = [
"black>=24.10.0",
"click>=8.1.7",
"jinja2>=3.1.4",
"stellar-sdk>=12.0.0",
]

[project.scripts]
stellar-contract-bindings = "stellar_contract_bindings.cli:cli"
Expand All @@ -20,10 +25,12 @@ CI = "https://github.com/lightsail-network/stellar-contract-bindings/actions"

[dependency-groups]
dev = [
"black>=24.10.0",
"mypy>=1.13.0",
"pytest-asyncio>=0.24.0",
"pytest>=8.3.3",
"aiohttp>=3.11.8",
"aiohttp-sse-client>=0.2.1",
]

[tool.uv]
package = true
19 changes: 11 additions & 8 deletions stellar_contract_bindings/python.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import keyword
import os
from typing import List
import black

import click
from jinja2 import Template
Expand Down Expand Up @@ -211,10 +212,7 @@ def from_scval(td: xdr.SCSpecTypeDef, name: str):

def render_info():
return (
f"# This file was generated by stellar_contract_bindings v{stellar_contract_bindings_version} and stellar_sdk v{stellar_sdk_version}.\n"
f"# Client is used to interact with the contract on the Stellar network synchronously,\n"
f"# while ClientAsync is used to interact with the contract on the Stellar network asynchronously.\n"
f"# If you don't need to interact with the contract asynchronously, you can remove the ClientAsync class and its methods."
f"# This file was generated by stellar_contract_bindings v{stellar_contract_bindings_version} and stellar_sdk v{stellar_sdk_version}."
)


Expand Down Expand Up @@ -661,15 +659,20 @@ def command(contract_id: str, rpc_url: str, output: str, client_type: str):

click.echo("Generating Python bindings")
generated = generate_binding(specs, client_type=client_type)
try:
generated = black.format_str(generated, mode=black.Mode())
except Exception as e:
click.echo(
f"formatting failed, there may be issues with the generated binding, please report to us: {e}",
err=True,
)

if not os.path.exists(output):
os.makedirs(output)
output_path = os.path.join(output, "bindings.py")
with open(output_path, "w") as f:
f.write(generated)
click.echo(f"Generated Python bindings to {output_path}")
click.echo(
f"We recommend running `black {output_path}` to format the generated code."
)


if __name__ == "__main__":
Expand All @@ -681,4 +684,4 @@ def command(contract_id: str, rpc_url: str, output: str, client_type: str):

specs = parse_contract_metadata(wasm).spec
generated = generate_binding(specs, client_type="both")
print(generated)
print(black.format_str(generated, mode=black.Mode()))
3 changes: 0 additions & 3 deletions tests/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# This file was generated by stellar_contract_bindings v0.2.0b0 and stellar_sdk v12.0.0.
# Client is used to interact with the contract on the Stellar network synchronously,
# while ClientAsync is used to interact with the contract on the Stellar network asynchronously.
# If you don't need to interact with the contract asynchronously, you can remove the ClientAsync class and its methods.

from enum import IntEnum, Enum
from typing import Dict, List, Tuple, Optional, Union
Expand Down
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.