projects
/
openvswitch
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
ovs.stream: Drop Stream.get_name() since clients can use 'name' directly.
[openvswitch]
/
python
/
ovs
/
db
/
data.py
diff --git
a/python/ovs/db/data.py
b/python/ovs/db/data.py
index bfdc11cbcf75c51881f4f5e0f7d5b265e8ac154f..9530fb9cdfc5b873d00a2de0ed9e0caa636ab216 100644
(file)
--- a/
python/ovs/db/data.py
+++ b/
python/ovs/db/data.py
@@
-1,4
+1,4
@@
-# 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.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@
-35,41
+35,41
@@
class ConstraintViolation(error.Error):
error.Error.__init__(self, msg, json, tag="constraint violation")
def escapeCString(src):
error.Error.__init__(self, msg, json, tag="constraint violation")
def escapeCString(src):
- dst =
""
+ dst =
[]
for c in src:
if c in "\\\"":
for c in src:
if c in "\\\"":
- dst
+= "\\" + c
+ dst
.append("\\" + c)
elif ord(c) < 32:
if c == '\n':
elif ord(c) < 32:
if c == '\n':
- dst
+= '\\n'
+ dst
.append('\\n')
elif c == '\r':
elif c == '\r':
- dst
+= '\\r'
+ dst
.append('\\r')
elif c == '\a':
elif c == '\a':
- dst
+= '\\a'
+ dst
.append('\\a')
elif c == '\b':
elif c == '\b':
- dst
+= '\\b'
+ dst
.append('\\b')
elif c == '\f':
elif c == '\f':
- dst
+= '\\f'
+ dst
.append('\\f')
elif c == '\t':
elif c == '\t':
- dst
+= '\\t'
+ dst
.append('\\t')
elif c == '\v':
elif c == '\v':
- dst
+= '\\v'
+ dst
.append('\\v')
else:
else:
- dst
+= '\\%03o' % ord(c
)
+ dst
.append('\\%03o' % ord(c)
)
else:
else:
- dst
+= c
- return
dst
+ dst
.append(c)
+ return
''.join(dst)
def returnUnchanged(x):
return x
class Atom(object):
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:
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:
def __cmp__(self, other):
if not isinstance(other, Atom) or self.type != other.type:
@@
-85,11
+85,11
@@
class Atom(object):
return hash(self.value)
@staticmethod
return hash(self.value)
@staticmethod
- def default(type):
- return Atom(type)
+ def default(type
_
):
+ return Atom(type
_
)
def is_default(self):
def is_default(self):
- return self == default(self.type)
+ return self ==
self.
default(self.type)
@staticmethod
def from_json(base, json, symtab=None):
@staticmethod
def from_json(base, json, symtab=None):
@@
-143,12
+143,12
@@
class Atom(object):
length = len(s)
if length < base.min_length:
raise ConstraintViolation(
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, 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:
def to_json(self):
if self.type == ovs.db.types.UuidType:
@@
-227,8
+227,8
@@
class Atom(object):
return Atom(t, x)
class Datum(object):
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):
self.values = values
def __cmp__(self, other):
@@
-250,18
+250,17
@@
class Datum(object):
return Datum(self.type, dict(self.values))
@staticmethod
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 = {}
values = {}
- elif type.is_map():
- values = {type
.key.default(): type
.value.default()}
+ elif type
_
.is_map():
+ values = {type
_.key.default(): type_
.value.default()}
else:
else:
- values = {type.key.default(): None}
- return Datum(type, values)
+ values = {type
_
.key.default(): None}
+ return Datum(type
_
, values)
- @staticmethod
def is_default(self):
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
def check_constraints(self):
"""Checks that each of the atoms in 'datum' conforms to the constraints
@@
-326,41
+325,43
@@
class Datum(object):
return Datum(type_, {keyAtom: None})
def to_json(self):
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()
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:
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):
def to_string(self):
+ head = tail = None
if self.type.n_max > 1 or len(self.values) == 0:
if self.type.is_map():
if self.type.n_max > 1 or len(self.values) == 0:
if self.type.is_map():
- s = "{"
+ head = "{"
+ tail = "}"
else:
else:
- s = "["
- else:
- s = ""
+ head = "["
+ tail = "]"
+
+ s = []
+ if head:
+ s.append(head)
i = 0
for key in sorted(self.values):
i = 0
for key in sorted(self.values):
- if i
> 0
:
- s
+= ", "
+ if i:
+ s
.append(", ")
i += 1
i += 1
+ s.append(key.to_string())
if self.type.is_map():
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 as_list(self):
if self.type.is_map():
@@
-401,7
+402,7
@@
class Datum(object):
def conforms_to_type(self):
n = len(self.values)
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:
def cInitDatum(self, var):
if len(self.values) == 0: