]>
git.proxmox.com Git - mirror_ovs.git/blob - python/ovs/db/parser.py
1 # Copyright (c) 2010, 2011 Nicira, Inc.
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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from ovs
.db
import error
21 def __init__(self
, json
, name
):
24 if type(json
) != dict:
25 self
.__raise
_error
("Object expected.")
28 def __get(self
, name
, types
, optional
, default
=None):
31 member
= float_to_int(self
.json
[name
])
32 if is_identifier(member
) and "id" in types
:
34 if len(types
) and type(member
) not in types
:
35 self
.__raise
_error
("Type mismatch for member '%s'." % name
)
39 self
.__raise
_error
("Required '%s' member is missing." % name
)
42 def get(self
, name
, types
):
43 return self
.__get
(name
, types
, False)
45 def get_optional(self
, name
, types
, default
=None):
46 return self
.__get
(name
, types
, True, default
)
48 def __raise_error(self
, message
):
49 raise error
.Error("Parsing %s failed: %s" % (self
.name
, message
),
53 missing
= set(self
.json
) - set(self
.used
)
57 present
= "and %d other members are" % len(missing
)
59 present
= "and 1 other member are"
62 self
.__raise
_error
("Member '%s' %s present but not allowed here" %
70 if integer
== x
and -2 ** 53 <= integer
< 2 ** 53:
75 id_re
= re
.compile("[_a-zA-Z][_a-zA-Z0-9]*$")
79 return type(s
) in [str, unicode] and id_re
.match(s
)
82 def json_type_to_string(type_
):
91 elif type_
in [int, long, float]:
93 elif type_
in [str, unicode]:
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
)
106 def parse_json_pair(json
):
107 if type(json
) != list or len(json
) != 2:
108 raise error
.Error("expected 2-element array", json
)