WireProto/docs/data/parse.py

68 lines
2.1 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
2024-07-09 18:30:10 -04:00
import os
import pathlib
2024-07-07 23:33:21 -04:00
import re
2024-07-09 18:30:10 -04:00
import sys
2024-07-07 23:33:21 -04:00
import zlib
prefixes = ('request', 'response')
suffixes = ('simple', 'multi')
linecharlimit = 80
linestrp = re.compile(r'^\s*(?P<hex>[A-Fa-f0-9N]+)?(?:\s*//.*)?$')
thisdir = pathlib.Path(__file__).absolute().parent
2024-07-07 23:33:21 -04:00
2024-07-09 18:30:10 -04:00
is_tty = os.isatty(sys.stdout.fileno())
2024-07-07 23:33:21 -04:00
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(thisdir.joinpath(fname), 'r') as fh:
2024-07-07 23:33:21 -04:00
raw = fh.read().strip()
hexstr, bytelen = parse(raw)
with open(thisdir.joinpath('{0}.hex'.format(fnamebase)), 'w') as fh:
2024-07-07 23:33:21 -04:00
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])
2024-07-09 18:30:10 -04:00
if is_tty:
print(fnamebase)
for i in repr_split:
print(', '.join(i), end = ',\n')