Skip to content

Commit

Permalink
move autobahn.xbr._util.hl* to autobahn.util and check for click at r… (
Browse files Browse the repository at this point in the history
#1549)

* move autobahn.xbr._util.hl* to autobahn.util and check for click at run-time (fixes #1548 )
* fix module imports/exports
  • Loading branch information
oberstet committed Apr 19, 2022
1 parent 5865482 commit 3da0c26
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 74 deletions.
2 changes: 1 addition & 1 deletion autobahn/asyncio/xbr/__init__.py
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions autobahn/twisted/websocket.py
Expand Up @@ -39,15 +39,14 @@
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
from autobahn.wamp.types import TransportDetails
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, \
Expand Down
2 changes: 1 addition & 1 deletion autobahn/twisted/xbr/__init__.py
Expand Up @@ -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

Expand Down
74 changes: 72 additions & 2 deletions autobahn/util.py
Expand Up @@ -23,7 +23,7 @@
# THE SOFTWARE.
#
###############################################################################

import inspect
import os
import time
import struct
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions autobahn/websocket/protocol.py
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions autobahn/xbr/__init__.py
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion autobahn/xbr/_buyer.py
Expand Up @@ -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


Expand Down
3 changes: 1 addition & 2 deletions autobahn/xbr/_cli.py
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions autobahn/xbr/_gui.py
Expand Up @@ -56,15 +56,14 @@
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
from autobahn.xbr import unpack_uint256
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))
Expand Down
2 changes: 1 addition & 1 deletion autobahn/xbr/_seller.py
Expand Up @@ -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


Expand Down
55 changes: 0 additions & 55 deletions autobahn/xbr/_util.py
Expand Up @@ -24,9 +24,6 @@
#
###############################################################################

import inspect

import click
import web3


Expand Down Expand Up @@ -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
7 changes: 5 additions & 2 deletions setup.py
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand Down

0 comments on commit 3da0c26

Please sign in to comment.