forked from DMTF/RDE-Dictionary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rde_schema_dictionary_gen.py
125 lines (105 loc) · 6.27 KB
/
rde_schema_dictionary_gen.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /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
"""
RDE Dictionary Builder
File : rde-dictionary-builder.py
Brief : This file contains the definitions and functionalities for generating
a RDE schema dictionary from a set of standard Redfish CSDL and JSON Schema
files
"""
import argparse
import sys
from rdebej.dictionary import *
if __name__ == '__main__':
# rde_schema_dictionary parse --schemaDir=directory --schemaFilename=filename
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='source')
# TODO: Fix remote for json fixups
# remote_parser = subparsers.add_parser('remote')
# remote_parser.add_argument('--schemaURL', type=str, required=True)
# remote_parser.add_argument('--entity', type=str, required=True)
# remote_parser.add_argument('--outputFile', type=str, required=False)
local_parser = subparsers.add_parser('local')
local_parser.add_argument('-c', '--csdlSchemaDirectories', nargs='*', type=str, required=True)
local_parser.add_argument('-j', '--jsonSchemaDirectories', nargs='*', type=str, required=True)
local_parser.add_argument('-s', '--schemaFilename', type=str, required=True)
local_parser.add_argument('-e', '--entity', type=str, required=True)
local_parser.add_argument('-o', '--oemSchemaFilenames', nargs='*', type=str, required=False)
local_parser.add_argument('-t', '--oemEntities', nargs='*', type=str, required=False)
local_parser.add_argument('-r', '--copyright', type=str, required=False)
local_parser.add_argument('-p', '--profile', type=str, required=False)
local_parser.add_argument('-d', '--outputFile', type=argparse.FileType('wb'), required=False)
local_parser.add_argument('-f', '--outputJsonDictionaryFile', type=argparse.FileType('w'), required=False)
annotation_v2_parser = subparsers.add_parser('annotation')
annotation_v2_parser.add_argument('-c', '--csdlSchemaDirectories', nargs='*', type=str, required=True)
annotation_v2_parser.add_argument('-j', '--jsonSchemaDirectories', nargs='*', type=str, required=True)
annotation_v2_parser.add_argument('-v', '--version', type=str, required=True)
annotation_v2_parser.add_argument('-r', '--copyright', type=str, required=False)
annotation_v2_parser.add_argument('-d', '--outputFile', type=argparse.FileType('wb'), required=False)
annotation_v2_parser.add_argument('-f', '--outputJsonDictionaryFile', type=argparse.FileType('w'), required=False)
error_parser = subparsers.add_parser('error')
error_parser.add_argument('-c', '--csdlSchemaDirectories', nargs='*', type=str, required=True)
error_parser.add_argument('-j', '--jsonSchemaDirectories', nargs='*', type=str, required=True)
error_parser.add_argument('-r', '--copyright', type=str, required=False)
error_parser.add_argument('-d', '--outputFile', type=argparse.FileType('wb'), required=False)
error_parser.add_argument('-f', '--outputJsonDictionaryFile', type=argparse.FileType('w'), required=False)
dictionary_dump = subparsers.add_parser('view')
dictionary_dump.add_argument('-f', '--file', type=str, required=True)
args = parser.parse_args()
if len(sys.argv) == 1 or args.source is None:
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
# view an existing binary dictionary
if args.source == 'view':
# load the binary dictionary file
file = open(args.file, 'rb')
contents = file.read()
print_binary_dictionary(list(contents))
sys.exit()
# Generate the schema dictionary.
schema_dictionary = None
if args.source == 'local':
schema_dictionary = generate_schema_dictionary(args.source, args.csdlSchemaDirectories,
args.jsonSchemaDirectories, args.entity,
args.schemaFilename, args.oemEntities,
args.oemSchemaFilenames, args.profile,
None,
args.copyright)
elif args.source == 'remote':
schema_dictionary = generate_schema_dictionary(args.source, None, None, args.entity, None,
None, None, None, args.schemaURL)
elif args.source == 'annotation':
# Just choose a dummy complex entity type to start the annotation dictionary generation process.
schema_dictionary = generate_annotation_schema_dictionary(args.csdlSchemaDirectories,
args.jsonSchemaDirectories, args.version,
args.copyright)
elif args.source == 'error':
schema_dictionary = generate_error_schema_dictionary(args.csdlSchemaDirectories,
args.jsonSchemaDirectories, args.copyright)
# Print table data.
if schema_dictionary is not None and schema_dictionary.dictionary:
if not silent:
print_table_data(
[["Row", "Sequence#", "Format", "Flags", "Field String", "Child Count", "Offset"]]
+
schema_dictionary.dictionary)
# Print dictionary summary.
if not silent:
print_dictionary_summary(schema_dictionary.dictionary, schema_dictionary.dictionary_byte_array)
# Generate binary dictionary file
if args.outputFile:
args.outputFile.write(bytes(schema_dictionary.dictionary_byte_array))
if args.outputJsonDictionaryFile:
args.outputJsonDictionaryFile.write(schema_dictionary.json_dictionary)
else:
print('Error, dictionary could not be generated')