48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
|
import argparse
|
||
|
import json
|
||
|
import sys
|
||
|
from bs4 import BeautifulSoup
|
||
|
|
||
|
from mailman.utilities.importer import NAME_MAPPINGS
|
||
|
|
||
|
parser = argparse.ArgumentParser(
|
||
|
prog=sys.argv[0], description="Munge Mailman config data"
|
||
|
)
|
||
|
|
||
|
parser.add_argument("file", type=argparse.FileType('r', encoding='utf-8'))
|
||
|
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
data_clean = {}
|
||
|
|
||
|
soup = BeautifulSoup(args.file.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'] == 'hidden':
|
||
|
continue
|
||
|
|
||
|
if field['type'] == 'RADIO':
|
||
|
if 'checked' not in field.attrs:
|
||
|
continue
|
||
|
|
||
|
name = NAME_MAPPINGS.get(field['name'], field['name'])
|
||
|
|
||
|
if 'msg' in name:
|
||
|
continue
|
||
|
|
||
|
try:
|
||
|
data_clean[name] = field['value']
|
||
|
except KeyError:
|
||
|
data_clean[name] = ""
|
||
|
|
||
|
print(json.dumps(data_clean))
|