From 2fa3d97c5445cc33185c623c2701e695c006211a Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 13 Oct 2023 09:51:12 +0000 Subject: [PATCH] pylint integration: class Hilite -> Hilite, HiliteStr --- mmgen/addr.py | 10 +++++----- mmgen/addrlist.py | 6 +++--- mmgen/key.py | 6 +++--- mmgen/msg.py | 4 ++-- mmgen/obj.py | 8 ++++---- mmgen/objmethods.py | 42 ++++++++++++++++++++++-------------------- mmgen/rpc.py | 4 ++-- mmgen/seed.py | 4 ++-- mmgen/seedsplit.py | 4 ++-- mmgen/subseed.py | 4 ++-- mmgen/tw/shared.py | 4 ++-- mmgen/xmrwallet.py | 4 ++-- 12 files changed, 51 insertions(+), 49 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index 1d498713..0d0eb4e7 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -22,14 +22,14 @@ addr: MMGen address-related types from collections import namedtuple -from .objmethods import Hilite,InitErrors,MMGenObject +from .objmethods import HiliteStr,InitErrors,MMGenObject from .obj import ImmutableAttr,MMGenIdx,get_obj from .seed import SeedID ati = namedtuple('addrtype_info', ['name','pubkey_type','compressed','gen_method','addr_fmt','wif_label','extra_attrs','desc']) -class MMGenAddrType(str,Hilite,InitErrors,MMGenObject): +class MMGenAddrType(HiliteStr,InitErrors,MMGenObject): width = 1 trunc_ok = False color = 'blue' @@ -93,7 +93,7 @@ class AddrIdx(MMGenIdx): def is_addr_idx(s): return get_obj( AddrIdx, n=s, silent=True, return_bool=True ) -class AddrListID(str,Hilite,InitErrors,MMGenObject): +class AddrListID(HiliteStr,InitErrors,MMGenObject): width = 10 trunc_ok = False color = 'yellow' @@ -120,7 +120,7 @@ class AddrListID(str,Hilite,InitErrors,MMGenObject): def is_addrlist_id(proto,s): return get_obj( AddrListID, proto=proto, id_str=s, silent=False, return_bool=True ) -class MMGenID(str,Hilite,InitErrors,MMGenObject): +class MMGenID(HiliteStr,InitErrors,MMGenObject): color = 'orange' width = 0 trunc_ok = False @@ -144,7 +144,7 @@ class MMGenID(str,Hilite,InitErrors,MMGenObject): def is_mmgen_id(proto,s): return get_obj( MMGenID, proto=proto, id_str=s, silent=True, return_bool=True ) -class CoinAddr(str,Hilite,InitErrors,MMGenObject): +class CoinAddr(HiliteStr,InitErrors,MMGenObject): color = 'cyan' hex_width = 40 width = 1 diff --git a/mmgen/addrlist.py b/mmgen/addrlist.py index 30a4a8f7..eaced94e 100755 --- a/mmgen/addrlist.py +++ b/mmgen/addrlist.py @@ -21,7 +21,7 @@ addrlist: Address list classes for the MMGen suite """ from .util import suf,make_chksum_N,Msg,die -from .objmethods import MMGenObject,Hilite,InitErrors +from .objmethods import MMGenObject,HiliteStr,InitErrors from .obj import MMGenListItem,ListItemAttr,MMGenDict,TwComment,WalletPassword from .key import PrivKey from .addr import MMGenID,MMGenAddrType,CoinAddr,AddrIdx,AddrListID,ViewKey @@ -88,7 +88,7 @@ class AddrListEntry(AddrListEntryBase): viewkey = ListItemAttr(ViewKey,include_proto=True) wallet_passwd = ListItemAttr(WalletPassword) -class AddrListChksum(str,Hilite): +class AddrListChksum(HiliteStr): color = 'pink' trunc_ok = False @@ -100,7 +100,7 @@ class AddrListChksum(str,Hilite): ) for e in addrlist.data] return str.__new__(cls,make_chksum_N(' '.join(lines), nchars=16, sep=True)) -class AddrListIDStr(str,Hilite): +class AddrListIDStr(HiliteStr): color = 'green' trunc_ok = False diff --git a/mmgen/key.py b/mmgen/key.py index 2fe0753e..1390a3fb 100755 --- a/mmgen/key.py +++ b/mmgen/key.py @@ -20,10 +20,10 @@ key: MMGen public and private key objects """ -from .objmethods import Hilite,InitErrors,MMGenObject +from .objmethods import HiliteStr,InitErrors,MMGenObject from .obj import ImmutableAttr,get_obj -class WifKey(str,Hilite,InitErrors): +class WifKey(HiliteStr,InitErrors): """ Initialize a WIF key, checking its well-formedness. The numeric validity of the private key it encodes is not checked. @@ -54,7 +54,7 @@ class PubKey(bytes,InitErrors,MMGenObject): # TODO: add some real checks except Exception as e: return cls.init_fail(e,s) -class PrivKey(bytes,Hilite,InitErrors,MMGenObject): +class PrivKey(bytes,InitErrors,MMGenObject): """ Input: a) raw, non-preprocessed bytes; or b) WIF key. Output: preprocessed key bytes, plus WIF key in 'wif' attribute diff --git a/mmgen/msg.py b/mmgen/msg.py index b0caac30..08f12529 100755 --- a/mmgen/msg.py +++ b/mmgen/msg.py @@ -14,14 +14,14 @@ msg: base message signing classes import os,importlib,json from .cfg import gc -from .objmethods import MMGenObject,Hilite,InitErrors +from .objmethods import MMGenObject,HiliteStr,InitErrors from .util import msg,die,make_chksum_6,fmt_list,remove_dups from .color import red,orange,grnbg from .protocol import init_proto from .fileutil import get_data_from_file,write_data_to_file from .addr import MMGenID,CoinAddr -class MMGenIDRange(str,Hilite,InitErrors,MMGenObject): +class MMGenIDRange(HiliteStr,InitErrors,MMGenObject): """ closely based on MMGenID """ diff --git a/mmgen/obj.py b/mmgen/obj.py index 41fa4fca..5f6d0771 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -22,7 +22,7 @@ obj: MMGen native classes import unicodedata -from .objmethods import MMGenObject,Hilite,InitErrors +from .objmethods import MMGenObject,Hilite,HiliteStr,InitErrors def get_obj(objname,*args,**kwargs): """ @@ -303,10 +303,10 @@ class MMGenIdx(Int): class ETHNonce(Int): min_val = 0 -class Str(str,Hilite): +class Str(HiliteStr): pass -class HexStr(str,Hilite,InitErrors): +class HexStr(HiliteStr,InitErrors): color = 'red' width = None hexcase = 'lower' @@ -343,7 +343,7 @@ class WalletPassword(HexStr): class MMGenTxID(HexStr): color,width,hexcase = ('red',6,'upper') -class MMGenLabel(str,Hilite,InitErrors): +class MMGenLabel(HiliteStr,InitErrors): color = 'pink' allowed = [] forbidden = [] diff --git a/mmgen/objmethods.py b/mmgen/objmethods.py index 78e91447..60688a32 100755 --- a/mmgen/objmethods.py +++ b/mmgen/objmethods.py @@ -48,14 +48,6 @@ class Hilite: width = 0 trunc_ok = True - # supports single-width characters only - def fmt( self, width, color=False ): - if len(self) > width: - assert self.trunc_ok, "If 'trunc_ok' is false, 'width' must be >= width of string" - return self.colorize( self[:width].ljust(width), color=color ) - else: - return self.colorize( self.ljust(width), color=color ) - # class method equivalent of fmt() @classmethod def fmtc( cls, s, width, color=False ): @@ -65,6 +57,28 @@ class Hilite: else: return cls.colorize( s.ljust(width), color=color ) + @classmethod + def hlc(cls,s,color=True): + return getattr( color_mod, cls.color )(s) if color else s + + @classmethod + def colorize(cls,s,color=True): + return getattr( color_mod, cls.color )(s) if color else s + + @classmethod + def colorize2(cls,s,color=True,color_override=''): + return getattr( color_mod, color_override or cls.color )(s) if color else s + +class HiliteStr(str,Hilite): + + # supports single-width characters only + def fmt( self, width, color=False ): + if len(self) > width: + assert self.trunc_ok, "If 'trunc_ok' is false, 'width' must be >= width of string" + return self.colorize( self[:width].ljust(width), color=color ) + else: + return self.colorize( self.ljust(width), color=color ) + # an alternative to fmt(), with double-width char support and other features def fmt2( self, @@ -99,21 +113,9 @@ class Hilite: else: return self.colorize2( s.ljust(width-s_wide_count), color=color, color_override=color_override ) - @classmethod - def colorize(cls,s,color=True): - return getattr( color_mod, cls.color )(s) if color else s - - @classmethod - def colorize2(cls,s,color=True,color_override=''): - return getattr( color_mod, color_override or cls.color )(s) if color else s - def hl(self,color=True): return getattr( color_mod, self.color )(self) if color else self - @classmethod - def hlc(cls,s,color=True): - return getattr( color_mod, cls.color )(s) if color else s - # an alternative to hl(), with enclosure and color override # can be called as an unbound method with class as first argument def hl2(self,s=None,color=True,encl='',color_override=''): diff --git a/mmgen/rpc.py b/mmgen/rpc.py index a703275a..7bdd808f 100755 --- a/mmgen/rpc.py +++ b/mmgen/rpc.py @@ -27,7 +27,7 @@ from collections import namedtuple from .util import msg,die,fmt,fmt_list,pp_fmt,oneshot_warning from .base_obj import AsyncInit from .obj import NonNegativeInt -from .objmethods import Hilite,InitErrors,MMGenObject +from .objmethods import HiliteStr,InitErrors,MMGenObject auth_data = namedtuple('rpc_auth_data',['user','passwd']) @@ -46,7 +46,7 @@ def dmsg_rpc_backend(host_url,host_path,payload): def noop(*args,**kwargs): pass -class IPPort(str,Hilite,InitErrors): +class IPPort(HiliteStr,InitErrors): color = 'yellow' width = 0 trunc_ok = False diff --git a/mmgen/seed.py b/mmgen/seed.py index 7b44e47c..db896882 100755 --- a/mmgen/seed.py +++ b/mmgen/seed.py @@ -21,10 +21,10 @@ seed: Seed-related classes and methods for the MMGen suite """ from .util import make_chksum_8,hexdigits_uc,die -from .objmethods import Hilite,InitErrors,MMGenObject +from .objmethods import HiliteStr,InitErrors,MMGenObject from .obj import ImmutableAttr,get_obj -class SeedID(str,Hilite,InitErrors): +class SeedID(HiliteStr,InitErrors): color = 'blue' width = 8 trunc_ok = False diff --git a/mmgen/seedsplit.py b/mmgen/seedsplit.py index c1629937..6b1ec4c6 100755 --- a/mmgen/seedsplit.py +++ b/mmgen/seedsplit.py @@ -22,7 +22,7 @@ seedsplit: Seed split classes and methods for the MMGen suite from .color import yellow from .util import msg,die -from .objmethods import MMGenObject,Hilite,InitErrors +from .objmethods import MMGenObject,HiliteStr,InitErrors from .obj import ImmutableAttr,MMGenPWIDString,MMGenIdx,get_obj,IndexedDict from .seed import Seed,SeedBase from .subseed import SubSeedList,SubSeedIdx,SubSeed @@ -37,7 +37,7 @@ class SeedShareCount(SeedShareIdx): class MasterShareIdx(MMGenIdx): max_val = 1024 -class SeedSplitSpecifier(str,Hilite,InitErrors,MMGenObject): +class SeedSplitSpecifier(HiliteStr,InitErrors,MMGenObject): color = 'red' def __new__(cls,s): if isinstance(s,cls): diff --git a/mmgen/subseed.py b/mmgen/subseed.py index 239b26ce..dbfbcc7e 100755 --- a/mmgen/subseed.py +++ b/mmgen/subseed.py @@ -22,7 +22,7 @@ subseed: Subseed classes and methods for the MMGen suite from .color import green from .util import msg_r,msg,die,make_chksum_8 -from .objmethods import MMGenObject,Hilite,InitErrors +from .objmethods import MMGenObject,HiliteStr,InitErrors from .obj import MMGenRange,IndexedDict,ImmutableAttr from .seed import SeedBase,SeedID @@ -30,7 +30,7 @@ class SubSeedIdxRange(MMGenRange): min_idx = 1 max_idx = 1000000 -class SubSeedIdx(str,Hilite,InitErrors): +class SubSeedIdx(HiliteStr,InitErrors): color = 'red' trunc_ok = False def __new__(cls,s): diff --git a/mmgen/tw/shared.py b/mmgen/tw/shared.py index 7c3e7c11..09916925 100755 --- a/mmgen/tw/shared.py +++ b/mmgen/tw/shared.py @@ -12,11 +12,11 @@ tw.shared: classes and functions shared by all tracking wallet classes """ -from ..objmethods import Hilite,InitErrors,MMGenObject +from ..objmethods import HiliteStr,InitErrors,MMGenObject from ..obj import TwComment from ..addr import MMGenID -class TwMMGenID(str,Hilite,InitErrors,MMGenObject): +class TwMMGenID(HiliteStr,InitErrors,MMGenObject): color = 'orange' width = 0 trunc_ok = False diff --git a/mmgen/xmrwallet.py b/mmgen/xmrwallet.py index bf6d78f5..06a80cd6 100755 --- a/mmgen/xmrwallet.py +++ b/mmgen/xmrwallet.py @@ -24,7 +24,7 @@ import re,time,json,atexit from collections import namedtuple from pathlib import Path -from .objmethods import MMGenObject,Hilite,InitErrors +from .objmethods import MMGenObject,HiliteStr,InitErrors from .obj import CoinTxID from .color import red,yellow,green,blue,cyan,pink,orange,purple from .util import ( @@ -76,7 +76,7 @@ xmrwallet_uarg_info = ( r'(?:[^:]+):(?:\d+)' ) -class XMRWalletAddrSpec(str,Hilite,InitErrors,MMGenObject): +class XMRWalletAddrSpec(HiliteStr,InitErrors,MMGenObject): color = 'cyan' width = 0 trunc_ok = False