]> git.proxmox.com Git - mirror_ovs.git/blob - python/ovs/db/parser.py
7f0a6f0629eb337c65fffb47d6f5ddb7329cb9bf
[mirror_ovs.git] / python / ovs / db / parser.py
1 # Copyright (c) 2010, 2011 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 present = "and %d other members are" % len(missing)
57 elif missing:
58 present = "and 1 other member are"
59 else:
60 present = "is"
61 self.__raise_error("Member '%s' %s present but not allowed here" %
62 (name, present))
63
64 def float_to_int(x):
65 # XXX still needed?
66 if type(x) == float:
67 integer = int(x)
68 if integer == x and -2**53 <= integer < 2**53:
69 return integer
70 return x
71
72 id_re = re.compile("[_a-zA-Z][_a-zA-Z0-9]*$")
73 def is_identifier(s):
74 return type(s) in [str, unicode] and id_re.match(s)
75
76 def json_type_to_string(type_):
77 if type_ == None:
78 return "null"
79 elif type_ == bool:
80 return "boolean"
81 elif type_ == dict:
82 return "object"
83 elif type_ == list:
84 return "array"
85 elif type_ in [int, long, float]:
86 return "number"
87 elif type_ in [str, unicode]:
88 return "string"
89 else:
90 return "<invalid>"
91
92 def unwrap_json(json, name, types, desc):
93 if (type(json) not in (list, tuple) or len(json) != 2 or json[0] != name or
94 type(json[1]) not in types):
95 raise error.Error('expected ["%s", <%s>]' % (name, desc), json)
96 return json[1]
97
98 def parse_json_pair(json):
99 if type(json) != list or len(json) != 2:
100 raise error.Error("expected 2-element array", json)
101 return json
102