From 7cbd2c0f3b9f9087331223df75ddc5b59d63d8fe Mon Sep 17 00:00:00 2001 From: Mike Shultz Date: Fri, 15 Mar 2019 22:01:37 -0600 Subject: [PATCH] minor cleanup --- solidbyte/script/__init__.py | 46 ++++++++++++------------------------ 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/solidbyte/script/__init__.py b/solidbyte/script/__init__.py index 6c6e693..9c0bbf7 100644 --- a/solidbyte/script/__init__.py +++ b/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 @@ -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? @@ -35,18 +37,11 @@ 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, } @@ -54,23 +49,12 @@ def get_availble_script_kwargs(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") @@ -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: