Implement initial Python bindings for Open vSwitch database.
[sliver-openvswitch.git] / python / ovs / db / parser.py
1 # Copyright (c) 2010 Nicira Networks
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 class Parser(object):
20     def __init__(self, json, name):
21         self.name = name
22         self.json = json
23         if type(json) != dict:
24             self.__raise_error("Object expected.")
25         self.used = set()
26
27     def __get(self, name, types, optional, default=None):
28         if name in self.json:
29             self.used.add(name)
30             member = float_to_int(self.json[name])
31             if is_identifier(member) and "id" in types:
32                 return member
33             if len(types) and type(member) not in types:
34                 self.__raise_error("Type mismatch for member '%s'." % name)
35             return member
36         else:
37             if not optional:        
38                 self.__raise_error("Required '%s' member is missing." % name)
39             return default
40
41     def get(self, name, types):
42         return self.__get(name, types, False)
43
44     def get_optional(self, name, types, default=None):
45         return self.__get(name, types, True, default)
46
47     def __raise_error(self, message):
48         raise error.Error("Parsing %s failed: %s" % (self.name, message),
49                           self.json)
50
51     def finish(self):
52         missing = set(self.json) - set(self.used)
53         if missing:
54             name = missing.pop()
55             if len(missing) > 1:
56                 self.__raise_error("Member '%s' and %d other members "
57                                    "are present but not allowed here"
58                                    % (name, len(missing)))
59             elif missing:
60                 self.__raise_error("Member '%s' and 1 other member "
61                                    "are present but not allowed here" % name)
62             else:
63                 self.__raise_error("Member '%s' is present but not "
64                                    "allowed here" % name)
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 integer >= -2**53 and integer < 2**53:
71             return integer
72     return x
73
74 id_re = re.compile("[_a-zA-Z][_a-zA-Z0-9]*$")
75 def is_identifier(s):
76     return type(s) in [str, unicode] and id_re.match(s)
77
78 def json_type_to_string(type):
79     if type == None:
80         return "null"
81     elif type == bool:
82         return "boolean"
83     elif type == dict:
84         return "object"
85     elif type == list:
86         return "array"
87     elif type in [int, long, float]:
88         return "number"
89     elif type in [str, unicode]:
90         return "string"
91     else:
92         return "<invalid>"
93
94 def unwrap_json(json, name, need_type):
95     if (type(json) != list or len(json) != 2 or json[0] != name or
96         type(json[1]) != need_type):
97         raise error.Error("expected [\"%s\", <%s>]"
98                           % (name, json_type_to_string(need_type)), json)
99     return json[1]
100
101 def parse_json_pair(json):
102     if type(json) != list or len(json) != 2:
103         raise error.Error("expected 2-element array", json)
104     return json
105