Skip to content

Commit

Permalink
Update get_default_modules structure to cleaner format
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Mar 12, 2019
1 parent 8253f5e commit d78e187
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 33 deletions.
3 changes: 2 additions & 1 deletion docs/web3.personal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ The following methods are available on the ``web3.parity.personal`` namespace.

.. code-block:: python
# Invalid call to personal_unlockAccount on Parity currently returns True, due to Parity bug
>>> web3.parity.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'wrong-passphrase')
False
True
>>> web3.parity.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'the-passphrase')
True
Expand Down
2 changes: 1 addition & 1 deletion tests/core/method-class/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class FakeModule(ModuleV2):
def dummy_w3():
return Web3(
EthereumTesterProvider(),
modules=[{'name': 'fake', 'module': FakeModule}],
modules={'fake': (FakeModule,)},
middlewares=[])


Expand Down
14 changes: 7 additions & 7 deletions tests/core/version-module/test_version_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
def blocking_w3():
return Web3(
EthereumTesterProvider(),
modules=[
{"name": "blocking_version", "module": BlockingVersion},
{"name": "legacy_version", "module": Version},
])
modules={
"blocking_version": (BlockingVersion,),
"legacy_version": (Version,),
})


@pytest.fixture
def async_w3():
return Web3(
AsyncEthereumTesterProvider(),
middlewares=[],
modules=[
{"name": 'async_version', "module": AsyncVersion},
])
modules={
'async_version': (AsyncVersion,),
})


def test_blocking_version(blocking_w3):
Expand Down
16 changes: 16 additions & 0 deletions web3/_utils/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from web3.exceptions import (
ValidationError,
)


def attach_modules(parent_module, module_definitions):
for module_name, module_info in module_definitions.items():
module_class = module_info[0]
module_class.attach(parent_module, module_name)

if len(module_info) == 2:
submodule_definitions = module_info[1]
module = getattr(parent_module, module_name)
attach_modules(module, submodule_definitions)
elif len(module_info) != 1:
raise ValidationError("Module definitions can only have 1 or 2 elements.")
1 change: 1 addition & 0 deletions web3/_utils/module_testing/personal_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def test_personal_unlockAccount_success(self,
)
assert result is True

# Seems to be an issue with Parity since this should return False
def test_personal_unlockAccount_failure(self,
web3,
unlockable_account_dual_type):
Expand Down
35 changes: 19 additions & 16 deletions web3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
to_text,
to_json,
)
from web3._utils.module import (
attach_modules,
)
from web3._utils.normalizers import (
abi_ens_resolver,
)
Expand Down Expand Up @@ -84,17 +87,21 @@


def get_default_modules():
return [
{"name": "eth", "module": Eth},
{"name": "net", "module": Net},
{"name": "version", "module": Version},
{"name": "txpool", "module": TxPool},
{"name": "miner", "module": Miner},
{"name": "admin", "module": Admin},
{"name": "parity", "module": Parity, 'submodules': {'personal': ParityPersonal}},
{"name": "geth", "module": Geth, 'submodules': {'personal': GethPersonal}},
{"name": "testing", "module": Testing},
]
return {
"eth": (Eth,),
"net": (Net,),
"version": (Version,),
"txpool": (TxPool,),
"miner": (Miner,),
"admin": (Admin,),
"parity": (Parity, {
"personal": (ParityPersonal,)
}),
"geth": (Geth, {
"personal": (GethPersonal,)
}),
"testing": (Testing,),
}


class Web3:
Expand Down Expand Up @@ -132,11 +139,7 @@ def __init__(self, provider=None, middlewares=None, modules=None, ens=empty):
if modules is None:
modules = get_default_modules()

for module in modules:
module['module'].attach(self, module['name'])
if 'submodules' in module:
for subname, submodule in module['submodules'].items():
submodule.attach(getattr(self, module['name']), subname)
attach_modules(self, modules)

self.ens = ens

Expand Down
8 changes: 0 additions & 8 deletions web3/personal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,53 @@ def importRawKey():
return Method(
"personal_importRawKey",
mungers=[default_root_munger],
formatter_lookup_fn=None,
)


def newAccount():
return Method(
"personal_newAccount",
mungers=[default_root_munger],
formatter_lookup_fn=None,
)


def listAccounts():
return Method(
"personal_listAccounts",
mungers=None,
formatter_lookup_fn=None,
)


def sendTransaction():
return Method(
"personal_sendTransaction",
mungers=[default_root_munger],
formatter_lookup_fn=None,
)


def lockAccount():
return Method(
"personal_lockAccount",
mungers=[default_root_munger],
formatter_lookup_fn=None,
)


def unlockAccount():
return Method(
"personal_unlockAccount",
mungers=[default_root_munger],
formatter_lookup_fn=None,
)


def sign():
return Method(
"personal_sign",
mungers=[default_root_munger],
formatter_lookup_fn=None,
)


def ecRecover():
return Method(
"personal_ecRecover",
mungers=[default_root_munger],
formatter_lookup_fn=None,
)

0 comments on commit d78e187

Please sign in to comment.