forked from TKCERT/pfFocus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.py
executable file
·73 lines (59 loc) · 2.43 KB
/
format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
import argparse
import sys
import yaml
from markdown import output_markdown
from parse import parse_pfsense
from pfsense import PfSenseDocument
from progress import Animation
def output_yaml(doc, stream):
yaml.safe_dump(doc.data, stream)
OUTPUT_FORMATS = {
'yaml': output_yaml,
'md': output_markdown,
}
def get_output_func(args):
return OUTPUT_FORMATS.get(args.output_format, output_yaml)
def get_progress_animation(args):
return Animation(args.quiet or args.output_path == '-')
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-q", dest="quiet", action="store_const", const=True, default=False, help="Hide progress messages")
parser.add_argument("-i", dest="input_path", help="XML input path", required=True)
parser.add_argument("-o", dest="output_path", help="Output path", default="-")
parser.add_argument("-f", dest="output_format", help="Output format", default="yaml", choices=OUTPUT_FORMATS.keys())
return parser.parse_args()
def step_parse(args, doc):
if not args.quiet:
print('\u268b Parsing "{}" ...'.format(args.input_path), file=sys.stderr)
with get_progress_animation(args):
parse_pfsense(args.input_path, doc)
if not args.quiet:
print('\u268d Successfully parsed pfSense config version {}.'.format(doc.pfsense.version), file=sys.stderr)
def step_stdout(args, doc, output_func):
if not args.quiet:
print('\u2631 Outputting to stdout ...', file=sys.stderr)
with get_progress_animation(args):
output_file = sys.stdout
output_func(doc, output_file)
if not args.quiet:
print('\u2630 Successfully outputted pfSense config as {}.'.format(args.output_format), file=sys.stderr)
def step_file(args, doc, output_func):
if not args.quiet:
print('\u2631 Outputting to "{}" ...'.format(args.output_path), file=sys.stderr)
with get_progress_animation(args):
with open(args.output_path, 'w+') as output_file:
output_func(doc, output_file)
if not args.quiet:
print('\u2630 Successfully outputted pfSense config as {}.'.format(args.output_format), file=sys.stderr)
def main():
args = parse_args()
doc = PfSenseDocument()
output_func = get_output_func(args)
step_parse(args, doc)
if args.output_path == '-':
step_stdout(args, doc, output_func)
else:
step_file(args, doc, output_func)
if __name__ == '__main__':
main()