summaryrefslogtreecommitdiff
path: root/python/qpid/codec.py
diff options
context:
space:
mode:
authorRafael H. Schloming <rhs@apache.org>2007-08-22 13:39:04 +0000
committerRafael H. Schloming <rhs@apache.org>2007-08-22 13:39:04 +0000
commit51c4f612aec73b473477cacb786865c76284e002 (patch)
tree64ab65d87efae9f81ca6e342693cb44f53e458c5 /python/qpid/codec.py
parentcaa402b97879d849273604842c1ec6bb63e40f26 (diff)
downloadqpid-python-51c4f612aec73b473477cacb786865c76284e002.tar.gz
added support for 0-10 style header encoding
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@568607 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/qpid/codec.py')
-rw-r--r--python/qpid/codec.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/python/qpid/codec.py b/python/qpid/codec.py
index 3920f2c8d9..ae113619ae 100644
--- a/python/qpid/codec.py
+++ b/python/qpid/codec.py
@@ -26,7 +26,7 @@ fields.
The unit test for this module is located in tests/codec.py
"""
-import re, qpid
+import re, qpid, spec
from cStringIO import StringIO
from struct import *
from reference import ReferenceId
@@ -113,13 +113,19 @@ class Codec:
"""
calls the appropriate encode function e.g. encode_octet, encode_short etc.
"""
- getattr(self, "encode_" + type)(value)
+ if isinstance(type, spec.Struct):
+ self.encode_struct(type, value)
+ else:
+ getattr(self, "encode_" + type)(value)
def decode(self, type):
"""
calls the appropriate decode function e.g. decode_octet, decode_short etc.
"""
- return getattr(self, "decode_" + type)()
+ if isinstance(type, spec.Struct):
+ return self.decode_struct(type)
+ else:
+ return getattr(self, "decode_" + type)()
def encode_bit(self, o):
"""
@@ -358,20 +364,30 @@ class Codec:
def decode_uuid(self):
return self.decode_longstr()
+ def encode_struct(self, type, s):
+ for f in type.fields:
+ if s == None:
+ val = f.default()
+ else:
+ val = s.get(f.name)
+ self.encode(f.type, val)
+ self.flush()
+
+ def decode_struct(self, type):
+ s = qpid.Struct(type)
+ for f in type.fields:
+ s.set(f.name, self.decode(f.type))
+ return s
+
def encode_long_struct(self, s):
enc = StringIO()
codec = Codec(enc, self.spec)
type = s.type
codec.encode_short(type.type)
- for f in type.fields:
- codec.encode(f.type, getattr(s, f.name))
- codec.flush()
+ codec.encode_struct(type, s)
self.encode_longstr(enc.getvalue())
def decode_long_struct(self):
codec = Codec(StringIO(self.decode_longstr()), self.spec)
type = self.spec.structs[codec.decode_short()]
- s = qpid.Struct(type)
- for f in type.fields:
- setattr(s, f.name, codec.decode(f.type))
- return s
+ return codec.decode_struct(type)