Completed: implement ext_to_type(), use in initializer

This commit is contained in:
The MMGen Project 2022-02-05 13:32:53 +00:00
commit d7ebc45915
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 25 additions and 15 deletions

View file

@ -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):

View file

@ -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

View file

@ -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