Skip to content

Commit

Permalink
refactor: move get_interface_file_path to avoid circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdefinitelyahuman committed Aug 21, 2020
1 parent 74b7e2b commit c1b1bc8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
13 changes: 13 additions & 0 deletions vyper/cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from pathlib import Path
from typing import Sequence

from vyper import ast as vy_ast
from vyper.exceptions import StructureException
from vyper.typing import InterfaceImports, SourceCode


def get_interface_file_path(base_paths: Sequence, import_path: str) -> Path:
relative_path = Path(import_path)
for path in base_paths:
file_path = path.joinpath(relative_path)
suffix = next((i for i in (".vy", ".json") if file_path.with_suffix(i).exists()), None)
if suffix:
return file_path.with_suffix(suffix)
raise FileNotFoundError(f" Cannot locate interface '{import_path}{{.vy,.json}}'")


def extract_file_interface_imports(code: SourceCode) -> InterfaceImports:
ast_tree = vy_ast.parse_to_ast(code)

Expand Down
17 changes: 5 additions & 12 deletions vyper/cli/vyper_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import warnings
from collections import OrderedDict
from pathlib import Path
from typing import Dict, Iterable, Iterator, Sequence, Set, TypeVar
from typing import Dict, Iterable, Iterator, Set, TypeVar

import vyper
from vyper.cli import vyper_json
from vyper.cli.utils import extract_file_interface_imports
from vyper.cli.utils import (
extract_file_interface_imports,
get_interface_file_path,
)
from vyper.opcodes import DEFAULT_EVM_VERSION, EVM_VERSIONS
from vyper.parser import parser_utils
from vyper.settings import VYPER_TRACEBACK_LIMIT
Expand Down Expand Up @@ -183,16 +186,6 @@ def get_interface_codes(root_path: Path, contract_sources: ContractCodes) -> Dic
return interfaces


def get_interface_file_path(base_paths: Sequence, import_path: str) -> Path:
relative_path = Path(import_path)
for path in base_paths:
file_path = path.joinpath(relative_path)
suffix = next((i for i in (".vy", ".json") if file_path.with_suffix(i).exists()), None)
if suffix:
return file_path.with_suffix(suffix)
raise FileNotFoundError(f" Cannot locate interface '{import_path}{{.vy,.json}}'")


def compile_files(
input_files: Iterable[str],
output_formats: OutputFormats,
Expand Down
6 changes: 4 additions & 2 deletions vyper/cli/vyper_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from typing import Callable, Dict, Tuple, Union

import vyper
from vyper.cli.utils import extract_file_interface_imports
from vyper.cli.vyper_compile import get_interface_file_path
from vyper.cli.utils import (
extract_file_interface_imports,
get_interface_file_path,
)
from vyper.exceptions import JSONError
from vyper.opcodes import DEFAULT_EVM_VERSION, EVM_VERSIONS
from vyper.typing import ContractCodes, ContractPath
Expand Down

0 comments on commit c1b1bc8

Please sign in to comment.