Browse Source

Completed: implement ext_to_type(), use in initializer

The MMGen Project 3 years ago
parent
commit
d7ebc45915
3 changed files with 25 additions and 15 deletions
  1. 1 1
      mmgen/tool/file.py
  2. 8 14
      mmgen/tx/__init__.py
  3. 16 0
      mmgen/tx/completed.py

+ 1 - 1
mmgen/tool/file.py

@@ -92,7 +92,7 @@ class tool_cmd(tool_cmd_base):
 
 		from ..filename import MMGenFileList
 		from ..tx import completed,CompletedTX
-		flist = MMGenFileList( infiles, ftype=completed.Completed )
+		flist = MMGenFileList( infiles, base_class=completed.Completed, proto=self.proto )
 		flist.sort_by_age( key=file_sort ) # in-place sort
 
 		async def process_file(f):

+ 8 - 14
mmgen/tx/__init__.py

@@ -41,20 +41,14 @@ def _get_cls_info(clsname,modname,args,kwargs):
 			f"{clsname} must be instantiated with 'proto','data' or 'filename' keyword" )
 
 	if clsname == 'Completed':
-		from ..util import get_extension,fmt_list
-		from .unsigned import Unsigned
-		from .signed import Signed
-
-		ext = get_extension(kwargs['filename'])
-		cls_data = {
-			Unsigned.ext: ('Unsigned','unsigned'),
-			Signed.ext:   ('OnlineSigned','online') if proto.tokensym else ('Signed','signed')
-		}
-
-		if ext not in cls_data:
-			die(1,f'{ext!r}: unrecognized file extension for CompletedTX (not in {fmt_list(cls_data)})')
-
-		clsname,modname = cls_data[ext]
+		from ..util import get_extension
+		from .completed import Completed
+		ext = get_extension( kwargs['filename'] )
+		cls = Completed.ext_to_type( ext, proto )
+		if not cls:
+			die(1,f'{ext!r}: unrecognized file extension for CompletedTX')
+		clsname = cls.__name__
+		modname = cls.__module__.split('.')[-1]
 
 	kwargs['proto'] = proto
 

+ 16 - 0
mmgen/tx/completed.py

@@ -51,3 +51,19 @@ class Completed(Base):
 	def file(self):
 		from ..txfile import MMGenTxFile
 		return MMGenTxFile(self)
+
+	@classmethod
+	def ext_to_type(cls,ext,proto):
+		"""
+		see twctl:import_token()
+		"""
+		from .unsigned import Unsigned
+		if ext == Unsigned.ext:
+			return Unsigned
+
+		if proto.tokensym:
+			from .online import OnlineSigned as Signed
+		else:
+			from .signed import Signed
+		if ext == Signed.ext:
+			return Signed