# See the License for the specific language governing permissions and
# limitations under the License.
-import errno
-import logging
-import os
import re
-import select
-import sys
import uuid
import ovs.poller
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 == self.default(self.type)
or (type_ == ovs.db.types.StringType and type(json) in [str, unicode])):
atom = Atom(type_, json)
elif type_ == ovs.db.types.UuidType:
- atom = Atom(type_, ovs.ovsuuid.UUID.from_json(json, symtab))
+ atom = Atom(type_, ovs.ovsuuid.from_json(json, symtab))
else:
raise error.Error("expected %s" % type_.to_string(), json)
atom.check_constraints(base)
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 self.value.to_json()
+ return ovs.ovsuuid.to_json(self.value)
else:
return self.value
return ['%s.string = xstrdup("%s");'
% (var, escapeCString(self.value))]
elif self.type == ovs.db.types.UuidType:
- return self.value.cInitUUID(var)
+ return ovs.ovsuuid.to_c_assignment(self.value, var)
def toEnglish(self, escapeLiteral=returnUnchanged):
if self.type == ovs.db.types.IntegerType:
elif type(x) == float:
t = ovs.db.types.RealType
elif x in [False, True]:
- t = ovs.db.types.RealType
+ t = ovs.db.types.BooleanType
elif type(x) in [str, unicode]:
t = ovs.db.types.StringType
elif isinstance(x, uuid):
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):
def __contains__(self, item):
return item in self.values
- def clone(self):
+ def copy(self):
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)
def is_default(self):
return self == Datum.default(self.type)
This function is not commonly useful because the most ordinary way to
obtain a datum is ultimately via Datum.from_json() or Atom.from_json(),
which check constraints themselves."""
- for keyAtom, valueAtom in self.values:
- keyAtom.check_constraints()
+ for keyAtom, valueAtom in self.values.iteritems():
+ keyAtom.check_constraints(self.type.key)
if valueAtom is not None:
- valueAtom.check_constraints()
+ valueAtom.check_constraints(self.type.value)
@staticmethod
def from_json(type_, json, symtab=None):
else:
class_ = "set"
- inner = ovs.db.parser.unwrap_json(json, class_, list)
+ inner = ovs.db.parser.unwrap_json(json, class_, [list, tuple],
+ "array")
n = len(inner)
if n < type_.n_min or n > type_.n_max:
raise error.Error("%s must have %d to %d members but %d are "
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 head:
s.append(head)
- i = 0
- for key in sorted(self.values):
+ for i, key in enumerate(sorted(self.values)):
if i:
s.append(", ")
- i += 1
s.append(key.to_string())
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:
s += ["%s->keys = xmalloc(%d * sizeof *%s->keys);"
% (var, len(self.values), var)]
- i = 0
- for key, value in sorted(self.values.items()):
+ for i, key in enumerate(sorted(self.values)):
s += key.cInitAtom("%s->keys[%d]" % (var, i))
- i += 1
if self.type.value:
s += ["%s->values = xmalloc(%d * sizeof *%s->values);"
% (var, len(self.values), var)]
- i = 0
- for key, value in sorted(self.values.items()):
+ for i, (key, value) in enumerate(sorted(self.values.items())):
s += value.cInitAtom("%s->values[%d]" % (var, i))
- i += 1
else:
s += ["%s->values = NULL;" % var]