-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(BACK-7982): refactor json methods to make it reusable * feat(BACK-7982): add `list` command/package, make lint use it * feat(BACK-7982): add `format` command * feat(BACK-7982): add command to main * feat(BACK-7982): add tests * feat(BACK-7982): add doc
- Loading branch information
1 parent
ed93331
commit 046d172
Showing
11 changed files
with
274 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Package implementing formatting commands to normalize descriptor files.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import os | ||
from concurrent.futures.thread import ThreadPoolExecutor | ||
from pathlib import Path | ||
|
||
from rich import print | ||
|
||
from erc7730.common.json import dict_from_json_file, dict_to_json_file | ||
from erc7730.common.output import ( | ||
AddFileOutputAdder, | ||
BufferAdder, | ||
ConsoleOutputAdder, | ||
DropFileOutputAdder, | ||
ExceptionsToOutput, | ||
OutputAdder, | ||
) | ||
from erc7730.list.list import get_erc7730_files | ||
|
||
|
||
def format_all_and_print_errors(paths: list[Path]) -> bool: | ||
""" | ||
Format all ERC-7730 descriptor files at given paths and print errors. | ||
:param paths: paths to apply formatter on | ||
:return: true if not errors occurred | ||
""" | ||
out = DropFileOutputAdder(delegate=ConsoleOutputAdder()) | ||
|
||
count = format_all(paths, out) | ||
|
||
if out.has_errors: | ||
print(f"[bold][red]formatted {count} descriptor files, some errors occurred ❌[/red][/bold]") | ||
return False | ||
|
||
if out.has_warnings: | ||
print(f"[bold][yellow]formatted {count} descriptor files, some warnings occurred ⚠️[/yellow][/bold]") | ||
return True | ||
|
||
print(f"[bold][green]formatted {count} descriptor files, no errors occurred ✅[/green][/bold]") | ||
return True | ||
|
||
|
||
def format_all(paths: list[Path], out: OutputAdder) -> int: | ||
""" | ||
Format all ERC-7730 descriptor files at given paths. | ||
Paths can be files or directories, in which case all descriptor files in the directory are recursively formatted. | ||
:param paths: paths to apply formatter on | ||
:param out: output adder | ||
:return: number of files formatted | ||
""" | ||
files = list(get_erc7730_files(*paths, out=out)) | ||
|
||
if len(files) <= 1 or not (root_path := os.path.commonpath(files)): | ||
root_path = None | ||
|
||
def label(f: Path) -> Path | None: | ||
return f.relative_to(root_path) if root_path is not None else None | ||
|
||
if len(files) > 1: | ||
print(f"📝 formatting {len(files)} descriptor files…\n") | ||
|
||
with ThreadPoolExecutor() as executor: | ||
for future in (executor.submit(format_file, file, out, label(file)) for file in files): | ||
future.result() | ||
|
||
return len(files) | ||
|
||
|
||
def format_file(path: Path, out: OutputAdder, show_as: Path | None = None) -> None: | ||
""" | ||
Format a single ERC-7730 descriptor file. | ||
:param path: ERC-7730 descriptor file path | ||
:param show_as: if provided, print this label instead of the file path | ||
:param out: error handler | ||
""" | ||
|
||
label = path if show_as is None else show_as | ||
file_out = AddFileOutputAdder(delegate=out, file=path) | ||
|
||
with BufferAdder(file_out, prolog=f"➡️ formatting [bold]{label}[/bold]…", epilog="") as out, ExceptionsToOutput(out): | ||
dict_to_json_file(path, dict_from_json_file(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Package implementing listing commands to easily find descriptor files.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from collections.abc import Generator | ||
from pathlib import Path | ||
|
||
from rich import print | ||
|
||
from erc7730 import ERC_7730_REGISTRY_CALLDATA_PREFIX, ERC_7730_REGISTRY_EIP712_PREFIX | ||
from erc7730.common.output import ( | ||
ConsoleOutputAdder, | ||
OutputAdder, | ||
) | ||
|
||
|
||
def list_all(paths: list[Path]) -> bool: | ||
""" | ||
List all ERC-7730 descriptor files at given paths. | ||
Paths can be files or directories, in which case all descriptor files in the directory are recursively listed. | ||
:param paths: paths to search for descriptor files | ||
:return: true if no error occurred | ||
""" | ||
out = ConsoleOutputAdder() | ||
|
||
for file in get_erc7730_files(*paths, out=out): | ||
print(file) | ||
|
||
return not out.has_errors | ||
|
||
|
||
def get_erc7730_files(*paths: Path, out: OutputAdder) -> Generator[Path, None, None]: | ||
""" | ||
List all ERC-7730 descriptor files at given paths. | ||
Paths can be files or directories, in which case all descriptor files in the directory are recursively listed. | ||
:param paths: paths to search for descriptor files | ||
:param out: error handler | ||
""" | ||
for path in paths: | ||
if path.is_file(): | ||
if is_erc7730_file(path): | ||
yield path | ||
else: | ||
out.error(title="Invalid path", message=f"{path} is not an ERC-7730 descriptor file") | ||
elif path.is_dir(): | ||
for file in path.rglob("*.json"): | ||
if is_erc7730_file(file): | ||
yield file | ||
else: | ||
out.error(title="Invalid path", message=f"{path} is not a file or directory") | ||
|
||
|
||
def is_erc7730_file(path: Path) -> bool: | ||
""" | ||
Check if a file is an ERC-7730 descriptor file. | ||
:param path: file path | ||
:return: true if the file is an ERC-7730 descriptor file | ||
""" | ||
return path.is_file() and ( | ||
path.name.startswith(ERC_7730_REGISTRY_CALLDATA_PREFIX) or path.name.startswith(ERC_7730_REGISTRY_EIP712_PREFIX) | ||
) |
Oops, something went wrong.