Created
January 24, 2017 19:44
-
-
Save dillonhicks/2ab3d3b65902f2da79da367036a98afe to your computer and use it in GitHub Desktop.
Flask Config Documentation to Protobuf Message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import BytesIO | |
import re | |
import functools | |
import requests | |
from requests.adapters import HTTPAdapter | |
import lxml.etree | |
class HTML: | |
"""Utilities for interacting with HTML documents""" | |
@staticmethod | |
def iter(content): | |
"""Generates each element in the DOM of the HTML one element at a | |
time. Will recover on failure. Unparsable documents become a | |
single paragraph element containing the text of the content. | |
""" | |
context = lxml.etree.iterparse(BytesIO(content), recover=True, html=True) | |
for action, elem in context: | |
yield elem | |
class HTTP: | |
_HTTP_TIMEOUT = 2.0 # seconds | |
"""HTTP constants and verb functions""" | |
_session = requests.Session() | |
_session.mount('http://', HTTPAdapter(max_retries=0)) | |
_session.mount('https://', HTTPAdapter(max_retries=0)) | |
#: The good status code for HTTP responses | |
ok = 200 | |
#: Asyncio friendly wrapper for requests.get with a timeout | |
get = functools.partial(_session.get, timeout=_HTTP_TIMEOUT) | |
class Tag: | |
"""Utilities for HTML Tags""" | |
@staticmethod | |
def is_(tag): | |
def matcher(element): | |
return element.tag == tag | |
return matcher | |
def attr_is(**kwargs): | |
assert len(kwargs) == 1 | |
key, value = next(iter(kwargs.items())) | |
def matcher(element): | |
return element.attrib.get(key) == str(value) | |
return matcher | |
is_span = Tag.is_('span') | |
is_class_pre = Tag.attr_is(**{'class': 'pre'}) | |
is_config_tag = lambda t: is_span(t) and is_class_pre(t) | |
tags = HTML.iter(HTTP.get('http://flask.pocoo.org/docs/latest/config/').content) | |
tags = filter(is_config_tag, tags) | |
keys = sorted(set(re.findall(r'[A-Z]{2,100}(?:(?:_?[A-Z0-9]+)*)', '|'.join(t)))) | |
print('message FlaskConfig {') | |
print('\n'.join([f' string {t.lower():<32} = {i:>3} [json_name="{t.upper()}"];' for i, t in enumerate(keys, start=1)])) | |
print('}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment