move TwMMGenID, TwLabel, get_tw_label() to tw.shared

This commit is contained in:
The MMGen Project 2022-11-12 13:34:40 +00:00
commit a7b11f1d3b
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
16 changed files with 100 additions and 82 deletions

View file

@ -73,7 +73,7 @@ class TwAddrData(AddrData,metaclass=AsyncInit):
async def __init__(self,proto,wallet=None):
from .rpc import rpc_init
from .tw.view import TwLabel
from .tw.shared import TwLabel
from .globalvars import g
from .seed import SeedID
self.proto = proto

View file

@ -24,7 +24,7 @@ from collections import namedtuple
from .common import *
from .addrlist import AddrList,KeyAddrList
from .tw.view import TwLabel
from .tw.shared import TwLabel
opts_data = {
'text': {

View file

@ -13,10 +13,10 @@ proto.btc.tw.addresses: Bitcoin base protocol tracking wallet address list class
"""
from ....tw.addresses import TwAddresses
from ....tw.view import TwLabel,get_obj
from ....tw.shared import TwLabel
from ....util import msg,msg_r
from ....addr import CoinAddr
from ....obj import NonNegativeInt
from ....obj import NonNegativeInt,get_obj
from .common import BitcoinTwCommon
class BitcoinTwAddresses(TwAddresses,BitcoinTwCommon):

View file

@ -13,7 +13,7 @@ proto.btc.twbal: Bitcoin base protocol tracking wallet balance class
"""
from ....tw.bal import TwGetBalance
from ....tw.view import get_tw_label
from ....tw.shared import get_tw_label
class BitcoinTwGetBalance(TwGetBalance):

View file

@ -15,7 +15,7 @@ proto.btc.tw.common: Bitcoin base protocol tracking wallet dependency classes
from ....addr import CoinAddr
from ....util import die,msg,rmsg
from ....obj import MMGenList
from ....tw.view import get_tw_label
from ....tw.shared import get_tw_label
class BitcoinTwCommon:

View file

@ -14,7 +14,7 @@ proto.btc.tw.json: export and import tracking wallet to JSON format
from collections import namedtuple
from ....tw.json import TwJSON
from ....tw.view import TwMMGenID
from ....tw.shared import TwMMGenID
class BitcoinTwJSON(TwJSON):

View file

@ -15,7 +15,7 @@ proto.btc.tw.txhistory: Bitcoin base protocol tracking wallet transaction histor
from collections import namedtuple
from ....globalvars import g
from ....tw.txhistory import TwTxHistory
from ....tw.view import get_tw_label,TwMMGenID
from ....tw.shared import get_tw_label,TwMMGenID
from ....addr import CoinAddr
from ....util import msg,msg_r
from ....color import nocolor,red,pink,gray

View file

@ -15,7 +15,7 @@ from ....globalvars import g
from ....tw.ctl import TrackingWallet
from ....tw.view import TwView
from ....addr import CoinAddr
from ....tw.view import TwLabel
from ....tw.shared import TwLabel
class EthereumTwCommon(TwView):

View file

@ -14,7 +14,7 @@ proto.eth.tw.json: export and import tracking wallet to JSON format
from collections import namedtuple
from ....tw.json import TwJSON
from ....tw.view import TwMMGenID
from ....tw.shared import TwMMGenID
class EthereumTwJSON(TwJSON):

View file

@ -20,7 +20,7 @@
proto.eth.twuo: Ethereum tracking wallet unspent outputs class
"""
from ....tw.view import TwLabel
from ....tw.shared import TwLabel
from ....tw.unspent import TwUnspentOutputs
from .common import EthereumTwCommon

View file

@ -19,7 +19,8 @@ from ..obj import MMGenListItem,ImmutableAttr,ListItemAttr,TwComment,NonNegative
from ..rpc import rpc_init
from ..addr import CoinAddr,MMGenID
from ..color import red,green
from .view import TwView,TwMMGenID
from .view import TwView
from .shared import TwMMGenID
class TwAddresses(MMGenObject,TwView,metaclass=AsyncInit):

View file

@ -36,7 +36,7 @@ from ..objmethods import MMGenObject
from ..obj import TwComment,get_obj
from ..addr import CoinAddr,is_mmgen_id,is_coin_addr
from ..rpc import rpc_init
from .view import TwMMGenID,TwLabel
from .shared import TwMMGenID,TwLabel
addr_info = namedtuple('addr_info',['twmmid','coinaddr'])

82
mmgen/tw/shared.py Executable file
View file

@ -0,0 +1,82 @@
#!/usr/bin/env python3
#
# mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
# Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
# Licensed under the GNU General Public License, Version 3:
# https://www.gnu.org/licenses
# Public project repositories:
# https://github.com/mmgen/mmgen
# https://gitlab.com/mmgen/mmgen
"""
tw.shared: classes and functions shared by all tracking wallet classes
"""
from ..objmethods import Hilite,InitErrors,MMGenObject
from ..obj import TwComment
from ..addr import MMGenID
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
try:
ret = addr = disp = MMGenID(proto,id_str)
sort_key,idtype = (ret.sort_key,'mmgen')
except Exception as e:
try:
coin,addr = id_str.split(':',1)
assert coin == proto.base_coin.lower(),(
f'not a string beginning with the prefix {proto.base_coin.lower()!r}:' )
assert addr.isascii() and addr.isalnum(), 'not an ASCII alphanumeric string'
ret,sort_key,idtype,disp = (id_str,'z_'+id_str,'non-mmgen','non-MMGen')
addr = proto.coin_addr(addr)
except Exception as e2:
return cls.init_fail(e,id_str,e2=e2)
me = str.__new__(cls,ret)
me.obj = ret
me.disp = disp
me.addr = addr
me.sort_key = sort_key
me.type = idtype
me.proto = proto
return me
@classmethod
def fmtc(cls,twmmid,*args,**kwargs):
return super().fmtc(twmmid.disp,*args,**kwargs)
# 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
"""
try:
return TwLabel(proto,s)
except Exception as e:
if type(e).__name__ == 'BadTwComment': # do it this way to avoid importing .exception
raise
else:
return None

View file

@ -35,7 +35,8 @@ from ..obj import (
NonNegativeInt )
from ..addr import CoinAddr,MMGenID
from ..rpc import rpc_init
from .view import TwView,TwMMGenID,get_tw_label
from .view import TwView
from .shared import TwMMGenID,get_tw_label
class TwUnspentOutputs(MMGenObject,TwView,metaclass=AsyncInit):

View file

@ -25,10 +25,9 @@ from collections import namedtuple
from ..globalvars import g
from ..objmethods import Hilite,InitErrors,MMGenObject
from ..obj import TwComment,get_obj,MMGenIdx,MMGenList
from ..obj import get_obj,MMGenIdx,MMGenList
from ..color import nocolor,yellow,green,red,blue
from ..util import msg,msg_r,fmt,die,capfirst,make_timestr
from ..addr import MMGenID
# base class for TwUnspentOutputs,TwAddresses,TwTxHistory:
class TwView:
@ -506,68 +505,3 @@ class TwView:
return None
return await do_comment_add(res)
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
try:
ret = addr = disp = MMGenID(proto,id_str)
sort_key,idtype = (ret.sort_key,'mmgen')
except Exception as e:
try:
coin,addr = id_str.split(':',1)
assert coin == proto.base_coin.lower(),(
f'not a string beginning with the prefix {proto.base_coin.lower()!r}:' )
assert addr.isascii() and addr.isalnum(), 'not an ASCII alphanumeric string'
ret,sort_key,idtype,disp = (id_str,'z_'+id_str,'non-mmgen','non-MMGen')
addr = proto.coin_addr(addr)
except Exception as e2:
return cls.init_fail(e,id_str,e2=e2)
me = str.__new__(cls,ret)
me.obj = ret
me.disp = disp
me.addr = addr
me.sort_key = sort_key
me.type = idtype
me.proto = proto
return me
@classmethod
def fmtc(cls,twmmid,*args,**kwargs):
return super().fmtc(twmmid.disp,*args,**kwargs)
# 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
"""
try:
return TwLabel(proto,s)
except Exception as e:
if type(e).__name__ == 'BadTwComment': # do it this way to avoid importing .exception
raise
else:
return None

View file

@ -483,7 +483,7 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared):
cmd_args = self._make_txcreate_cmdline(tx_data)
if cmdline_inputs:
from mmgen.tw.view import TwLabel
from mmgen.tw.shared import TwLabel
cmd_args = [
'--inputs={},{},{},{},{},{}'.format(
TwLabel(self.proto,dfake[0][self.lbl_id]).mmid,dfake[1]['address'],