Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions tools/extract-formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
import re
import fileinput

# We allow either ordered or unordered lists.
typeline = re.compile(
'1\. type: (?P<value>[-0-9A-Za-z_|]+) \(`(?P<name>[A-Za-z0-9_]+)`\)( \(`?(?P<option>[^)`]*)`\))?')
'(1\.|\*) type: (?P<value>[-0-9A-Za-z_|]+) \(`(?P<name>[A-Za-z0-9_]+)`\)( \(`?(?P<option>[^)`]*)`\))?')
tlvline = re.compile(
'1\. tlvs: `(?P<name>[A-Za-z0-9_]+)`( \(`?(?P<option>[^)`]*)`\))?')
'(1\.|\*) tlvs: `(?P<name>[A-Za-z0-9_]+)`( \(`?(?P<option>[^)`]*)`\))?')
subtypeline = re.compile(
'1\. subtype: `(?P<name>[A-Za-z0-9_]+)`( \(`?(?P<option>[^)`]*)`\))?')
'(1\.|\*) subtype: `(?P<name>[A-Za-z0-9_]+)`( \(`?(?P<option>[^)`]*)`\))?')
dataline = re.compile(
'\s+\* \[`(?P<typefield>[-_a-zA-Z0-9*+]+)`:`(?P<name>[_a-z0-9]+)`\]( \(`?(?P<option>[^)`]*)`?\))?')
'\s+([0-9]+\.|\*) \[`(?P<typefield>[-._a-zA-Z0-9*+]+)`:`(?P<name>[_a-z0-9]+)`\]( \(`?(?P<option>[^)`]*)`?\))?')
datastartline = re.compile(
'(2\.|\*) data:')
tlvtypesline = re.compile(
'(2\.|\*) types:')

# Generator to give us one line at a time.
def next_line(args, lines):
Expand Down Expand Up @@ -75,14 +80,14 @@ def print_csv(output, fmt, option):
# * [`chain_hash`:`chain_hash`]
# * [`u16`:`len`]
# * [`len*byte`:`encoded_short_ids`]
# * [`tlvs`:`query_short_channel_ids_tlvs`]
# * [`query_short_channel_ids_tlvs`:`tlvs`]
#
# output:
# msgtype,query_short_channel_ids,261,gossip_queries
# msgdata,query_short_channel_ids,chain_hash,chain_hash,
# msgdata,query_short_channel_ids,len,u16,
# msgdata,query_short_channel_ids,encoded_short_ids,byte,len
# msgdata,query_short_channel_ids,query_short_channel_ids_tlvs,tlvs,
# msgdata,query_short_channel_ids,tlvs,query_short_channel_ids_tlvs,
def parse_type(genline, output, name, value, option, in_tlv=None):
_, line = next(genline)

Expand All @@ -96,7 +101,7 @@ def parse_type(genline, output, name, value, option, in_tlv=None):
print_csv(output, '{},{},{}'.format(type_prefix, name, value), option)

# Expect a data: line before values, if any
if line.lstrip() != '2. data:':
if not datastartline.fullmatch(line.lstrip()):
return _, line

while True:
Expand All @@ -121,17 +126,17 @@ def parse_type(genline, output, name, value, option, in_tlv=None):
# 1. type: 1 (`query_flags`)
# 2. data:
# * [`byte`:`encoding_type`]
# * [`tlv_len-1*byte`:`encoded_query_flags`]
# * [`...*byte`:`encoded_query_flags`]
#
# output:
# tlvtype,query_short_channel_ids_tlvs,query_flags,1
# tlvdata,query_short_channel_ids_tlvs,query_flags,encoding_type,byte,
# tlvdata,query_short_channel_ids_tlvs,query_flags,encoded_query_flags,byte,tlv_len-1
# tlvdata,query_short_channel_ids_tlvs,query_flags,encoded_query_flags,byte,...
def parse_tlv(genline, output, name, option):
i, line = next(genline)

# Expect a types: line after tlvs.
if line != '2. types:':
if not tlvtypesline.fullmatch(line):
raise ValueError('{}: Expected "2. types:" line'.format(i))

_, line = next(genline)
Expand All @@ -158,7 +163,7 @@ def parse_subtype(genline, output, name, option):
i, line = next(genline)

# Expect a data: line after subtype.
if line != '2. data:':
if not datastartline.fullmatch(line):
raise ValueError('{}: Expected "2. data:" line'.format(i))

print_csv(output, 'subtype,{}'.format(name), option)
Expand Down