Browse Source

ImmutableAttr: remove support for type conversion from str

The MMGen Project 3 years ago
parent
commit
0dc3ef9a1f
5 changed files with 19 additions and 24 deletions
  1. 3 3
      mmgen/addrlist.py
  2. 6 12
      mmgen/obj.py
  3. 2 2
      mmgen/passwdlist.py
  4. 4 4
      mmgen/twuo.py
  5. 4 3
      mmgen/tx.py

+ 3 - 3
mmgen/addrlist.py

@@ -22,7 +22,7 @@ addrlist.py: Address list classes for the MMGen suite
 
 from .util import qmsg,qmsg_r,suf,make_chksum_N,Msg
 from .objmethods import MMGenObject,Hilite,InitErrors
-from .obj import MMGenListItem,ListItemAttr,MMGenDict,WalletPassword
+from .obj import MMGenListItem,ListItemAttr,MMGenDict,TwComment,WalletPassword
 from .key import PrivKey
 from .addr import MMGenID,MMGenAddrType,CoinAddr,AddrIdx,AddrListID,ViewKey
 
@@ -70,10 +70,10 @@ class AddrListEntryBase(MMGenListItem):
 class AddrListEntry(AddrListEntryBase):
 	addr          = ListItemAttr(CoinAddr,include_proto=True)
 	idx           = ListItemAttr(AddrIdx) # not present in flat addrlists
-	label         = ListItemAttr('TwComment',reassign_ok=True)
+	label         = ListItemAttr(TwComment,reassign_ok=True)
 	sec           = ListItemAttr(PrivKey,include_proto=True)
 	viewkey       = ListItemAttr(ViewKey,include_proto=True)
-	wallet_passwd = ListItemAttr('WalletPassword')
+	wallet_passwd = ListItemAttr(WalletPassword)
 
 class AddrListChksum(str,Hilite):
 	color = 'pink'

+ 6 - 12
mmgen/obj.py

@@ -94,7 +94,7 @@ class ImmutableAttr: # Descriptor
 	For attributes that are always present in the data instance
 	Reassignment and deletion forbidden
 	"""
-	ok_dtypes = (str,type,type(None),type(lambda:0))
+	ok_dtypes = (type,type(None),type(lambda:0))
 
 	def __init__(self,dtype,typeconv=True,set_none_ok=False,include_proto=False):
 		assert isinstance(dtype,self.ok_dtypes), 'ImmutableAttr_check1'
@@ -108,18 +108,12 @@ class ImmutableAttr: # Descriptor
 			self.conv = lambda instance,value: getattr(instance.conv_funcs,self.name)(instance,value)
 		elif typeconv:
 			"convert this attribute's type"
-			if type(dtype) == str:
-				if include_proto:
-					self.conv = lambda instance,value: globals()[dtype](instance.proto,value)
-				else:
-					self.conv = lambda instance,value: globals()[dtype](value)
+			if set_none_ok:
+				self.conv = lambda instance,value: None if value is None else dtype(value)
+			elif include_proto:
+				self.conv = lambda instance,value: dtype(instance.proto,value)
 			else:
-				if set_none_ok:
-					self.conv = lambda instance,value: None if value is None else dtype(value)
-				elif include_proto:
-					self.conv = lambda instance,value: dtype(instance.proto,value)
-				else:
-					self.conv = lambda instance,value: dtype(value)
+				self.conv = lambda instance,value: dtype(value)
 		else:
 			"check this attribute's type"
 			def assign_with_check(instance,value):

+ 2 - 2
mmgen/passwdlist.py

@@ -23,7 +23,7 @@ passwdlist.py: Password list class for the MMGen suite
 from collections import namedtuple
 
 from .util import ymsg,is_int,keypress_confirm
-from .obj import ImmutableAttr,ListItemAttr,MMGenPWIDString
+from .obj import ImmutableAttr,ListItemAttr,MMGenPWIDString,TwComment
 from .key import PrivKey
 from .addr import MMGenPasswordType,AddrIdx,AddrListID
 from .addrlist import (
@@ -37,7 +37,7 @@ from .addrlist import (
 class PasswordListEntry(AddrListEntryBase):
 	passwd = ListItemAttr(str,typeconv=False) # TODO: create Password type
 	idx    = ImmutableAttr(AddrIdx)
-	label  = ListItemAttr('TwComment',reassign_ok=True)
+	label  = ListItemAttr(TwComment,reassign_ok=True)
 	sec    = ListItemAttr(PrivKey,include_proto=True)
 
 class PasswordList(AddrList):

+ 4 - 4
mmgen/twuo.py

@@ -40,7 +40,7 @@ from .util import (
 )
 from .base_obj import AsyncInit
 from .objmethods import MMGenObject
-from .obj import ImmutableAttr,ListItemAttr,MMGenListItem,TwComment,get_obj
+from .obj import ImmutableAttr,ListItemAttr,MMGenListItem,TwComment,get_obj,HexStr,CoinTxID
 from .addr import CoinAddr,MMGenID,AddrIdx
 from .rpc import rpc_init
 from .tw import TwCommon,TwMMGenID,get_tw_label
@@ -75,16 +75,16 @@ Actions: [q]uit view, [p]rint to file, pager [v]iew, [w]ide view, add [l]abel:
 	class MMGenTwOutputList(list,MMGenObject): pass
 
 	class MMGenTwUnspentOutput(MMGenListItem):
-		txid         = ListItemAttr('CoinTxID')
+		txid         = ListItemAttr(CoinTxID)
 		vout         = ListItemAttr(int,typeconv=False)
 		amt          = ImmutableAttr(None)
 		amt2         = ListItemAttr(None)
-		label        = ListItemAttr('TwComment',reassign_ok=True)
+		label        = ListItemAttr(TwComment,reassign_ok=True)
 		twmmid       = ImmutableAttr(TwMMGenID,include_proto=True)
 		addr         = ImmutableAttr(CoinAddr,include_proto=True)
 		confs        = ImmutableAttr(int,typeconv=False)
 		date         = ListItemAttr(int,typeconv=False,reassign_ok=True)
-		scriptPubKey = ImmutableAttr('HexStr')
+		scriptPubKey = ImmutableAttr(HexStr)
 		skip         = ListItemAttr(str,typeconv=False,reassign_ok=True)
 
 		# required by gen_unspent(); setting valid_attrs explicitly is also more efficient

+ 4 - 3
mmgen/tx.py

@@ -56,6 +56,7 @@ from .obj import (
 	HexStr,
 	MMGenTxID,
 	MMGenDict,
+	TwComment,
 	CoinTxID,
 	get_obj,
 )
@@ -246,11 +247,11 @@ class DeserializedTX(dict,MMGenObject):
 class MMGenTxIO(MMGenListItem):
 	vout     = ListItemAttr(int,typeconv=False)
 	amt      = ImmutableAttr(None)
-	label    = ListItemAttr('TwComment',reassign_ok=True)
+	label    = ListItemAttr(TwComment,reassign_ok=True)
 	mmid     = ListItemAttr(MMGenID,include_proto=True)
 	addr     = ImmutableAttr(CoinAddr,include_proto=True)
 	confs    = ListItemAttr(int) # confs of type long exist in the wild, so convert
-	txid     = ListItemAttr('CoinTxID')
+	txid     = ListItemAttr(CoinTxID)
 	have_wif = ListItemAttr(bool,typeconv=False,delete_ok=True)
 
 	invalid_attrs = {'proto','tw_copy_attrs'}
@@ -277,7 +278,7 @@ class MMGenTxIO(MMGenListItem):
 			return self.proto.coin_amt(value)
 
 class MMGenTxInput(MMGenTxIO):
-	scriptPubKey = ListItemAttr('HexStr')
+	scriptPubKey = ListItemAttr(HexStr)
 	sequence     = ListItemAttr(int,typeconv=False)
 	tw_copy_attrs = { 'scriptPubKey','vout','amt','label','mmid','addr','confs','txid' }