Step 1: Move vm scripts to the right place
[integration/test.git] / csit / libraries / HsfJson / hsfod.py
1 """This module contains single class, to store a sorted dict."""
2 # Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3 #
4 # This program and the accompanying materials are made available under the
5 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 # and is available at http://www.eclipse.org/legal/epl-v10.html
7
8 __author__ = "Vratko Polak"
9 __copyright__ = "Copyright(c) 2015, Cisco Systems, Inc."
10 __license__ = "Eclipse Public License v1.0"
11 __email__ = "vrpolak@cisco.com"
12
13 import collections as _collections
14
15
16 class Hsfod(_collections.OrderedDict):
17     """
18     Hashable sorted (by key) frozen OrderedDict implementation stub.
19
20     Supports only __init__, __repr__ and __hash__ methods.
21     Other OrderedDict methods are available, but they may break contract.
22     """
23
24     def __init__(self, *args, **kwargs):
25         """Put arguments to OrderedDict, sort, pass to super, cache values."""
26         self_unsorted = _collections.OrderedDict(*args, **kwargs)
27         items_sorted = sorted(self_unsorted.items(), key=repr)
28         sup = super(Hsfod, self)  # possibly something else than OrderedDict
29         sup.__init__(items_sorted)
30         # Repr string is used for sorting, keys are more important than values.
31         self.__repr = '{' + repr(self.keys()) + ':' + repr(self.values()) + '}'
32         self.__hash = hash(self.__repr)
33
34     def __repr__(self):
35         """Return cached repr string."""
36         return self.__repr
37
38     def __hash__(self):
39         """Return cached hash."""
40         return self.__hash