def db_class(name): '''Returns a DB engine class.''' for db_class in util.subclasses(Storage): if db_class.__name__.lower() == name.lower(): db_class.import_module() return db_class raise RuntimeError('unrecognised DB engine "{}"'.format(name))
def lookup_xverbytes(verbytes): '''Return a (is_xpub, coin_class) pair given xpub/xprv verbytes.''' # Order means BTC testnet will override NMC testnet for coin in util.subclasses(Coin): if verbytes == coin.XPUB_VERBYTES: return True, coin if verbytes == coin.XPRV_VERBYTES: return False, coin raise CoinError('version bytes unrecognised')
def lookup_coin_class(cls, name, net): '''Return a coin class given name and network. Raise an exception if unrecognised.''' for coin in util.subclasses(Coin): if (coin.NAME.lower() == name.lower() and coin.NET.lower() == net.lower()): return coin raise CoinError('unknown coin {} and network {} combination'.format( name, net))
def lookup_coin_class(cls, name, net): '''Return a coin class given name and network. Raise an exception if unrecognised.''' req_attrs = ['TX_COUNT', 'TX_COUNT_HEIGHT', 'TX_PER_BLOCK'] for coin in util.subclasses(Coin): if (coin.NAME.lower() == name.lower() and coin.NET.lower() == net.lower()): coin_req_attrs = req_attrs.copy() missing = [attr for attr in coin_req_attrs if not hasattr(coin, attr)] if missing: raise CoinError('coin {} missing {} attributes' .format(name, missing)) return coin raise CoinError('unknown coin {} and network {} combination' .format(name, net))
import gc import pytest import os from server.storage import Storage, db_class from lib.util import subclasses # Find out which db engines to test # Those that are not installed will be skipped db_engines = [] for c in subclasses(Storage): try: c.import_module() except ImportError: db_engines.append(pytest.mark.skip(c.__name__)) else: db_engines.append(c.__name__) @pytest.fixture(params=db_engines) def db(tmpdir, request): cwd = os.getcwd() os.chdir(str(tmpdir)) db = db_class(request.param)("db", False) yield db os.chdir(cwd) # Make sure all the locks and handles are closed del db gc.collect()
def test_subclasses(): assert util.subclasses(Base) == [A, B] assert util.subclasses(Base, strict=False) == [A, B, Base]