#! /usr/bin/python3 # Simple script to parse specs and produce CSV files. # Released by Rusty Russell under CC0: # https://creativecommons.org/publicdomain/zero/1.0/ from optparse import OptionParser import sys import re import fileinput # Figure out if we can determine type from size. def guess_alignment(message, name, sizestr): # Exceptions: # - Padding has no alignment requirements. # - channel-id is size 8, but has alignment 4. # - node_announcement.ipv6 has size 16, but alignment 4 (to align IPv4). # - node_announcement.alias is a string, so alignment 1 # - signatures have no alignment requirement. if name.startswith('pad'): return 1 if name == 'channel-id': return 4 if message == 'node_announcement' and name == 'ipv6': return 4 if message == 'node_announcement' and name == 'alias': return 1 if 'signature' in name: return 1 # Size can be variable. try: size = int(sizestr) except ValueError: # If it contains a "*xxx" factor, that's our per-unit size. s = re.search('\*([0-9]*)$', sizestr) if s is None: size = 1 else: size = int(s.group(1)) if size % 8 == 0: return 8 elif size % 4 == 0: return 4 elif size % 2 == 0: return 2 return 1 def main(options, args=None, output=sys.stdout, lines=None): # Example inputs: # 1. type: 17 (`error`) # 2. data: # * [`8`:`channel_id`] # * [`4`:`len`] # * [`len`:`data`] (optionXXX) # # 1. type: PERM|NODE|3 (`required_node_feature_missing`) message = None havedata = None typeline = re.compile( '1\. type: (?P[-0-9A-Za-z_|]+) \(`(?P[A-Za-z_]+)`\)') dataline = re.compile( '\s+\* \[`(?P[_a-z0-9*+]+)`:`(?P[_a-z0-9]+)`\]( \(`?(?P