From d08503129e71366eca798bd06e38b3191b6dcd27 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 15 Jan 2022 14:00:10 +0000 Subject: [PATCH] obj.py: move TwLabel and TwMMGenID to tw.py --- mmgen/addrdata.py | 2 +- mmgen/main_addrimport.py | 2 +- mmgen/obj.py | 48 -------------------------------------- mmgen/tw.py | 49 ++++++++++++++++++++++++++++++++++++++- test/test_py_d/ts_main.py | 2 +- 5 files changed, 51 insertions(+), 52 deletions(-) diff --git a/mmgen/addrdata.py b/mmgen/addrdata.py index a22f3b7f..91bdfa1e 100755 --- a/mmgen/addrdata.py +++ b/mmgen/addrdata.py @@ -84,7 +84,7 @@ class TwAddrData(AddrData,metaclass=AsyncInit): async def __init__(self,proto,wallet=None): from .rpc import rpc_init - from .obj import TwLabel + from .tw import TwLabel from .globalvars import g from .seed import SeedID self.proto = proto diff --git a/mmgen/main_addrimport.py b/mmgen/main_addrimport.py index 85817bf7..17a6c3d3 100755 --- a/mmgen/main_addrimport.py +++ b/mmgen/main_addrimport.py @@ -24,7 +24,7 @@ import time from .common import * from .addrlist import AddrList,KeyAddrList -from .obj import TwLabel +from .tw import TwLabel ai_msgs = lambda k: { 'rescan': """ diff --git a/mmgen/obj.py b/mmgen/obj.py index 0da0dadc..1a10ac79 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -299,54 +299,6 @@ class MMGenRange(tuple,InitErrors,MMGenObject): def items(self): return list(self.iterate()) -class TwMMGenID(str,Hilite,InitErrors,MMGenObject): - color = 'orange' - width = 0 - trunc_ok = False - def __new__(cls,proto,id_str): - if type(id_str) == cls: - return id_str - ret = None - from .addr import MMGenID - try: - ret = MMGenID(proto,id_str) - sort_key,idtype = ret.sort_key,'mmgen' - except Exception as e: - try: - assert id_str.split(':',1)[0] == proto.base_coin.lower(),( - f'not a string beginning with the prefix {proto.base_coin.lower()!r}:' ) - assert set(id_str[4:]) <= set(ascii_letters+digits),'contains non-alphanumeric characters' - assert len(id_str) > 4,'not more that four characters long' - ret,sort_key,idtype = str(id_str),'z_'+id_str,'non-mmgen' - except Exception as e2: - return cls.init_fail(e,id_str,e2=e2) - - me = str.__new__(cls,ret) - me.obj = ret - me.sort_key = sort_key - me.type = idtype - me.proto = proto - return me - -# non-displaying container for TwMMGenID,TwComment -class TwLabel(str,InitErrors,MMGenObject): - exc = BadTwLabel - passthru_excs = (BadTwComment,) - def __new__(cls,proto,text): - if type(text) == cls: - return text - try: - ts = text.split(None,1) - mmid = TwMMGenID(proto,ts[0]) - comment = TwComment(ts[1] if len(ts) == 2 else '') - me = str.__new__( cls, mmid + (' ' + comment if comment else '') ) - me.mmid = mmid - me.comment = comment - me.proto = proto - return me - except Exception as e: - return cls.init_fail(e,text) - class HexStr(str,Hilite,InitErrors): color = 'red' width = None diff --git a/mmgen/tw.py b/mmgen/tw.py index 8aeee13c..9f620a6b 100755 --- a/mmgen/tw.py +++ b/mmgen/tw.py @@ -47,10 +47,57 @@ from .util import ( ) from .base_obj import AsyncInit from .objmethods import Hilite,InitErrors,MMGenObject -from .obj import ImmutableAttr,ListItemAttr,MMGenListItem,MMGenList,MMGenDict,TwComment,get_obj,TwLabel,TwMMGenID +from .obj import ImmutableAttr,ListItemAttr,MMGenListItem,MMGenList,MMGenDict,TwComment,get_obj from .addr import CoinAddr,MMGenID,AddrIdx,is_mmgen_id,is_coin_addr from .rpc import rpc_init +class TwMMGenID(str,Hilite,InitErrors,MMGenObject): + color = 'orange' + width = 0 + trunc_ok = False + def __new__(cls,proto,id_str): + if type(id_str) == cls: + return id_str + ret = None + try: + ret = MMGenID(proto,id_str) + sort_key,idtype = ret.sort_key,'mmgen' + except Exception as e: + try: + assert id_str.split(':',1)[0] == proto.base_coin.lower(),( + f'not a string beginning with the prefix {proto.base_coin.lower()!r}:' ) + assert set(id_str[4:]) <= set(ascii_letters+digits),'contains non-alphanumeric characters' + assert len(id_str) > 4,'not more that four characters long' + ret,sort_key,idtype = str(id_str),'z_'+id_str,'non-mmgen' + except Exception as e2: + return cls.init_fail(e,id_str,e2=e2) + + me = str.__new__(cls,ret) + me.obj = ret + me.sort_key = sort_key + me.type = idtype + me.proto = proto + return me + +# non-displaying container for TwMMGenID,TwComment +class TwLabel(str,InitErrors,MMGenObject): + exc = BadTwLabel + passthru_excs = (BadTwComment,) + def __new__(cls,proto,text): + if type(text) == cls: + return text + try: + ts = text.split(None,1) + mmid = TwMMGenID(proto,ts[0]) + comment = TwComment(ts[1] if len(ts) == 2 else '') + me = str.__new__( cls, mmid + (' ' + comment if comment else '') ) + me.mmid = mmid + me.comment = comment + me.proto = proto + return me + except Exception as e: + return cls.init_fail(e,text) + def get_tw_label(proto,s): """ raise an exception on a malformed comment, return None on an empty or invalid label diff --git a/test/test_py_d/ts_main.py b/test/test_py_d/ts_main.py index 7968ce6a..6b87b1e3 100755 --- a/test/test_py_d/ts_main.py +++ b/test/test_py_d/ts_main.py @@ -477,7 +477,7 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared): cmd_args = self._make_txcreate_cmdline(tx_data) if cmdline_inputs: - from mmgen.tx import TwLabel + from mmgen.tw import TwLabel cmd_args = [ '--inputs={},{},{},{},{},{}'.format( TwLabel(self.proto,dfake[0][self.lbl_id]).mmid,dfake[1]['address'],