scripts: net: add enumerate_http_status.py

Add a script to extract HTTP status values and format them
in a way that is both human readable and machine parseable.

Each line of output is of the form:

```
HTTP_{key}_{upper_val} = {key}, /**< val */
```

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This commit is contained in:
Christopher Friedt 2022-10-10 13:03:24 -04:00 committed by Christopher Friedt
parent 4382df571f
commit ef142c117e

View file

@ -0,0 +1,53 @@
#!/usr/bin/env python3
# Copyright(c) 2022 Meta
# SPDX-License-Identifier: Apache-2.0
"""Format HTTP Status codes for use in a C header
This script extracts HTTP status codes from mozilla.org
and formats them to fit inside of a C enum along with
comments.
The output may appear somewhat redundant but it strives
to
a) be human readable
b) eliminate the need to look up status manually,
c) be machine parseable for table generation
The output is sorted for convenience.
Usage:
./scripts/net/enumerate_http_status.py
HTTP_100_CONTINUE = 100, /**< Continue */
...
HTTP_418_IM_A_TEAPOT = 418, /**< I'm a teapot */
...
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511, /**< Network Authentication Required */
"""
from lxml import html
import requests
import re
page = requests.get('https://developer.mozilla.org/en-US/docs/Web/HTTP/Status')
tree = html.fromstring(page.content)
codes = tree.xpath('//code/text()')
codes2 = {}
for c in codes:
if re.match('[0-9][0-9][0-9] [a-zA-Z].*', c):
key = int(c[0:3])
val = c[4:]
codes2[key] = val
keys = sorted(codes2.keys())
for key in keys:
val = codes2[key]
enum_head = 'HTTP'
enum_body = f'{key}'
enum_tail = val.upper().replace(' ', '_').replace("'", '').replace('-', '_')
enum_label = '_'.join([enum_head, enum_body, enum_tail])
comment = f'/**< {val} */'
print(f'{enum_label} = {key}, {comment}')