Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshultz committed Mar 16, 2019
1 parent 841b153 commit 7cbd2c0
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions solidbyte/script/__init__.py
@@ -1,13 +1,13 @@
""" Functionality for running user scripts """
import inspect
from types import ModuleType
from typing import Optional, List
from typing import Optional, Any, Dict, List
from attrdict import AttrDict
# from importlib import SourceFileLoader
# from importlib.abc import SourceLoader
from importlib.util import spec_from_file_location, module_from_spec
from web3.eth import Contract as Web3Contract
from ..deploy import Deployer
from ..common.utils import to_path
from ..deploy.objects import Contract
from ..common.utils import Path, to_path
from ..common.web3 import web3c
from ..common.exceptions import InvalidScriptError
from ..common.logging import getLogger
Expand All @@ -16,15 +16,17 @@
deployer: Optional[Deployer] = None


def get_contracts(network):
def get_contracts(network: str) -> AttrDict:
""" Get a list of web3 contract instances. """
global deployer

if not deployer:
deployer = Deployer(network_name=network)

contracts = AttrDict({})
contracts: AttrDict = AttrDict({})

contract_name: str
contract: Contract
for contract_name, contract in deployer.contracts.items():
try:
# TODO: Internal API? Better option?
Expand All @@ -35,42 +37,24 @@ def get_contracts(network):
return contracts


def get_availble_script_kwargs(network):
global deployer

if not deployer:
deployer = Deployer(network_name=network)

contracts = get_contracts(network)

def get_availble_script_kwargs(network) -> Dict[str, Any]:
""" Get a dict of the kwargs available for user scripts """
return {
'web3': web3c.get_web3(network),
'contracts': contracts,
'deployer_account': deployer.account,
'contracts': get_contracts(network),
'network': network,
}


def run_script(network: str, script: str) -> bool:
""" Runs a user script """

scriptPath = to_path(script)
script_kwargs = get_availble_script_kwargs(network)
scriptPath: Path = to_path(script)
availible_script_kwargs: Dict[str, Any] = get_availble_script_kwargs(network)

spec = spec_from_file_location(scriptPath.name[:-3], str(scriptPath))
mod: Optional[ModuleType] = module_from_spec(spec)

# try:
# log.debug("Loading deploy script {}".format(scriptPath))
# mod = SourceFileLoader(scriptPath.name[:-3], str(scriptPath)).exec_module()
# except ModuleNotFoundError as e:
# if str(e) == "No module named 'deploy'":
# raise ScriptError(
# "Unable to import script."
# )
# else:
# raise e

if not spec or not spec.loader or not mod:
raise InvalidScriptError("Script not found")

Expand All @@ -81,8 +65,8 @@ def run_script(network: str, script: str) -> bool:
raise InvalidScriptError("Function main() must be defined in script")

func_spec = inspect.getfullargspec(mod.main)
script_kwargs = {k: script_kwargs.get(k) for k in func_spec.args}
retval = mod.main(**script_kwargs)
script_kwargs: Dict[str, Any] = {k: availible_script_kwargs.get(k) for k in func_spec.args}
retval: Any = mod.main(**script_kwargs)

# If a script choses to return False, they're signalling a failure
if retval is False:
Expand Down

0 comments on commit 7cbd2c0

Please sign in to comment.