Skip to content

Commit

Permalink
Add graphene common, instance, and wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Dec 10, 2018
1 parent fd1b900 commit 43e7154
Show file tree
Hide file tree
Showing 5 changed files with 446 additions and 31 deletions.
Empty file added graphenecommon/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions graphenecommon/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
__all__ = [
"InvalidWifError",
"KeyAlreadyInStoreException",
"KeyNotFound",
"NoWalletException",
"OfflineHasNoRPCException",
"WalletExists",
"WalletLocked",
]


class WalletExists(Exception):
""" A wallet has already been created and requires a password to be
unlocked by means of :func:`bitshares.wallet.unlock`.
"""

pass


class WalletLocked(Exception):
""" Wallet is locked
"""

pass


class OfflineHasNoRPCException(Exception):
""" When in offline mode, we don't have RPC
"""

pass


class NoWalletException(Exception):
""" No Wallet could be found, please use :func:`bitshares.wallet.create` to
create a new wallet
"""

pass


class KeyNotFound(Exception):
""" Key not found
"""

pass


class KeyAlreadyInStoreException(Exception):
""" The key is already stored in the store
"""

pass


class InvalidWifError(Exception):
""" The provided private Key has an invalid format
"""

pass
67 changes: 67 additions & 0 deletions graphenecommon/instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class SharedInstance:
""" This class merely offers a singelton for the Blockchain Instance
"""

instance = None
config = {}


class BlockchainInstance:
""" This is a class that allows compatibility with previous
naming conventions. It will extract 'blockchain_instance'
from the key word arguments and ensure that self.blockchain
contains an instance of the main chain instance
"""

def get_instance_class(self):
""" Should return the Chain instance class, e.g. `bitshares.BitShares`
"""
raise NotImplementedError

def clear_cache(self):
raise NotImplementedError

def __init__(self, *args, **kwargs):
if "blockchain_instance" in kwargs and kwargs["blockchain_instance"]:
SharedInstance.instance = kwargs["blockchain_instance"]

@property
def blockchain(self):
return self.shared_blockchain_instance()

@property
def chain(self):
""" Short form for blockchain (for the lazy)
"""
return self.blockchain

def shared_blockchain_instance(self):
""" This method will initialize ``SharedInstance.instance`` and return it.
The purpose of this method is to have offer single default
instance that can be reused by multiple classes.
"""
if not SharedInstance.instance:
klass = self.get_instance_class()
SharedInstance.instance = klass(**SharedInstance.config)
return SharedInstance.instance

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

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.shared_blockchain_instance().clear_cache()
SharedInstance.instance = None
Loading

0 comments on commit 43e7154

Please sign in to comment.