shared.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. tw.shared: classes and functions shared by all tracking wallet classes
  12. """
  13. from ..objmethods import HiliteStr,InitErrors,MMGenObject
  14. from ..obj import TwComment
  15. from ..addr import MMGenID
  16. class TwMMGenID(HiliteStr,InitErrors,MMGenObject):
  17. color = 'orange'
  18. width = 0
  19. trunc_ok = False
  20. def __new__(cls,proto,id_str):
  21. if isinstance(id_str,cls):
  22. return id_str
  23. try:
  24. ret = addr = disp = MMGenID(proto,id_str)
  25. sort_key,idtype = (ret.sort_key,'mmgen')
  26. except Exception as e:
  27. try:
  28. coin,addr = id_str.split(':',1)
  29. assert coin == proto.base_coin.lower(),(
  30. f'not a string beginning with the prefix {proto.base_coin.lower()!r}:' )
  31. ret,sort_key,idtype,disp = (id_str,'z_'+id_str,'non-mmgen','non-MMGen')
  32. addr = proto.coin_addr(addr)
  33. except Exception as e2:
  34. return cls.init_fail(e,id_str,e2=e2)
  35. me = str.__new__(cls,ret)
  36. me.obj = ret
  37. me.disp = disp
  38. me.addr = addr
  39. me.sort_key = sort_key
  40. me.type = idtype
  41. me.proto = proto
  42. return me
  43. def fmt(self,**kwargs):
  44. return super().fmtc(self.disp,**kwargs)
  45. # non-displaying container for TwMMGenID,TwComment
  46. class TwLabel(str,InitErrors,MMGenObject):
  47. exc = 'BadTwLabel'
  48. passthru_excs = ('BadTwComment',)
  49. def __new__(cls,proto,text):
  50. if isinstance(text,cls):
  51. return text
  52. try:
  53. ts = text.split(None,1)
  54. mmid = TwMMGenID(proto,ts[0])
  55. comment = TwComment(ts[1] if len(ts) == 2 else '')
  56. me = str.__new__( cls, mmid + (' ' + comment if comment else '') )
  57. me.mmid = mmid
  58. me.comment = comment
  59. me.proto = proto
  60. return me
  61. except Exception as e:
  62. return cls.init_fail(e,text)
  63. def get_tw_label(proto,s):
  64. """
  65. raise an exception on a malformed comment, return None on an empty or invalid label
  66. """
  67. try:
  68. return TwLabel(proto,s)
  69. except Exception as e:
  70. if type(e).__name__ == 'BadTwComment': # do it this way to avoid importing .exception
  71. raise
  72. else:
  73. return None