WireProto/docs/data/parse.py

61 lines
2.0 KiB
Python
Raw Normal View History

2024-07-07 23:33:21 -04:00
#!/usr/bin/env python3
################################################################################################################################
## WireProto Specification © 2024 by Brent Saner is licensed under Creative Commons Attribution-ShareAlike 4.0 International. ##
## To view a copy of this license, visit https://creativecommons.org/licenses/by-sa/4.0/ ##
################################################################################################################################
import binascii
import re
import zlib
prefixes = ('request', 'response')
suffixes = ('simple', 'multi')
linecharlimit = 80
linestrp = re.compile(r'^\s*(?P<hex>[A-Fa-f0-9N]+)?(?:\s*//.*)?$')
def parse(text):
ret = []
bytelen = 0
for line in text.splitlines():
m = linestrp.search(line)
if not m:
continue
if not m.group('hex'):
continue
ret.append(m.group('hex'))
raw = ''.join(ret)
bytelen = int(len(raw) / 2)
ret = []
block = ''
for idx, c in enumerate(raw):
block = block + c
if len(block) == linecharlimit:
ret.append(block)
block = ''
if block:
ret.append(block)
return(('\n'.join(ret), bytelen))
for p in prefixes:
for s in suffixes:
fnamebase = '{0}.{1}'.format(p, s)
fname = '{0}.txt'.format(fnamebase)
with open(fname, 'r') as fh:
raw = fh.read().strip()
hexstr, bytelen = parse(raw)
with open('{0}.hex'.format(fnamebase), 'w') as fh:
fh.write(hexstr)
fh.write('\n')
b = binascii.unhexlify(hexstr.replace('\n', '').strip().encode('utf-8'))
repr = ['0x{0}'.format(format(i, '02x')) for i in b]
repr_split = []
for i in range(0, len(repr), 16):
repr_split.append(repr[i:i + 16])
print(fnamebase)
for i in repr_split:
print(', '.join(i), end = ',\n')