76 lines
1.9 KiB
Python
76 lines
1.9 KiB
Python
import argparse
|
|
import json
|
|
import sys
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from mailman.utilities.importer import NAME_MAPPINGS
|
|
|
|
KEYFILTER = ('submit')
|
|
|
|
def msg(*args, **kwargs):
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
def get_form_data(htmlfile):
|
|
data_clean = {}
|
|
|
|
soup = BeautifulSoup(htmlfile.read(), 'html.parser')
|
|
|
|
for field in soup.find_all('textarea'):
|
|
name = NAME_MAPPINGS.get(field['name'], field['name'])
|
|
|
|
if 'msg' in name:
|
|
continue
|
|
|
|
data_clean[name] = [l for l in field.get_text().split('\n') if l != ""]
|
|
|
|
|
|
for field in soup.find_all('input'):
|
|
if field['type'].lower() in ('hidden', 'submit'):
|
|
continue
|
|
|
|
if field['type'].lower() == 'radio':
|
|
if 'checked' not in field.attrs:
|
|
continue
|
|
|
|
name = NAME_MAPPINGS.get(field['name'], field['name'])
|
|
|
|
if 'msg' in name:
|
|
continue
|
|
|
|
try:
|
|
value = field['value']
|
|
try:
|
|
value = int(value)
|
|
except ValueError:
|
|
...
|
|
except KeyError:
|
|
value = ''
|
|
|
|
data_clean[name] = value
|
|
|
|
return data_clean
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(
|
|
prog=sys.argv[0], description="Munge Mailman config data"
|
|
)
|
|
parser.add_argument("config", help="A list of files (named specifically) to import as configurations",
|
|
nargs='+',
|
|
action="append")
|
|
args = parser.parse_args()
|
|
|
|
globalconfig = {}
|
|
for page in args.config[0]:
|
|
msg(page)
|
|
with open(page) as inf:
|
|
result = get_form_data(inf)
|
|
for key, value in result.items():
|
|
if key in globalconfig:
|
|
msg(f"warning - duplicate key {key}")
|
|
else:
|
|
globalconfig[key] = value
|
|
print(json.dumps(globalconfig))
|