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.''' 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.''' 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 pytest import os from electrumx.server.storage import Storage, db_class from electrumx.lib.util import subclasses # Find out which db engines to test # Those that are not installed will be skipped db_engines = [] for klass in subclasses(Storage): try: klass.import_module() except ImportError: db_engines.append("skip") else: db_engines.append(klass.__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) db.close() def test_put_get(db): db.put(b"x", b"y") assert db.get(b"x") == b"y"
import pytest import os from electrumx.server.storage import Storage, db_class from electrumx.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)) try: name = request.param.args[0] except AttributeError: name = request.param db = db_class(name)("db", False) yield db os.chdir(cwd) db.close()
def test_subclasses(): assert util.subclasses(Base) == [A, B] assert util.subclasses(Base, strict=False) == [A, B, Base]