2 # Copyright 2016 RIFT.io Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
24 from deepdiff
import DeepDiff
26 from toscaparser
.utils
.gettextutils
import _
29 class CompareDescShell(object):
31 SUPPORTED_TYPES
= ['json']
33 DIFF_KEYS
= (REMOVED_ITEMS
, ADDED_ITEMS
, TYPE_CHANGES
, VALUES_CHANGED
) = \
34 ('dic_item_removed', 'dic_item_added', 'type_changes',
36 DIFF_MAP
= {REMOVED_ITEMS
: 'Items removed',
37 ADDED_ITEMS
: 'Items added',
38 TYPE_CHANGES
: 'Changes in types',
39 VALUES_CHANGED
: 'Changes in values'}
40 # Currently considering changes in removed keys or type changes
42 ERROR_ITEMS
= [REMOVED_ITEMS
, TYPE_CHANGES
]
44 def main(self
, log
, args
):
46 print("Args: {}".format(args
))
47 self
.log
.debug(_("Args: {0}").format(args
))
48 if args
.type not in self
.SUPPORTED_TYPES
:
49 self
.log
.error(_("Unsupported file type {0}").
53 with
open(args
.generated_file
) as g
:
55 json_gen
= json
.loads(gen_data
)
56 self
.log
.debug(_("Generated: {0}").format(json_gen
))
58 with
open(args
.expected_file
) as e
:
60 json_exp
= json
.loads(exp_data
)
61 self
.log
.debug(_("Expected: {0}").format(json_exp
))
63 diff
= DeepDiff(json_exp
, json_gen
)
64 self
.log
.debug(_("Keys in diff: {0}").format(diff
.keys()))
65 self
.log
.info(_("Differences:\n"))
67 d
= pprint
.pformat(diff
, indent
=self
.INDENT
)
68 self
.log
.info("Differences:\n{0}".format(d
))
70 if len(set(self
.ERROR_ITEMS
).intersection(diff
.keys())):
71 diff_str
= pprint
.pformat(diff
)
72 msg
= _("Found item changes: {0}").format(diff_str
)
78 parser
= argparse
.ArgumentParser(
79 description
='Validate descriptors by comparing')
84 help="Generated descriptor file")
89 help="Descriptor to compare")
94 help="File type. Default json")
97 help="Enable debug logging",
100 args
= parser
.parse_args(args
)
102 args
= parser
.parse_args()
105 logging
.basicConfig(level
=logging
.DEBUG
)
107 logging
.basicConfig(level
=logging
.ERROR
)
108 log
= logging
.getLogger("rwmano-translator")
110 CompareDescShell().main(log
, args
)
113 if __name__
== '__main__':