Skip to content

Commit

Permalink
Work Encode and Decode with Message in AMF3
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan committed Apr 4, 2017
1 parent c113bf4 commit 9858cb5
Show file tree
Hide file tree
Showing 36 changed files with 434 additions and 457 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dkms.conf
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
.idea/

# Sensitive or high-churn files:
.idea/**/dataSources/
Expand Down
24 changes: 13 additions & 11 deletions pyamf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@status: Production/Stable
"""

import types
import inspect

from pyamf import util, _version
Expand Down Expand Up @@ -377,6 +376,9 @@ def load_class(alias):
@return: Class registered to the alias.
@rtype: C{classobj}
"""
if isinstance(alias, bytes):
alias = alias.decode()

# Try the CLASS_CACHE first
try:
return CLASS_CACHE[alias]
Expand All @@ -399,10 +401,10 @@ def load_class(alias):

raise TypeError("Expecting class object or ClassAlias from loader")

mod_class = alias.split('.')
mod_class = alias.split(b'.')

if mod_class:
module = '.'.join(mod_class[:-1])
module = b'.'.join(mod_class[:-1])
klass = mod_class[-1]

try:
Expand Down Expand Up @@ -571,15 +573,15 @@ def flex_loader(alias):
@raise UnknownClassAlias: Trying to load an unknown Flex compatibility
class.
"""
if not alias.startswith('flex.'):
if not alias.startswith(b'flex.'):
return

try:
if alias.startswith('flex.messaging.messages'):
if alias.startswith(b'flex.messaging.messages'):
import pyamf.flex.messaging
elif alias.startswith('flex.messaging.io'):
elif alias.startswith(b'flex.messaging.io'):
import pyamf.flex
elif alias.startswith('flex.data.messages'):
elif alias.startswith(b'flex.data.messages'):
import pyamf.flex.data # noqa

return CLASS_CACHE[alias]
Expand Down Expand Up @@ -611,7 +613,7 @@ def _check_type(type_):
if type_ in TYPE_MAP:
raise KeyError('Type %r already exists' % (type_,))

if isinstance(type_, types.TupleType):
if isinstance(type_, tuple):
for x in type_:
_check_type(x)
else:
Expand All @@ -630,7 +632,7 @@ def get_type(type_):
if isinstance(type_, list):
type_ = tuple(type_)

for k, v in TYPE_MAP.iteritems():
for k, v in TYPE_MAP.items():
if k == type_:
return v

Expand Down Expand Up @@ -757,7 +759,7 @@ def register_alias_type(klass, *args):
- At least one type must be supplied
"""
def check_type_registered(arg):
for k, v in ALIAS_TYPES.iteritems():
for k, v in list(ALIAS_TYPES.items()):
for kl in v:
if arg is kl:
raise RuntimeError('%r is already registered under %r' % (
Expand Down Expand Up @@ -785,7 +787,7 @@ def check_type_registered(arg):

ALIAS_TYPES[klass] = args

for k, v in CLASS_CACHE.copy().iteritems():
for k, v in list(CLASS_CACHE.copy().items()):
new_alias = util.get_class_alias(v.klass)

if new_alias is klass:
Expand Down
8 changes: 4 additions & 4 deletions pyamf/adapters/_django_db_models_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def getEncodableAttributes(self, obj, **kwargs):
if not attrs:
attrs = {}

for name, prop in self.fields.iteritems():
for name, prop in self.fields.items():
if name not in attrs.keys():
continue

Expand All @@ -197,7 +197,7 @@ def getEncodableAttributes(self, obj, **kwargs):
if key.startswith('_'):
del attrs[key]

for name, relation in self.relations.iteritems():
for name, relation in self.relations.items():
if '_%s_cache' % name in obj.__dict__:
attrs[name] = getattr(obj, name)

Expand Down Expand Up @@ -230,7 +230,7 @@ def getDecodableAttributes(self, obj, attrs, **kwargs):

# primary key of django object must always be set first for
# relationships with other model objects to work properly
# and dict.iteritems() does not guarantee order
# and dict.items() does not guarantee order
#
# django also forces the use only one attribute as primary key, so
# our obj._meta.pk.attname check is sufficient)
Expand All @@ -249,7 +249,7 @@ def getDecodableAttributes(self, obj, attrs, **kwargs):
pass

if not getattr(obj, pk_attr):
for name, relation in self.relations.iteritems():
for name, relation in self.relations.items():
if isinstance(relation, models.ManyToManyField):
try:
if len(attrs[name]) == 0:
Expand Down
4 changes: 2 additions & 2 deletions pyamf/adapters/_google_appengine_ext_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def getCustomProperties(self):
self.properties = {}
reverse_props = []

for name, prop in self.klass.properties().iteritems():
for name, prop in self.klass.properties().items():
self.properties[name] = prop

if isinstance(prop, db.ReferenceProperty):
Expand All @@ -86,7 +86,7 @@ def getCustomProperties(self):
# check if the property is a defined as a collection_name. These types
# of properties are read-only and the datastore freaks out if you
# attempt to meddle with it. We delete the attribute entirely ..
for name, value in self.klass.__dict__.iteritems():
for name, value in self.klass.__dict__.items():
if isinstance(value, db._ReverseReferenceProperty):
reverse_props.append(name)

Expand Down
8 changes: 4 additions & 4 deletions pyamf/adapters/_google_appengine_ext_ndb.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def getCustomProperties(self):
# list of property names that are computed
computed_props = {}

for name, prop in self.klass._properties.iteritems():
for name, prop in self.klass._properties.items():
props[name] = prop

if prop._repeated:
Expand All @@ -88,7 +88,7 @@ def getCustomProperties(self):

# check if the property is a defined as a computed property. These
# types of properties are read-only
for name, value in self.klass.__dict__.iteritems():
for name, value in self.klass.__dict__.items():
if isinstance(value, ndb.ComputedProperty):
read_only_props.append(name)

Expand All @@ -110,7 +110,7 @@ def getDecodableAttributes(self, obj, attrs, codec=None):
)

if self.repeated_properties:
for name, prop in self.repeated_properties.iteritems():
for name, prop in self.repeated_properties.items():
try:
value = attrs[name]
except KeyError:
Expand Down Expand Up @@ -183,7 +183,7 @@ def getEncodableAttributes(self, obj, codec=None):
)

if isinstance(obj, ndb.Expando):
for name, prop in obj._properties.iteritems():
for name, prop in obj._properties.items():
if name in self.model_properties:
continue

Expand Down
2 changes: 1 addition & 1 deletion pyamf/adapters/gae_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def transform(self, stub=None):

return

for stub, (klass, key) in self.stubs.iteritems():
for stub, (klass, key) in self.stubs.items():
self.transformStub(stub, klass, key)


Expand Down
4 changes: 2 additions & 2 deletions pyamf/adapters/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def decode_model_property(obj, prop, value):
if handler:
return handler(obj, prop, value)

for model_prop, handler in _property_decoders.iteritems():
for model_prop, handler in _property_decoders.items():
if isinstance(prop, model_prop):
_property_decoders[prop.__class__] = handler

Expand All @@ -97,7 +97,7 @@ def encode_model_property(obj, prop, value):
if handler:
return handler(obj, prop, value)

for model_prop, handler in _property_encoders.iteritems():
for model_prop, handler in _property_encoders.items():
if isinstance(prop, model_prop):
_property_encoders[prop.__class__] = handler

Expand Down
2 changes: 1 addition & 1 deletion pyamf/adapters/tests/google/test_blobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BlobStoreTestCase(google.BaseTestCase):
'content_type': 'text/plain',
'size': 1234567,
'filename': 'not-telling.ogg',
'creation': datetime.datetime(2010, 07, 11, 14, 15, 01)
'creation': datetime.datetime(2010, 7, 11, 14, 15, 1)
}

def setUp(self):
Expand Down
4 changes: 2 additions & 2 deletions pyamf/adapters/tests/google/test_ndb.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def test_amf0(self):
b'\x00\x00\x00\x00\x00\x00\x00\nbirth_date\x0bB9\x15\xda$\x00\x00'
b'\x00\x00\x00\x00\x06height\x00?\xfc=p\xa3\xd7\n=\x00\x0cmeasurem'
b'ents\n\x00\x00\x00\x03\x00?\xf0\x00\x00\x00\x00\x00\x00\x00@\x00'
'\x00\x00\x00\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00\x00\x04'
'name\x02\x00\nHeidi Klum\x00\x00\t'
b'\x00\x00\x00\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00\x00\x04'
b'name\x02\x00\nHeidi Klum\x00\x00\t'
)

decoder = pyamf.decode(data, encoding=pyamf.AMF0)
Expand Down
8 changes: 4 additions & 4 deletions pyamf/adapters/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def setUp(self):

self.orig = ['f', 'o', 'o']

self.obj = array.array('c')
self.obj = array.array('b')

self.obj.append('f')
self.obj.append('o')
self.obj.append('o')
self.obj.append(ord('f'))
self.obj.append(ord('o'))
self.obj.append(ord('o'))

def encdec(self, encoding):
return pyamf.decode(
Expand Down
18 changes: 12 additions & 6 deletions pyamf/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def compile(self):
self.decodable_properties.update(self.klass.__slots__)
self.encodable_properties.update(self.klass.__slots__)

for k, v in self.klass.__dict__.iteritems():
for k, v in list(self.klass.__dict__.items()):
if not isinstance(v, property):
continue

Expand Down Expand Up @@ -318,7 +318,7 @@ def __repr__(self):
)

def __eq__(self, other):
if isinstance(other, basestring):
if isinstance(other, str):
return self.alias == other
elif isinstance(other, self.__class__):
return self.klass == other.klass
Expand Down Expand Up @@ -439,14 +439,14 @@ def getEncodableAttributes(self, obj, codec=None):
if self.proxy_attrs is not None and attrs and codec:
context = codec.context

for k, v in attrs.copy().iteritems():
for k, v in attrs.copy().items():
if k in self.proxy_attrs:
attrs[k] = context.getProxyForObject(v)

if self.synonym_attrs:
missing = object()

for k, v in self.synonym_attrs.iteritems():
for k, v in self.synonym_attrs.items():
value = attrs.pop(k, missing)

if value is missing:
Expand Down Expand Up @@ -474,7 +474,7 @@ def getDecodableAttributes(self, obj, attrs, codec=None):

changed = False

props = set(attrs.keys())
props = set(key.decode() if isinstance(key, bytes) else key for key in attrs.keys())

if self.static_attrs:
missing_attrs = self.static_attrs_set.difference(props)
Expand Down Expand Up @@ -528,7 +528,7 @@ def getDecodableAttributes(self, obj, attrs, codec=None):
if self.synonym_attrs:
missing = object()

for k, v in self.synonym_attrs.iteritems():
for k, v in self.synonym_attrs.items():
value = attrs.pop(v, missing)

if value is missing:
Expand All @@ -552,6 +552,12 @@ def applyAttributes(self, obj, attrs, codec=None):
if not self._compiled:
self.compile()

old_attrs = attrs
attrs = dict()
for key, value in old_attrs.items():
key = key.decode() if isinstance(key, bytes) else key
attrs[key] = value

if not self.shortcut_decode:
attrs = self.getDecodableAttributes(obj, attrs, codec=codec)
else:
Expand Down
Loading

0 comments on commit 9858cb5

Please sign in to comment.