Skip to content

Commit

Permalink
Apply pep8.patch: bring PyAMF in line with coding conventions from PEP-8
Browse files Browse the repository at this point in the history
Author: thijs
Reviewer: nick
Fixes: #562


git-svn-id: https://svn.pyamf.org/pyamf/trunk@2432 2dde4cc4-cf3c-0410-b1a3-a9b8ff274da5
  • Loading branch information
Thijs Triemstra committed May 26, 2009
1 parent 5d08981 commit d6f5ec2
Show file tree
Hide file tree
Showing 54 changed files with 359 additions and 60 deletions.
14 changes: 10 additions & 4 deletions pyamf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
@status: Production/Stable
"""

import types, inspect
import types
import inspect

from pyamf import util
from pyamf.adapters import register_adapters
Expand All @@ -43,7 +44,8 @@
#: Alias mapping support
ALIAS_TYPES = {}

#: Specifies that objects are serialized using AMF for ActionScript 1.0 and 2.0.
#: Specifies that objects are serialized using AMF for ActionScript 1.0
#: and 2.0.
AMF0 = 0
#: Specifies that objects are serialized using AMF for ActionScript 3.0.
AMF3 = 3
Expand Down Expand Up @@ -78,6 +80,7 @@ class ClientTypes:


class UndefinedType(object):

def __repr__(self):
return 'pyamf.Undefined'

Expand Down Expand Up @@ -343,8 +346,8 @@ def __init__(self, klass, alias, attrs=None, attr_func=None, metadata=[]):
@param klass: The class to alias.
@type alias: C{str}
@param alias: The alias to the class e.g. C{org.example.Person}. If the
value of this is C{None}, then it is worked out based on the C{klass}.
The anonymous tag is also added to the class.
value of this is C{None}, then it is worked out based on the
C{klass}. The anonymous tag is also added to the class.
@type attrs: A list of attributes to encode for this class.
@param attrs: C{list}
@type metadata: A list of metadata tags similar to ActionScript tags.
Expand Down Expand Up @@ -696,6 +699,7 @@ class CustomTypeFunc(object):
"""
Custom type mappings.
"""

def __init__(self, encoder, func):
self.encoder = encoder
self.func = func
Expand Down Expand Up @@ -1206,6 +1210,7 @@ def add_type(type_, func=None):
@raise TypeError: Unable to add as a custom type (expected a class or callable).
@raise KeyError: Type already exists.
"""

def _check_type(type_):
if not (isinstance(type_, (type, types.ClassType)) or callable(type_)):
raise TypeError('Unable to add \'%r\' as a custom type (expected a class or callable)' % (type_,))
Expand Down Expand Up @@ -1332,6 +1337,7 @@ def register_alias_type(klass, *args):
@raise ValueError: New aliases must subclass L{pyamf.ClassAlias}.
@raise ValueError: At least one type must be supplied.
"""

def check_type_registered(arg):
# FIXME: Create a reverse index of registered types and do a quicker lookup
for k, v in ALIAS_TYPES.iteritems():
Expand Down
6 changes: 5 additions & 1 deletion pyamf/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
@since: 0.1.0
"""

import os.path, glob
import os.path
import glob

from pyamf.util import imports


class PackageImporter(object):
"""
Package importer used for lazy module loading.
Expand All @@ -24,6 +26,7 @@ def __call__(self, mod):

adapters_registered = False


def register_adapters():
global adapters_registered

Expand All @@ -49,6 +52,7 @@ def register_adapters():

adapters_registered = True


def register_adapter(mod, func):
"""
Registers a callable to be executed when a module is imported. If the
Expand Down
1 change: 1 addition & 0 deletions pyamf/adapters/_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import pyamf


def convert_Decimal(x, encoder):
"""
Called when an instance of L{decimal.Decimal} is about to be encoded to
Expand Down
1 change: 1 addition & 0 deletions pyamf/adapters/_django_db_models_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import pyamf


class DjangoClassAlias(pyamf.ClassAlias):
def getAttrs(self, obj, codec=None):
static_attrs, dynamic_attrs = [], []
Expand Down
1 change: 1 addition & 0 deletions pyamf/adapters/_django_db_models_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pyamf


def convert_NOT_PROVIDED(x, encoder):
"""
@rtype: L{Undefined<pyamf.Undefined>}
Expand Down
1 change: 1 addition & 0 deletions pyamf/adapters/_django_utils_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pyamf


def convert_lazy(l, encoder=None):
if l.__class__._delegate_unicode:
return unicode(l)
Expand Down
7 changes: 7 additions & 0 deletions pyamf/adapters/_google_appengine_ext_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pyamf.util import imports
from pyamf.adapters import util


class ModelStub(object):
"""
This class represents a L{db.Model} or L{db.Expando} class as the typed
Expand All @@ -42,6 +43,7 @@ def properties(self):
def dynamic_properties(self):
return []


class GAEReferenceCollection(dict):
"""
This helper class holds a dict of klass to key/objects loaded from the
Expand Down Expand Up @@ -92,6 +94,7 @@ def addClassKey(self, klass, key, obj):

d[key] = obj


class DataStoreClassAlias(pyamf.ClassAlias):
"""
This class contains all the business logic to interact with Google's
Expand Down Expand Up @@ -248,6 +251,7 @@ def applyAttributes(self, obj, attrs, codec=None):
for k, v in attrs.iteritems():
setattr(obj, k, v)


def getGAEObjects(context):
"""
Returns a reference to the C{gae_objects} on the context. If it doesn't
Expand All @@ -264,6 +268,7 @@ def getGAEObjects(context):

return context.gae_objects


def loadInstanceFromDatastore(klass, key, codec=None):
"""
Attempt to load an instance from the datastore, based on C{klass}
Expand Down Expand Up @@ -305,6 +310,7 @@ def loadInstanceFromDatastore(klass, key, codec=None):

return obj


def writeGAEObject(self, object, *args, **kwargs):
"""
The GAE Datastore creates new instances of objects for each get request.
Expand Down Expand Up @@ -343,6 +349,7 @@ def writeGAEObject(self, object, *args, **kwargs):

self.writeNonGAEObject(referenced_object, *args, **kwargs)


def install_gae_reference_model_hook(mod):
"""
Called when L{pyamf.amf0} or L{pyamf.amf3} are imported. Attaches the
Expand Down
10 changes: 6 additions & 4 deletions pyamf/adapters/_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
except Exception, e:
UnmappedInstanceError = e.__class__


class SaMappedClassAlias(pyamf.ClassAlias):
KEY_ATTR = 'sa_key'
LAZY_ATTR = 'sa_lazy'
Expand Down Expand Up @@ -120,7 +121,7 @@ def getAttributes(self, obj, *args, **kwargs):

for attr in dynamic_attr_names:
if attr in obj.__dict__:
dynamic_attrs[attr] = getattr(obj, attr)
dynamic_attrs[attr] = getattr(obj, attr)

static_attrs[self.LAZY_ATTR] = lazy_attrs

Expand All @@ -138,7 +139,7 @@ def applyAttributes(self, obj, attrs, *args, **kwargs):
return

# Delete lazy-loaded attrs.
#
#
# Doing it this way ensures that lazy-loaded attributes are not
# attached to the object, even if there is a default value specified
# in the __init__ method.
Expand All @@ -158,8 +159,8 @@ def applyAttributes(self, obj, attrs, *args, **kwargs):
# SA callbacks are not triggered.
del obj.__dict__[lazy_attr]

# Delete from committed_state so
# SA thinks this attribute was never modified.
# Delete from committed_state so SA thinks this attribute was
# never modified.
#
# If the attribute was set in the __init__ method,
# SA will think it is modified and will try to update
Expand All @@ -185,6 +186,7 @@ def applyAttributes(self, obj, attrs, *args, **kwargs):

pyamf.util.set_attrs(obj, attrs)


def is_class_sa_mapped(klass):
"""
@rtype: C{bool}
Expand Down
4 changes: 4 additions & 0 deletions pyamf/adapters/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
if not hasattr(__builtin__, 'set'):
from sets import Set as set


def to_list(obj, encoder):
"""
Converts an arbitrary object C{obj} to a list.
Expand All @@ -20,6 +21,7 @@ def to_list(obj, encoder):
"""
return list(obj)


def to_dict(obj, encoder):
"""
Converts an arbitrary object C{obj} to a dict.
Expand All @@ -28,6 +30,7 @@ def to_dict(obj, encoder):
"""
return dict(obj)


def to_set(obj, encoder):
"""
Converts an arbitrary object C{obj} to a set.
Expand All @@ -36,6 +39,7 @@ def to_set(obj, encoder):
"""
return set(obj)


def to_tuple(x, encoder):
"""
Converts an arbitrary object C{obj} to a tuple.
Expand Down
6 changes: 5 additions & 1 deletion pyamf/amf0.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
@since: 0.1.0
"""

import datetime, types
import datetime
import types
import copy

import pyamf
Expand Down Expand Up @@ -104,6 +105,7 @@ class Context(pyamf.BaseContext):
L{AMF3<pyamf.amf3>}.
@type amf3_objs: L{util.IndexedCollection}
"""

def __init__(self, **kwargs):
self.amf3_objs = util.IndexedCollection(exceptions=False)

Expand Down Expand Up @@ -810,6 +812,7 @@ def decode(stream, context=None, strict=False):
except pyamf.EOStream:
break


def encode(*args, **kwargs):
"""
A helper function to encode an element into the AMF0 format.
Expand Down Expand Up @@ -901,6 +904,7 @@ def __repr__(self):

pyamf.register_class(RecordSet, 'RecordSet', attrs=['serverInfo'], metadata=['amf0'])


def _check_for_int(x):
"""
This is a compatibility function that takes a C{float} and converts it to an
Expand Down
13 changes: 10 additions & 3 deletions pyamf/amf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
@since: 0.1.0
"""

import types, datetime, zlib
import types
import datetime
import zlib

import pyamf
from pyamf import util
from pyamf.flex import ObjectProxy, ArrayCollection

#: If True encode/decode lists/tuples to ArrayCollections
#: and dicts to ObjectProxy
#: If True encode/decode lists/tuples to L{ArrayCollections<ArrayCollection>}
#: and dicts to L{ObjectProxy}
use_proxies_default = False

try:
Expand Down Expand Up @@ -1202,6 +1204,7 @@ def readByteArray(self):

return obj


class Encoder(pyamf.BaseEncoder):
"""
Encodes an AMF3 data stream.
Expand Down Expand Up @@ -1733,6 +1736,7 @@ def writeXML(self, n, use_references=True, use_proxies=None):

self._writeString(util.ET.tostring(n, 'utf-8'), False)


def decode(stream, context=None, strict=False):
"""
A helper function to decode an AMF3 datastream.
Expand All @@ -1750,6 +1754,7 @@ def decode(stream, context=None, strict=False):
except pyamf.EOStream:
break


def encode(*args, **kwargs):
"""
A helper function to encode an element into AMF3 format.
Expand All @@ -1769,6 +1774,7 @@ def encode(*args, **kwargs):

return buf


def encode_int(n):
"""
@raise OverflowError: Out of range.
Expand Down Expand Up @@ -1803,6 +1809,7 @@ def encode_int(n):

return bytes


def decode_int(stream, signed=False):
n = result = 0
b = stream.read_uchar()
Expand Down
2 changes: 2 additions & 0 deletions pyamf/flex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

__all__ = ['ArrayCollection', 'ObjectProxy']


class ArrayCollection(list):
"""
I represent the ActionScript 3 based class
Expand Down Expand Up @@ -207,6 +208,7 @@ def toArray(self):
pyamf.register_class(ArrayCollection, 'flex.messaging.io.ArrayCollection',
metadata=['external', 'amf3'])


class ObjectProxy(object):
"""
I represent the ActionScript 3 based class C{flex.messaging.io.ObjectProxy}
Expand Down
Loading

0 comments on commit d6f5ec2

Please sign in to comment.