Skip to content

Commit

Permalink
Ensure we can set the config without creating an instance
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Apr 8, 2019
1 parent c2b4c46 commit c7503d6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
21 changes: 7 additions & 14 deletions graphenecommon/chain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import logging
from graphenestorage import SqliteConfigurationStore
from .instance import SharedInstance

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -131,19 +130,13 @@ def unlock(self, *args, **kwargs):
def set_shared_instance(self):
""" This method allows to set the current instance as default
"""
SharedInstance.instance = self

def set_shared_config(self, config):
""" This allows to set a config that will be used when calling
``shared_blockchain_instance`` and allows to define the configuration
without requiring to actually create an instance
"""
assert isinstance(config, dict)
SharedInstance.config.update(config)
# if one is already set, delete
if SharedInstance.instance:
self.clear_cache()
SharedInstance.instance = None
# self._sharedInstance.instance = self
log.warning(
DeprecationWarning(
"set_shared_instance in chaininstance is no longer supported. "
"This interface no longer exists, please use .instance.set_shared_instance() instead."
)
)

# -------------------------------------------------------------------------
# General transaction/operation stuff
Expand Down
47 changes: 41 additions & 6 deletions graphenecommon/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class AbstractBlockchainInstanceProvider:
contains an instance of the main chain instance
"""

_sharedInstance = SharedInstance

def __init__(self, *args, **kwargs):
self._blockchain = None
if kwargs.get("blockchain_instance"):
Expand Down Expand Up @@ -68,19 +70,52 @@ def shared_blockchain_instance(self):
The purpose of this method is to have offer single default
instance that can be reused by multiple classes.
"""
if not SharedInstance.instance:
if not self._sharedInstance.instance:
klass = self.get_instance_class()
SharedInstance.instance = klass(**SharedInstance.config)
return SharedInstance.instance
self._sharedInstance.instance = klass(**self._sharedInstance.config)
return self._sharedInstance.instance

@staticmethod
def set_shared_blockchain_instance(instance):
@classmethod
def set_shared_blockchain_instance(cls, instance):
""" This method allows us to override default instance for all
users of ``SharedInstance.instance``.
:param chaininstance instance: Chain instance
"""
SharedInstance.instance = instance
cls._sharedInstance.instance = instance

# -------------------------------------------------------------------------
# Shared instance interface
# -------------------------------------------------------------------------
def set_shared_instance(self):
""" This method allows to set the current instance as default
"""
self._sharedInstance.instance = self

@classmethod
def set_shared_config(cls, config):
""" This allows to set a config that will be used when calling
``shared_blockchain_instance`` and allows to define the configuration
without requiring to actually create an instance
"""
assert isinstance(config, dict)
cls._sharedInstance.config.update(config)
# if one is already set, delete
if cls._sharedInstance.instance:
cls._sharedInstance.instance = None


def shared_blockchain_instance():
return BlockchainInstance().shared_blockchain_instance()


def set_shared_blockchain_instance(instance):
instance.clear_cache()
BlockchainInstance.set_shared_blockchain_instance(instance)


def set_shared_config(config):
BlockchainInstance.set_shared_config(config)


# Legacy alias
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
from graphenecommon.instance import (
BlockchainInstance as GBlockchainInstance,
SharedInstance as GSharedInstance,
shared_blockchain_instance,
set_shared_blockchain_instance,
set_shared_config,
)
from graphenecommon.amount import Amount as GAmount
from graphenecommon.account import Account as GAccount, AccountUpdate as GAccountUpdate
Expand Down
19 changes: 13 additions & 6 deletions tests/test_instance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# -*- coding: utf-8 -*-
import unittest
from .fixtures import fixture_data, Chain, SharedInstance
from .fixtures import (
fixture_data,
Chain,
SharedInstance,
shared_blockchain_instance,
set_shared_blockchain_instance,
set_shared_config,
)


class Testcases(unittest.TestCase):
Expand All @@ -10,18 +17,18 @@ def setUp(self):
def test_shared_instance(self):
self.assertFalse(SharedInstance.instance)
c = Chain()
c.set_shared_instance()
set_shared_blockchain_instance(c)
self.assertEqual(id(c), id(SharedInstance.instance))
c2 = Chain()
c2.set_shared_instance()
set_shared_blockchain_instance(c2)
self.assertEqual(id(c2), id(SharedInstance.instance))

def test_shared_config(self):
self.assertFalse(SharedInstance.config)
c = Chain()
c.set_shared_config(dict(nobroadcast=True))
set_shared_config(dict(nobroadcast=True))
self.assertTrue(SharedInstance.config.get("nobroadcast", False))

c.set_shared_instance()
c.set_shared_config(dict(nobroadcast=False))
set_shared_blockchain_instance(c)
set_shared_config(dict(nobroadcast=False))
self.assertFalse(SharedInstance.config.get("nobroadcast", True))

0 comments on commit c7503d6

Please sign in to comment.