Update to handle multiple files at a time, and also coerce integers into integers
This commit is contained in:
parent
61a87f190c
commit
d58953d10d
@ -1,47 +1,75 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
from mailman.utilities.importer import NAME_MAPPINGS
|
from mailman.utilities.importer import NAME_MAPPINGS
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
KEYFILTER = ('submit')
|
||||||
prog=sys.argv[0], description="Munge Mailman config data"
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument("file", type=argparse.FileType('r', encoding='utf-8'))
|
def msg(*args, **kwargs):
|
||||||
|
print(*args, file=sys.stderr, **kwargs)
|
||||||
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'):
|
def get_form_data(htmlfile):
|
||||||
if field['type'] == 'hidden':
|
data_clean = {}
|
||||||
continue
|
|
||||||
|
|
||||||
if field['type'] == 'RADIO':
|
soup = BeautifulSoup(htmlfile.read(), 'html.parser')
|
||||||
if 'checked' not in field.attrs:
|
|
||||||
|
for field in soup.find_all('textarea'):
|
||||||
|
name = NAME_MAPPINGS.get(field['name'], field['name'])
|
||||||
|
|
||||||
|
if 'msg' in name:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
name = NAME_MAPPINGS.get(field['name'], field['name'])
|
data_clean[name] = [l for l in field.get_text().split('\n') if l != ""]
|
||||||
|
|
||||||
if 'msg' in name:
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
for field in soup.find_all('input'):
|
||||||
data_clean[name] = field['value']
|
if field['type'].lower() in ('hidden', 'submit'):
|
||||||
except KeyError:
|
continue
|
||||||
data_clean[name] = ""
|
|
||||||
|
|
||||||
print(json.dumps(data_clean))
|
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))
|
||||||
|
Loading…
Reference in New Issue
Block a user