-
Notifications
You must be signed in to change notification settings - Fork 0
/
ardconv
executable file
·67 lines (54 loc) · 1.94 KB
/
ardconv
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
#!/usr/bin/env python3
import argparse, ard, sys
def main():
parser = argparse.ArgumentParser(description='Convert ARD between transmission formats')
parser.add_argument('--version', '-V', action='store_true', help='show version')
parser.add_argument('input', type=str, nargs='?', help='input file (stdin if not provided)')
parser.add_argument('output', type=str, nargs='?', help='output file (stdout if not provided)')
parser.add_argument('--input', '-i', dest='input_format', type=str, choices=('yaml', 'json', 'cjson', 'xml', 'cbor'), default='yaml', help='input format')
parser.add_argument('--output', '-o', dest='output_format', type=str, choices=('yaml', 'json', 'cjson', 'xml', 'cbor'), default='yaml', help='output format')
parser.add_argument('--indent', '-n', type=int, default=2, help='output indentation size')
parser.add_argument('--verbose', '-v', action='store_true', help='verbose errors')
args = parser.parse_args()
if args.version:
print(ard.__version__)
sys.exit(0)
try:
if args.input is not None:
input = open(args.input, 'r')
else:
if args.input_format == 'cbor':
input = sys.stdin.buffer
else:
input = sys.stdin
try:
value = ard.read(input, args.input_format)
finally:
if args.input is not None:
input.close()
if args.output is not None:
output = open(args.output, 'w')
else:
if args.output_format == 'cbor':
output = sys.stdout.buffer
else:
output = sys.stdout
try:
ard.write(value, output, args.output_format, indent=' ' * args.indent)
finally:
if args.output is not None:
output.close()
except KeyboardInterrupt:
sys.exit(130)
except BaseException as e:
if args.verbose:
raise e
else:
m = str(e)
if m:
m = ': ' + m
m = type(e).__name__ + m
print(m, file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()