|
| 1 | +#!/bin/env python3 |
| 2 | +# |
| 3 | +# This script opens a markdown file containing the OpenMetrics specification, |
| 4 | +# extracts the ABNF grammar from it, and checks if the grammar is valid. |
| 5 | +# ABNF grammer must be enclosed in |
| 6 | +# ```abnf |
| 7 | +# exposition = metricset HASH SP eof [ LF ] |
| 8 | +# ... |
| 9 | +# ``` |
| 10 | +# code block, and the top node must be `exposition`. |
| 11 | +# It also extracts examples from the OpenMetrics spec file and checks if they |
| 12 | +# are valid according to the grammar. |
| 13 | +# Exampes must be enclosed in |
| 14 | +# ```openmetrics |
| 15 | +# ... example content ... |
| 16 | +# ``` |
| 17 | +# code blocks. |
| 18 | + |
| 19 | +from abnf import Rule |
| 20 | +import sys |
| 21 | + |
| 22 | +class Grammar(Rule): |
| 23 | + pass |
| 24 | + |
| 25 | +# Start node for the OpenMetrics spec. |
| 26 | +start_node = 'exposition' |
| 27 | + |
| 28 | +def get_spec(filename): |
| 29 | + with open(filename, 'r') as file: |
| 30 | + lines = file.readlines() |
| 31 | + spec = [] |
| 32 | + collecting = False |
| 33 | + for line in lines: |
| 34 | + if collecting: |
| 35 | + if line.startswith('```'): |
| 36 | + collecting = False |
| 37 | + else: |
| 38 | + spec.append(line.strip()) |
| 39 | + continue |
| 40 | + if line.startswith('```abnf'): |
| 41 | + if len(spec) > 0: |
| 42 | + raise ValueError("Multiple ABNF blocks found in the file.") |
| 43 | + collecting = True |
| 44 | + |
| 45 | + if len(spec) == 0: |
| 46 | + raise ValueError("No or empty ABNF block found in the file. Wanted ```abnf ... ```.") |
| 47 | + return '\n'.join(spec) |
| 48 | + |
| 49 | + |
| 50 | +class example: |
| 51 | + def __init__(self, line_number, content): |
| 52 | + self.line_number = line_number |
| 53 | + self.content = content |
| 54 | + |
| 55 | +class examples: |
| 56 | + """ |
| 57 | + Extracts examples from the OpenMetrics spec file with generator function. |
| 58 | + """ |
| 59 | + def __init__(self, filename): |
| 60 | + self.file = open(filename, 'r') |
| 61 | + self.line_number = 0 |
| 62 | + |
| 63 | + def __iter__(self): |
| 64 | + return self |
| 65 | + |
| 66 | + def __next__(self): |
| 67 | + collecting = False |
| 68 | + start_line = self.line_number |
| 69 | + example_lines = [] |
| 70 | + for line in self.file: |
| 71 | + self.line_number += 1 |
| 72 | + if collecting: |
| 73 | + if line.startswith('```'): |
| 74 | + collecting = False |
| 75 | + break |
| 76 | + else: |
| 77 | + example_lines.append(line) |
| 78 | + elif line.startswith('```openmetrics'): |
| 79 | + start_line = self.line_number |
| 80 | + collecting = True |
| 81 | + if len(example_lines) > 0: |
| 82 | + return example(start_line, ''.join(example_lines).strip()) |
| 83 | + |
| 84 | + raise StopIteration("No more examples found.") |
| 85 | + |
| 86 | +# Main |
| 87 | +if __name__ == "__main__": |
| 88 | + if len(sys.argv) != 2: |
| 89 | + print("Usage: python3 check_openmetrics_spec.py <filename.md>") |
| 90 | + sys.exit(1) |
| 91 | + |
| 92 | + filename = sys.argv[1] |
| 93 | + if not filename.endswith('.md'): |
| 94 | + print(f"Error: {filename} is not a Markdown file.") |
| 95 | + sys.exit(1) |
| 96 | + spec = get_spec(filename) |
| 97 | + print(spec) |
| 98 | + try: |
| 99 | + Grammar.load_grammar(grammar=spec, strict=True) |
| 100 | + except Exception as e: |
| 101 | + print(f"Error parsing ABNF: {e}") |
| 102 | + sys.exit(1) |
| 103 | + print("ABNF parsed successfully.") |
| 104 | + for ex in examples(filename): |
| 105 | + try: |
| 106 | + Grammar.get(start_node).parse_all(ex.content) |
| 107 | + print(f"Example parsed successfully: {ex.line_number}: {ex.content[:30]}...") # Print first 30 chars |
| 108 | + except Exception as e: |
| 109 | + print(f"Error parsing example at line {ex.line_number}: {e}\nExample: {ex.content[:30]}...") |
0 commit comments