Global replace of Nicira Networks.
[sliver-openvswitch.git] / python / ovs / db / parser.py
1 # Copyright (c) 2010, 2011 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import re
16
17 from ovs.db import error
18
19
20 class Parser(object):
21     def __init__(self, json, name):
22         self.name = name
23         self.json = json
24         if type(json) != dict:
25             self.__raise_error("Object expected.")
26         self.used = set()
27
28     def __get(self, name, types, optional, default=None):
29         if name in self.json:
30             self.used.add(name)
31             member = float_to_int(self.json[name])
32             if is_identifier(member) and "id" in types:
33                 return member
34             if len(types) and type(member) not in types:
35                 self.__raise_error("Type mismatch for member '%s'." % name)
36             return member
37         else:
38             if not optional:
39                 self.__raise_error("Required '%s' member is missing." % name)
40             return default
41
42     def get(self, name, types):
43         return self.__get(name, types, False)
44
45     def get_optional(self, name, types, default=None):
46         return self.__get(name, types, True, default)
47
48     def __raise_error(self, message):
49         raise error.Error("Parsing %s failed: %s" % (self.name, message),
50                           self.json)
51
52     def finish(self):
53         missing = set(self.json) - set(self.used)
54         if missing:
55             name = missing.pop()
56             if len(missing) > 1:
57                 present = "and %d other members are" % len(missing)
58             elif missing:
59                 present = "and 1 other member are"
60             else:
61                 present = "is"
62             self.__raise_error("Member '%s' %s present but not allowed here" %
63                                (name, present))
64
65
66 def float_to_int(x):
67     # XXX still needed?
68     if type(x) == float:
69         integer = int(x)
70         if integer == x and -2 ** 53 <= integer < 2 ** 53:
71             return integer
72     return x
73
74
75 id_re = re.compile("[_a-zA-Z][_a-zA-Z0-9]*$")
76
77
78 def is_identifier(s):
79     return type(s) in [str, unicode] and id_re.match(s)
80
81
82 def json_type_to_string(type_):
83     if type_ == None:
84         return "null"
85     elif type_ == bool:
86         return "boolean"
87     elif type_ == dict:
88         return "object"
89     elif type_ == list:
90         return "array"
91     elif type_ in [int, long, float]:
92         return "number"
93     elif type_ in [str, unicode]:
94         return "string"
95     else:
96         return "<invalid>"
97
98
99 def unwrap_json(json, name, types, desc):
100     if (type(json) not in (list, tuple) or len(json) != 2 or json[0] != name or
101         type(json[1]) not in types):
102         raise error.Error('expected ["%s", <%s>]' % (name, desc), json)
103     return json[1]
104
105
106 def parse_json_pair(json):
107     if type(json) != list or len(json) != 2:
108         raise error.Error("expected 2-element array", json)
109     return json