Skip to content

Commit

Permalink
refactor: move common constructor code to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
danceratopz committed Jul 5, 2023
1 parent 6c60144 commit 34cc32d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
13 changes: 3 additions & 10 deletions src/evm_transition_tool/evmone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""
import json
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

from ethereum_test_forks import Fork

from .transition_tool import TransitionTool, TransitionToolNotFoundInPath
from .transition_tool import TransitionTool


def write_json_file(data: Dict[str, Any], file_path: str) -> None:
Expand All @@ -38,15 +37,9 @@ def __init__(
binary: Optional[Path] = None,
trace: bool = False,
):
if binary is None:
binary = self.default_binary
binary = shutil.which(os.path.expanduser(binary)) # type: ignore
if not binary:
raise TransitionToolNotFoundInPath(binary=binary)
self.binary = Path(binary)
if trace:
super().__init__(binary=binary, trace=trace)
if self.trace:
raise Exception("`evmone-t8n` does not support tracing.")
self.trace = trace

@staticmethod
def matches_binary_path(binary_path: Path) -> bool:
Expand Down
13 changes: 3 additions & 10 deletions src/evm_transition_tool/geth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

import json
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

from ethereum_test_forks import Fork

from .transition_tool import TransitionTool, TransitionToolNotFoundInPath
from .transition_tool import TransitionTool


class GethTransitionTool(TransitionTool):
Expand All @@ -21,7 +20,7 @@ class GethTransitionTool(TransitionTool):
"""

default_binary = Path("evm")
binary: Path | None
binary: Path
cached_version: Optional[str] = None
trace: bool

Expand All @@ -31,13 +30,7 @@ def __init__(
binary: Optional[Path] = None,
trace: bool = False,
):
if binary is None:
binary = self.default_binary
binary = shutil.which(os.path.expanduser(binary)) # type: ignore
if not binary:
raise TransitionToolNotFoundInPath(binary=binary)
self.binary = Path(binary)
self.trace = trace
super().__init__(binary=binary, trace=trace)
args = [str(self.binary), "t8n", "--help"]
try:
result = subprocess.run(args, capture_output=True, text=True)
Expand Down
12 changes: 11 additions & 1 deletion src/evm_transition_tool/transition_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Transition tool abstract class.
"""

import os
import shutil
from abc import abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Type
Expand Down Expand Up @@ -34,6 +36,7 @@ class TransitionTool:

registered_tools: List[Type["TransitionTool"]] = []
default_tool: Optional[Type["TransitionTool"]] = None
default_binary: Path | None = None

# Abstract methods that each tool must implement

Expand All @@ -47,7 +50,14 @@ def __init__(
"""
Abstract initialization method that all subclasses must implement.
"""
pass
if binary is None:
binary = self.default_binary
# expanduser: tilde does not get expanded by shutil.which
binary = shutil.which(os.path.expanduser(binary)) # type: ignore
if not binary:
raise TransitionToolNotFoundInPath(binary=binary)
self.binary = Path(binary)
self.trace = trace

def __init_subclass__(cls):
"""
Expand Down

0 comments on commit 34cc32d

Please sign in to comment.