Skip to content

Instantly share code, notes, and snippets.

@sizhky
Created October 14, 2024 05:23
Show Gist options
  • Save sizhky/74d46f1f8d8c42961938b692938606b7 to your computer and use it in GitHub Desktop.
Save sizhky/74d46f1f8d8c42961938b692938606b7 to your computer and use it in GitHub Desktop.
Parse all chunks of bruno
from torch_snippets import json, readlines
import re
def parse_bruno(file):
def process_chunks(chunk):
a, *b = chunk.split('\n')
a, _b = a.split()
b = '\n'.join([_b, *b])
if a == 'body:json':
b = json.loads(b[1:-1])
return a, b
splitter = r'^[a-z].*\{$'
chunks = []
lines = readlines(file, _strip=False)
nlines = len(lines)
current_chunk = [0, -1]
for ix, line in enumerate(lines, start=1):
if ix == nlines:
current_chunk[1] = ix-1
chunks.append(current_chunk)
chunks = chunks[1:]
return dict(process_chunks('\n'.join(lines[a:b])) for a,b in chunks)
if re.match(splitter, line):
current_chunk[1] = ix-2
chunks.append(current_chunk)
current_chunk = [ix-1, -1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment