chainlib

Generic blockchain access library and tooling
Info | Log | Files | Refs | README | LICENSE

chainlib-flags.py (1337B)


      1 # standard imports
      2 import argparse
      3 import sys
      4 import math
      5 
      6 # local imports
      7 from chainlib.cli.arg import ArgFlag
      8 
      9 
     10 def value_int(v):
     11     return str(v)
     12 
     13 def value_hex(v):
     14     sz = math.log(v, 2)
     15     if v == 0:
     16         return "00"
     17     sz = math.floor(sz)
     18     sz = (sz / 8) + 1
     19     return '0x' + v.to_bytes(int(sz), byteorder='big').hex()
     20 
     21 argflag = ArgFlag()
     22 
     23 argparser = argparse.ArgumentParser()
     24 argparser.add_argument('-l', '--list', action='store_true', help='List all flag names and values')
     25 argparser.add_argument('-x', '--hex', action='store_true', help='Output all values in hex')
     26 argparser.add_argument('-0', dest='nonl', action='store_true', help='Omit newline in single flag output')
     27 argparser.add_argument('flag', type=str, nargs='*', help='One or more flag name to calculate flag value for')
     28 args = argparser.parse_args(sys.argv[1:])
     29 
     30 m = value_int
     31 if args.hex:
     32     m = value_hex
     33 
     34 if args.list:
     35     o = dict(argflag)
     36     for k in o.keys():
     37         r = k.split("=")
     38         v = m(o[k])
     39         if len(r) == 1:
     40             print(k + ' ' + v)
     41         else:
     42             print(r[0] + ' ' + v + ' (' + r[1] + ')')
     43     sys.exit(0)
     44 
     45 if len(args.flag) == 0:
     46     sys.stdout.write(m(argflag.all))
     47 else:
     48     r = 0
     49     for k in args.flag:
     50         r += argflag.val(k)
     51     sys.stdout.write(m(r))
     52 
     53 if not args.nonl:
     54     sys.stdout.write("\n")