-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpldm_bej_encoder_decoder.py
105 lines (83 loc) · 3.93 KB
/
pldm_bej_encoder_decoder.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/python3
# Copyright Notice:
# Copyright 2018-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/RDE-Dictionary/blob/master/LICENSE.md
"""
PLDM BEJ Encoder/Decoder
File : pldm_bej_encoder_decoder.py
Brief : This file allows encoding a JSON file to PLDM Binary encoded JSON (BEJ) and
decoding a PLDM BEJ file back into JSON.
"""
import argparse
import json
import io
import sys
from rdebej import encode, decode
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("--silent", help="no output prints unless errors", action="store_true")
subparsers = parser.add_subparsers(dest='operation')
encode_parser = subparsers.add_parser('encode')
encode_parser.add_argument('-s', '--schemaDictionary', type=argparse.FileType('rb'), required=True)
encode_parser.add_argument('-a', '--annotationDictionary', type=argparse.FileType('rb'), required=True)
encode_parser.add_argument('-j', '--jsonFile', type=argparse.FileType('r'), required=False)
encode_parser.add_argument('-o', '--bejOutputFile', type=argparse.FileType('wb'), required=False)
encode_parser.add_argument('-op', '--pdrMapFile', type=argparse.FileType('w'), required=False)
decode_parser = subparsers.add_parser('decode')
decode_parser.add_argument('-s', '--schemaDictionary', type=argparse.FileType('rb'), required=True)
decode_parser.add_argument('-a', '--annotationDictionary', type=argparse.FileType('rb'), required=True)
decode_parser.add_argument('-b', '--bejEncodedFile', type=argparse.FileType('rb'), required=True)
decode_parser.add_argument('-p', '--pdrMapFile', type=argparse.FileType('r'), required=False)
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
# Set the verbose flag.
verbose = args.verbose
silent = args.silent
if verbose and silent: # override silent if verbose is set
verbose = True
silent = False
# Read the binary schema dictionary into a byte array
schema_dictionary = list(args.schemaDictionary.read())
# Read the binary annotation dictionary into a byte array
annotation_dictionary = list(args.annotationDictionary.read())
if args.operation == 'encode':
json_str = {}
# Read the json file
if args.jsonFile:
json_str = args.jsonFile.read()
else: # read from stdin
json_str = sys.stdin.read()
json_to_encode = json.loads(json_str)
# create a byte stream
output_stream = io.BytesIO()
success, pdr_map = encode.bej_encode(output_stream, json_to_encode, schema_dictionary, annotation_dictionary)
if success:
encoded_bytes = output_stream.getvalue()
if not silent:
encode.print_encode_summary(json_to_encode, encoded_bytes)
if args.bejOutputFile:
args.bejOutputFile.write(encoded_bytes)
if args.pdrMapFile:
args.pdrMapFile.write(json.dumps(pdr_map))
else:
if not silent:
print('Failed to encode JSON')
elif args.operation == 'decode':
# Read the encoded bytes
bej_encoded_bytes = list(args.bejEncodedFile.read())
pdr_map = {}
if args.pdrMapFile:
pdr_map = json.loads(args.pdrMapFile.read())
input_stream = io.BytesIO(bytes(bej_encoded_bytes))
output_stream = io.StringIO()
success = decode.bej_decode(output_stream, input_stream, schema_dictionary, annotation_dictionary, {}, pdr_map,
{})
if success:
if not silent:
print(json.dumps(json.loads(output_stream.getvalue()), indent=3))
else:
if not silent:
print('Failed to decode JSON')