From 1bb2c5bac2dca05240eaf0a14fbf555c79ca920c Mon Sep 17 00:00:00 2001 From: avik Date: Wed, 10 Jul 2024 01:17:27 +0530 Subject: [PATCH] chore: code refactored & working on fixing the cli --- README.md | 8 +- pyproject.toml | 149 +++++++++++++++++++++++++++---------- setup.cfg | 7 -- setup.py | 84 --------------------- src/ape_utils/__about__.py | 2 +- src/ape_utils/__init__.py | 3 + src/ape_utils/_cli.py | 17 +++-- src/ape_utils/utils.py | 17 ++--- 8 files changed, 135 insertions(+), 152 deletions(-) delete mode 100644 setup.cfg delete mode 100644 setup.py diff --git a/README.md b/README.md index 711c53a..3a71741 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ TODO: Description You can install the latest release via [`pip`](https://pypi.org/project/pip/): ```bash -pip install +pip install ape_utils ``` ### via `setuptools` @@ -21,9 +21,9 @@ pip install You can clone the repository and use [`setuptools`](https://github.com/pypa/setuptools) for the most up-to-date version: ```bash -git clone https://github.com/ApeWorX/.git -cd -python3 setup.py install +git clone https://github.com/Aviksaikat/ape_utils.git +cd ape_utils +pip install -e . ``` ## Quick Usage diff --git a/pyproject.toml b/pyproject.toml index 6156c35..23af65d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,15 +24,114 @@ classifiers = [ ] dependencies = [] +# Cli tool name & options +[project.scripts] +ape_call = "ape_utils._cli:main" + [project.urls] Documentation = "https://github.com/Aviksaikat/ape-utils#readme" Issues = "https://github.com/Aviksaikat/ape-utils/issues" Source = "https://github.com/Aviksaikat/ape-utils" [tool.hatch.version] +source = "regex_commit" +commit_extra_args = ["-e"] path = "src/ape_utils/__about__.py" +################## +# External Tools # +################## + +[tool.mypy] +files = ["ape_utils"] +exclude = ["build/", "dist/", "docs/", "tests/"] +disallow_untyped_defs = true +disallow_any_unimported = true +no_implicit_optional = true +check_untyped_defs = true +warn_return_any = true +warn_unused_ignores = true +show_error_codes = true +ignore_missing_imports = true +# plugins = ["pydantic.mypy"] + + +[tool.ruff] +target-version = "py39" +line-length = 120 +indent-width = 4 +include = [ + "src/**/*.py", + "src/**/*.pyi", + "tests/**/*.py", + "tests/**/*.pyi" +] +exclude = ["tests", "src/ape_utils/_version.py"] + +[tool.ruff.lint] +preview = true # preview features & checks, use with caution +extend-select = [ # features in preview + "W292", # missing-newline-at-end-of-file +] +select = [ + "A", + "ARG", + "B", + "C", + "DTZ", + "E", + "EM", + "F", + "FBT", # Boolean trap + "ICN", + "ISC", + "I", + "N", + "PLC", + "PLE", + "PLR", + "PLW", + "Q", + "RUF", + "S", + "T", + "TID", + "UP", + "W", + "YTT", + "RUF100", # Automatically remove unused # noqa directives +] +ignore = [ + # Allow non-abstract empty methods in abstract base classes + "B027", + # Allow boolean positional values in function calls, like `dict.get(... True)` + "FBT003", + # Ignore checks for possible passwords + "S105", "S106", "S107", + # Ignore complexity + "C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915", + "PLC1901", # empty string comparisons + "PLW2901", # `for` loop variable overwritten + "SIM114", # Combine `if` branches using logical `or` operator + "E203", # Whitespace before :, needed for black compatability and also `ruff format` + "ISC001", # causes unexpected behaviour with formatter +] + +[tool.ruff.lint.isort] +known-first-party = ["ape_utils"] + + +[tool.ruff.lint.flake8-tidy-imports] +ban-relative-imports = "all" + + +[tool.ruff.lint.per-file-ignores] +# Allow print/pprint +"examples/*" = ["T201"] +# Tests can use magic values, assertions, and relative imports +"tests/**/*" = ["PLR2004", "S101", "TID252"] + [tool.coverage.run] source_pkgs = ["src/ape_utils", "tests"] @@ -43,8 +142,8 @@ omit = [ ] [tool.coverage.paths] -ape_utils = ["src/ape_utils", "*/ape-utils/ape_utils"] -tests = ["tests", "*/ape-utils/tests"] +ape_utils = ["src/ape_utils", "*/ape_utils/src/ape_utils"] +tests = ["tests", "*/ape_utils/tests"] [tool.coverage.report] exclude_lines = [ @@ -62,57 +161,25 @@ dependencies = [ # "eth-ape", "multicall", "click", - "rich" -] - -[tool.hatch.envs.dev] -dependencies = [ "rich", - "multicall", - "click", - "commitizen", - "pre-commit", - "pytest-watch", - "IPython", - "ipdb", - "pytest>=6.0", - "pytest-xdist", - "pytest-cov", - "hypothesis>=6.2.0,<7.0", - "black>=24.4.2,<25", - "mypy>=1.10.0,<2", - "types-setuptools", - "types-requests", - "flake8>=7.0.0,<8", - "flake8-breakpoint>=1.1.0,<2", - "flake8-print>=5.0.0,<6", - "isort>=5.13.2,<6", - "mdformat>=0.7.17", - "mdformat-gfm>=0.3.5", - "mdformat-frontmatter>=0.4.1", - "mdformat-pyproject>=0.0.1", ] [tool.hatch.envs.test] -dependencies = ["pytest", "pytest-xdist", "pytest-cov", "hypothesis", "multicall", "click",] - +dependencies = ["pytest", "pytest-xdist", "pytest-cov", "hypothesis"] [tool.hatch.envs.lint] -dependencies = [ - "multicall", - "click", - "black", +# If you define environments with dependencies that only slightly differ from their +# inherited environments, you can use the extra-dependencies option to avoid redeclaring the +# dependencies option: https://hatch.pypa.io/latest/config/environment/overview/#dependencies +extra-dependencies = [ "mypy", - "flake8", - "isort", "types-setuptools", "types-requests", - "flake8-breakpoint>=1.1.0", - "flake8-print>=5.0.0", "mdformat>=0.7.17", "mdformat-gfm>=0.3.5", "mdformat-frontmatter>=0.4.1", "mdformat-pyproject>=0.0.1", + ] [tool.hatch.envs.lint.scripts] # check = "mypy --install-types --non-interactive {args:ape_utils tests}" @@ -134,5 +201,5 @@ lint-check = [ ] [build-system] -requires = ["hatchling"] +requires = ["hatchling", "hatch-regex-commit"] build-backend = "hatchling.build" \ No newline at end of file diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index c362920..0000000 --- a/setup.cfg +++ /dev/null @@ -1,7 +0,0 @@ -[flake8] -max-line-length = 100 -exclude = - venv* - .eggs - docs - build diff --git a/setup.py b/setup.py deleted file mode 100644 index 6898941..0000000 --- a/setup.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python -from setuptools import find_packages, setup - -extras_require = { - "test": [ # `test` GitHub Action jobs uses this - "pytest>=6.0", # Core testing package - "pytest-xdist", # Multi-process runner - "pytest-cov", # Coverage analyzer plugin - "hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer - ], - "lint": [ - "black>=24.4.2,<25", # Auto-formatter and linter - "mypy>=1.10.0,<2", # Static type analyzer - "types-setuptools", # Needed for mypy type shed - "types-requests", # Needed for mypy type shed - "flake8>=7.0.0,<8", # Style linter - "flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code - "flake8-print>=5.0.0,<6", # Detect print statements left in code - "isort>=5.13.2,<6", # Import sorting linter - "mdformat>=0.7.17", # Auto-formatter for markdown - "mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown - "mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates - "mdformat-pyproject>=0.0.1", # Allows configuring in pyproject.toml - ], - "release": [ # `release` GitHub Action job uses this - "setuptools", # Installation tool - "wheel", # Packaging tool - "twine", # Package upload tool - ], - "dev": [ - "commitizen", # Manage commits and publishing releases - "pre-commit", # Ensure that linters are run prior to committing - "pytest-watch", # `ptw` test watcher/runner - "IPython", # Console for interacting - "ipdb", # Debugger (Must use `export PYTHONBREAKPOINT=ipdb.set_trace`) - ], -} - -# NOTE: `pip install -e .[dev]` to install package -extras_require["dev"] = ( - extras_require["test"] - + extras_require["lint"] - + extras_require["release"] - + extras_require["dev"] -) - -with open("./README.md") as readme: - long_description = readme.read() - - -setup( - name="", - use_scm_version=True, - setup_requires=["setuptools_scm"], - description=""": """, - long_description=long_description, - long_description_content_type="text/markdown", - author="ApeWorX Ltd.", - author_email="admin@apeworx.io", - url="https://github.com/ApeWorX/", - include_package_data=True, - install_requires=[], - python_requires=">=3.9,<4", - extras_require=extras_require, - py_modules=[""], - license="Apache-2.0", - zip_safe=False, - keywords="ethereum", - packages=find_packages(exclude=["tests", "tests.*"]), - package_data={"": ["py.typed"]}, - classifiers=[ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "License :: OSI Approved :: Apache Software License", - "Natural Language :: English", - "Operating System :: MacOS", - "Operating System :: POSIX", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - ], -) diff --git a/src/ape_utils/__about__.py b/src/ape_utils/__about__.py index 06fbe7e..5820a72 100644 --- a/src/ape_utils/__about__.py +++ b/src/ape_utils/__about__.py @@ -1 +1 @@ -version = "0.0.1" \ No newline at end of file +version = "0.0.1" diff --git a/src/ape_utils/__init__.py b/src/ape_utils/__init__.py index 57e12c5..8780de9 100644 --- a/src/ape_utils/__init__.py +++ b/src/ape_utils/__init__.py @@ -1 +1,4 @@ # Add module top-level imports here +from ape_utils.utils import call_view_function + +__all__ = ["call_view_function"] diff --git a/src/ape_utils/_cli.py b/src/ape_utils/_cli.py index 873eef7..38df07f 100644 --- a/src/ape_utils/_cli.py +++ b/src/ape_utils/_cli.py @@ -1,16 +1,21 @@ import click + from ape_utils.utils import call_view_function + @click.command() -@click.option('--function-sig', prompt='Function signature', help='The function signature (e.g., gsr_query(uint256)(string)).') -@click.option('--address', prompt='Contract address', help='The address of the smart contract.') -@click.option('--args', prompt='Arguments', help='The arguments for the function call.', type=int) -def main(function_sig, address, args): +@click.option( + "--function-sig", prompt="Function signature", help="The function signature (e.g., function_name(uint256)(string))." +) +@click.option("--address", prompt="Contract address", help="The address of the smart contract.") +@click.option("--args", prompt="Arguments", help="The arguments for the function call.", type=int) +def main(function_sig: str, address: str, args: int) -> None: try: output = call_view_function(function_sig, address, args) click.echo(f"Output: {output}") except Exception as e: - click.echo(f"Error: {str(e)}") + click.echo(f"Error: {e!s}") + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/src/ape_utils/utils.py b/src/ape_utils/utils.py index 2287bab..7afad2b 100644 --- a/src/ape_utils/utils.py +++ b/src/ape_utils/utils.py @@ -1,26 +1,26 @@ -from multicall import Call # type: ignore -from web3 import Web3 import os +from typing import Any + +from multicall import Call from rich.console import Console from rich.traceback import install - - +from web3 import Web3 # install rich traceback install() console = Console() - -def call_view_function(function_sig: str, address: str, args: int) -> None: - +def call_view_function(function_sig: str, address: str, args: int) -> Any: w3 = Web3(Web3.HTTPProvider(os.environ["sepoliafork"])) # get_sitnature(address, function_sig) output = Call(address, [function_sig, args])(_w3=w3) - console.print(f"[blue]Output: [green bold]{output}") + # console.print(f"[blue]Output: [green bold]{output}") + return output + if __name__ == "__main__": function_sig: str = "gsr_query(uint256)(string)" @@ -28,4 +28,3 @@ def call_view_function(function_sig: str, address: str, args: int) -> None: args = 6147190 call_view_function(function_sig, address, args) -