Skip to content

Instantly share code, notes, and snippets.

Created November 5, 2013 16:53
Show Gist options
  • Save igor47/7322131 to your computer and use it in GitHub Desktop.
Save igor47/7322131 to your computer and use it in GitHub Desktop.
The haproxy configuration manual contains a matrix listing configuration keywords and their applicability by haproxy section (defaults, listen, backend, frontend). This code parses the manual and produces a json hash of sections and the keywords that apply in those sections.
#!/usr/bin/env python
from collections import defaultdict
import json
import requests
config_url = ""
longest_keyword = 42 #works for haproxy 1.5 docs, but might need to be adjusted
# first, get the lines that pertain to the matrix
matrix_lines = []
in_matrix = False
configuration = requests.get(config_url)
for line in configuration.text.split('\n'):
if line.split() == ['keyword', 'defaults', 'frontend', 'listen', 'backend']:
in_matrix = False if in_matrix else True
if in_matrix:
# now parse the lines to figure out keyword/section applicability
fields = defaultdict(lambda: [])
allowed = {}
for l in matrix_lines:
if l.startswith('-') or l.startswith(' '):
names, boxes = l[0:longest_keyword], l[longest_keyword:]
name = names.split('(')[0].strip()
allowed['defaults'], allowed['frontend'], allowed['listen'], allowed['backend'] = [
p == 'X' for p in boxes.split()]
for section in allowed:
if allowed[section]:
print json.dumps(fields)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment