diff --git a/autobahn/asyncio/xbr/__init__.py b/autobahn/asyncio/xbr/__init__.py index f29831c65..1acff4f13 100644 --- a/autobahn/asyncio/xbr/__init__.py +++ b/autobahn/asyncio/xbr/__init__.py @@ -38,7 +38,7 @@ import uuid - from autobahn.xbr._util import hl + from autobahn.util import hl from autobahn.xbr._interfaces import IProvider, ISeller, IConsumer, IBuyer def run_in_executor(*args, **kwargs): diff --git a/autobahn/twisted/websocket.py b/autobahn/twisted/websocket.py index 01c99541e..a9fb4cbe8 100644 --- a/autobahn/twisted/websocket.py +++ b/autobahn/twisted/websocket.py @@ -39,7 +39,7 @@ ConnectionLost from twisted.internet.defer import Deferred -from autobahn.util import public +from autobahn.util import public, hltype from autobahn.util import _is_tls_error, _maybe_tls_reason from autobahn.wamp import websocket from autobahn.websocket.types import ConnectionRequest, ConnectionResponse, ConnectionDeny @@ -47,7 +47,6 @@ from autobahn.websocket import protocol from autobahn.websocket.interfaces import IWebSocketClientAgent from autobahn.twisted.util import peer2str, transport_channel_id -from autobahn.xbr._util import hltype from autobahn.websocket.compress import PerMessageDeflateOffer, \ PerMessageDeflateOfferAccept, \ diff --git a/autobahn/twisted/xbr/__init__.py b/autobahn/twisted/xbr/__init__.py index 3233a0452..a84b2f8da 100644 --- a/autobahn/twisted/xbr/__init__.py +++ b/autobahn/twisted/xbr/__init__.py @@ -43,7 +43,7 @@ import uuid - from autobahn.xbr._util import hl + from autobahn.util import hl from autobahn.xbr._interfaces import IProvider, ISeller, IConsumer, IBuyer, IDelegate from autobahn.xbr import _seller, _buyer, _blockchain diff --git a/autobahn/util.py b/autobahn/util.py index 5dc6ccf89..a7e26818e 100644 --- a/autobahn/util.py +++ b/autobahn/util.py @@ -23,7 +23,7 @@ # THE SOFTWARE. # ############################################################################### - +import inspect import os import time import struct @@ -68,7 +68,15 @@ "generate_activation_code", "generate_serial_number", "generate_user_password", - "machine_id") + "machine_id", + "hl", + "hltype", + "hlid", + "hluserid", + "hlval", + "hlcontract", + "with_0x", + "without_0x") def public(obj): @@ -912,3 +920,65 @@ def machine_id() -> str: return plistlib.loads(plist_data)[0]["IOPlatformSerialNumber"] else: return socket.gethostname() + + +try: + import click + _HAS_CLICK = True +except ImportError: + _HAS_CLICK = False + + +def hl(text, bold=False, color='yellow'): + if not isinstance(text, str): + text = '{}'.format(text) + if _HAS_CLICK: + return click.style(text, fg=color, bold=bold) + else: + return text + + +def _qn(obj): + if inspect.isclass(obj) or inspect.isfunction(obj) or inspect.ismethod(obj): + qn = '{}.{}'.format(obj.__module__, obj.__qualname__) + else: + qn = 'unknown' + return qn + + +def hltype(obj): + qn = _qn(obj).split('.') + text = hl(qn[0], color='yellow', bold=True) + hl('.' + '.'.join(qn[1:]), color='yellow', bold=False) + return '<' + text + '>' + + +def hlid(oid): + return hl('{}'.format(oid), color='blue', bold=True) + + +def hluserid(oid): + if not isinstance(oid, str): + oid = '{}'.format(oid) + return hl('"{}"'.format(oid), color='yellow', bold=True) + + +def hlval(val, color='green'): + return hl('{}'.format(val), color=color, bold=True) + + +def hlcontract(oid): + if not isinstance(oid, str): + oid = '{}'.format(oid) + return hl('<{}>'.format(oid), color='magenta', bold=True) + + +def with_0x(address): + if address and not address.startswith('0x'): + return '0x{address}'.format(address=address) + return address + + +def without_0x(address): + if address and address.startswith('0x'): + return address[2:] + return address diff --git a/autobahn/websocket/protocol.py b/autobahn/websocket/protocol.py index 35d848207..fd8237796 100755 --- a/autobahn/websocket/protocol.py +++ b/autobahn/websocket/protocol.py @@ -48,7 +48,7 @@ from autobahn.websocket.types import ConnectingRequest, ConnectionRequest, ConnectionResponse, ConnectionDeny from autobahn.wamp.types import TransportDetails -from autobahn.util import Stopwatch, wildcards2patterns, encode_truncate +from autobahn.util import Stopwatch, wildcards2patterns, encode_truncate, hltype from autobahn.util import _LazyHexFormatter from autobahn.util import ObservableMixin from autobahn.websocket.utf8validator import Utf8Validator @@ -57,7 +57,6 @@ from autobahn.websocket.util import parse_url from autobahn.exception import PayloadExceededError, Disconnected from autobahn.util import _maybe_tls_reason -from autobahn.xbr._util import hltype import txaio import hyperlink diff --git a/autobahn/xbr/__init__.py b/autobahn/xbr/__init__.py index b4c33d038..c1fd7b4a1 100644 --- a/autobahn/xbr/__init__.py +++ b/autobahn/xbr/__init__.py @@ -49,7 +49,7 @@ from autobahn.xbr._abi import XBR_DEBUG_TOKEN_ADDR, XBR_DEBUG_NETWORK_ADDR, XBR_DEBUG_MARKET_ADDR, XBR_DEBUG_CATALOG_ADDR, XBR_DEBUG_CHANNEL_ADDR # noqa from autobahn.xbr._abi import XBR_DEBUG_TOKEN_ADDR_SRC, XBR_DEBUG_NETWORK_ADDR_SRC, XBR_DEBUG_MARKET_ADDR_SRC, XBR_DEBUG_CATALOG_ADDR_SRC, XBR_DEBUG_CHANNEL_ADDR_SRC # noqa from autobahn.xbr._interfaces import IMarketMaker, IProvider, IConsumer, ISeller, IBuyer, IDelegate # noqa - from autobahn.xbr._util import make_w3, pack_uint256, unpack_uint256, with_0x, without_0x # noqa + from autobahn.xbr._util import make_w3, pack_uint256, unpack_uint256 # noqa from autobahn.xbr._eip712_member_register import sign_eip712_member_register, recover_eip712_member_register # noqa from autobahn.xbr._eip712_member_login import sign_eip712_member_login, recover_eip712_member_login # noqa @@ -324,8 +324,6 @@ def account_from_ethkey(ethkey: bytes) -> eth_account.account.Account: 'make_w3', 'pack_uint256', 'unpack_uint256', - 'with_0x', - 'without_0x', 'generate_seedphrase', 'check_seedphrase', 'account_from_seedphrase', diff --git a/autobahn/xbr/_buyer.py b/autobahn/xbr/_buyer.py index 2c3078805..952e18deb 100644 --- a/autobahn/xbr/_buyer.py +++ b/autobahn/xbr/_buyer.py @@ -42,7 +42,7 @@ import eth_keys -from ._util import hl, hlval +from ..util import hl, hlval from ._eip712_channel_close import sign_eip712_channel_close, recover_eip712_channel_close diff --git a/autobahn/xbr/_cli.py b/autobahn/xbr/_cli.py index 5d872b25b..23736ddb5 100644 --- a/autobahn/xbr/_cli.py +++ b/autobahn/xbr/_cli.py @@ -85,8 +85,7 @@ from autobahn.xbr import ActorType, ChannelType from autobahn.xbr._config import load_or_create_profile -from autobahn.xbr._util import hlval, hlid, hltype - +from autobahn.util import hltype, hlid, hlval _COMMANDS = ['version', 'get-member', 'register-member', 'register-member-verify', 'get-market', 'create-market', 'create-market-verify', diff --git a/autobahn/xbr/_gui.py b/autobahn/xbr/_gui.py index 8e3d9b3fc..d6a4b2dc0 100644 --- a/autobahn/xbr/_gui.py +++ b/autobahn/xbr/_gui.py @@ -56,7 +56,7 @@ import click from humanize import naturaldelta, naturaltime -from autobahn.util import parse_activation_code +from autobahn.util import parse_activation_code, hltype, hlid, hlval from autobahn.wamp.serializer import CBORSerializer from autobahn.twisted.util import sleep from autobahn.twisted.wamp import ApplicationRunner @@ -64,7 +64,6 @@ from autobahn.xbr import account_from_seedphrase, generate_seedphrase, account_from_ethkey from autobahn.xbr._cli import Client from autobahn.xbr._config import UserConfig, Profile -from autobahn.xbr._util import hlval, hlid, hltype LOGO_RESOURCE = pkg_resources.resource_filename('autobahn', 'asset/xbr_gray.svg') print(LOGO_RESOURCE, os.path.isfile(LOGO_RESOURCE)) diff --git a/autobahn/xbr/_seller.py b/autobahn/xbr/_seller.py index bd53a6618..fe1f3bbaf 100644 --- a/autobahn/xbr/_seller.py +++ b/autobahn/xbr/_seller.py @@ -42,7 +42,7 @@ import nacl.public import txaio -from ._util import hl, hlval +from ..util import hl, hlval from ._eip712_channel_close import sign_eip712_channel_close, recover_eip712_channel_close diff --git a/autobahn/xbr/_util.py b/autobahn/xbr/_util.py index 3273385c6..c51a72022 100644 --- a/autobahn/xbr/_util.py +++ b/autobahn/xbr/_util.py @@ -24,9 +24,6 @@ # ############################################################################### -import inspect - -import click import web3 @@ -108,55 +105,3 @@ def pack_uint256(value): return b'\x00' * (32 - len(data)) + data else: return b'\x00' * 32 - - -def hl(text, bold=False, color='yellow'): - if not isinstance(text, str): - text = '{}'.format(text) - return click.style(text, fg=color, bold=bold) - - -def _qn(obj): - if inspect.isclass(obj) or inspect.isfunction(obj) or inspect.ismethod(obj): - qn = '{}.{}'.format(obj.__module__, obj.__qualname__) - else: - qn = 'unknown' - return qn - - -def hltype(obj): - qn = _qn(obj).split('.') - text = hl(qn[0], color='yellow', bold=True) + hl('.' + '.'.join(qn[1:]), color='yellow', bold=False) - return '<' + text + '>' - - -def hlid(oid): - return hl('{}'.format(oid), color='blue', bold=True) - - -def hluserid(oid): - if not isinstance(oid, str): - oid = '{}'.format(oid) - return hl('"{}"'.format(oid), color='yellow', bold=True) - - -def hlval(val, color='green'): - return hl('{}'.format(val), color=color, bold=True) - - -def hlcontract(oid): - if not isinstance(oid, str): - oid = '{}'.format(oid) - return hl('<{}>'.format(oid), color='magenta', bold=True) - - -def with_0x(address): - if address and not address.startswith('0x'): - return '0x{address}'.format(address=address) - return address - - -def without_0x(address): - if address and address.startswith('0x'): - return address[2:] - return address diff --git a/setup.py b/setup.py index 5bf4c4120..085c4d2c9 100644 --- a/setup.py +++ b/setup.py @@ -116,6 +116,9 @@ # XBR contracts and ABI file bundle 'xbr>=21.2.1', # Apache 2.0 + # CLI handling and color terminal output + 'click>=8.1.2', # BSD license + # the following is needed for XBR basics and XBR IDL code generation 'cbor2>=5.2.0', # MIT license 'zlmdb>=21.2.1', # MIT license @@ -235,7 +238,7 @@ def run_tests(self): setup( name='autobahn', - version=__version__, + version=__version__, # noqa description='WebSocket client & server library, WAMP real-time framework', long_description=docstr, license='MIT License', @@ -253,7 +256,7 @@ def run_tests(self): ], extras_require={ 'all': extras_require_all, - 'asyncio': [], # backwards compatibility + 'asyncio': [], # backwards compatibility 'twisted': extras_require_twisted, 'accelerate': extras_require_accelerate, 'compress': extras_require_compress,