-# Copyright (c) 2009, 2010 Nicira Networks
+# Copyright (c) 2009, 2010, 2011 Nicira Networks
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
error.Error.__init__(self, msg, json, tag="constraint violation")
def escapeCString(src):
- dst = ""
+ dst = []
for c in src:
if c in "\\\"":
- dst += "\\" + c
+ dst.append("\\" + c)
elif ord(c) < 32:
if c == '\n':
- dst += '\\n'
+ dst.append('\\n')
elif c == '\r':
- dst += '\\r'
+ dst.append('\\r')
elif c == '\a':
- dst += '\\a'
+ dst.append('\\a')
elif c == '\b':
- dst += '\\b'
+ dst.append('\\b')
elif c == '\f':
- dst += '\\f'
+ dst.append('\\f')
elif c == '\t':
- dst += '\\t'
+ dst.append('\\t')
elif c == '\v':
- dst += '\\v'
+ dst.append('\\v')
else:
- dst += '\\%03o' % ord(c)
+ dst.append('\\%03o' % ord(c))
else:
- dst += c
- return dst
+ dst.append(c)
+ return ''.join(dst)
def returnUnchanged(x):
return x
class Atom(object):
- def __init__(self, type, value=None):
- self.type = type
+ def __init__(self, type_, value=None):
+ self.type = type_
if value is not None:
self.value = value
else:
- self.value = type.default_atom()
+ self.value = type_.default_atom()
def __cmp__(self, other):
if not isinstance(other, Atom) or self.type != other.type:
return hash(self.value)
@staticmethod
- def default(type):
- return Atom(type)
+ def default(type_):
+ return Atom(type_)
def is_default(self):
- return self == default(self.type)
+ return self == self.default(self.type)
@staticmethod
def from_json(base, json, symtab=None):
length = len(s)
if length < base.min_length:
raise ConstraintViolation(
- "\"%s\" length %d is less than minimum allowed length %d"
+ '"%s" length %d is less than minimum allowed length %d'
% (s, length, base.min_length))
elif length > base.max_length:
raise ConstraintViolation(
- "\"%s\" length %d is greater than maximum allowed "
- "length %d" % (s, length, base.max_length))
+ '"%s" length %d is greater than maximum allowed '
+ 'length %d' % (s, length, base.max_length))
def to_json(self):
if self.type == ovs.db.types.UuidType:
return Atom(t, x)
class Datum(object):
- def __init__(self, type, values={}):
- self.type = type
+ def __init__(self, type_, values={}):
+ self.type = type_
self.values = values
def __cmp__(self, other):
return Datum(self.type, dict(self.values))
@staticmethod
- def default(type):
- if type.n_min == 0:
+ def default(type_):
+ if type_.n_min == 0:
values = {}
- elif type.is_map():
- values = {type.key.default(): type.value.default()}
+ elif type_.is_map():
+ values = {type_.key.default(): type_.value.default()}
else:
- values = {type.key.default(): None}
- return Datum(type, values)
+ values = {type_.key.default(): None}
+ return Datum(type_, values)
- @staticmethod
def is_default(self):
- return self == default(self.type)
+ return self == Datum.default(self.type)
def check_constraints(self):
"""Checks that each of the atoms in 'datum' conforms to the constraints
return Datum(type_, {keyAtom: None})
def to_json(self):
- if len(self.values) == 1 and not self.type.is_map():
+ if self.type.is_map():
+ return ["map", [[k.to_json(), v.to_json()]
+ for k, v in sorted(self.values.items())]]
+ elif len(self.values) == 1:
key = self.values.keys()[0]
return key.to_json()
- elif not self.type.is_map():
- return ["set", [k.to_json() for k in sorted(self.values.keys())]]
else:
- return ["map", [[k.to_json(), v.to_json()]
- for k, v in sorted(self.values.items())]]
+ return ["set", [k.to_json() for k in sorted(self.values.keys())]]
def to_string(self):
+ head = tail = None
if self.type.n_max > 1 or len(self.values) == 0:
if self.type.is_map():
- s = "{"
+ head = "{"
+ tail = "}"
else:
- s = "["
- else:
- s = ""
+ head = "["
+ tail = "]"
+
+ s = []
+ if head:
+ s.append(head)
i = 0
for key in sorted(self.values):
- if i > 0:
- s += ", "
+ if i:
+ s.append(", ")
i += 1
+ s.append(key.to_string())
if self.type.is_map():
- s += "%s=%s" % (key.to_string(), self.values[key].to_string())
- else:
- s += key.to_string()
+ s.append("=")
+ s.append(self.values[key].to_string())
- if self.type.n_max > 1 or len(self.values) == 0:
- if self.type.is_map():
- s += "}"
- else:
- s += "]"
- return s
+ if tail:
+ s.append(tail)
+ return ''.join(s)
def as_list(self):
if self.type.is_map():
def conforms_to_type(self):
n = len(self.values)
- return n >= self.type.n_min and n <= self.type.n_max
+ return self.type.n_min <= n <= self.type.n_max
def cInitDatum(self, var):
if len(self.values) == 0: