1. pyamf — pyamf

PyAMF provides Action Message Format (AMF) support for Python that is compatible with the Adobe Flash Player.

Copyright:Copyright (c) 2007-2010 The PyAMF Project. All Rights Reserved.
Contact:users@pyamf.org
See:http://pyamf.org
Since:October 2007
Status:Production/Stable
pyamf.register_class(klass, alias=None)

Registers a class to be used in the data streaming.

Returns:The registered ClassAlias.
pyamf.register_class_loader(loader)

Registers a loader that is called to provide the Class for a specific alias.

The loader is provided with one argument, the Class alias. If the loader succeeds in finding a suitable class then it should return that class, otherwise it should return None.

Raises TypeError:
 The loader is not callable.
Raises ValueError:
 The loader is already registered.
pyamf.encode(*args, **kwargs)

A helper function to encode an element.

Parameters:
Return type:

StringIO

Returns:

File-like object.

pyamf.decode(*args, **kwargs)

A generator function to decode a datastream.

Parameters:
  • stream (BufferedByteStream) – AMF data.
  • encoding (int) – AMF encoding type.
  • context (AMF0 Context or AMF3 Context) – Context.
Returns:

Each element in the stream.

pyamf.version
PyAMF version number.
pyamf.get_class_alias(klass)

Finds the alias registered to the class.

Returns:The class alias linked to klass.
Return type:ClassAlias
Raises UnknownClassAlias:
 Class not found.
pyamf.unregister_class(alias)

Deletes a class from the cache.

If alias is a class, the matching alias is found.

Parameter:alias (class or str) – Alias for class to delete.
Raises UnknownClassAlias:
 Unknown alias.
pyamf.register_alias_type(klass, *args)

This function allows you to map subclasses of ClassAlias to classes listed in args.

When an object is read/written from/to the AMF stream, a paired ClassAlias instance is created (or reused), based on the Python class of that object. ClassAlias provides important metadata for the class and can also control how the equivalent Python object is created, how the attributes are applied etc.

Use this function if you need to do something non-standard.

See:pyamf.adapters._google_appengine_ext_db.DataStoreClassAlias for a good example.
Since:0.4
Raises RuntimeError:
 Type is already registered.
Raises TypeError:
 klass must be a class.
Raises ValueError:
 New aliases must subclass pyamf.ClassAlias.
Raises ValueError:
 At least one type must be supplied.
pyamf.unregister_alias_type(klass)

Removes the klass from the ALIAS_TYPE register.

See:register_alias_type()
pyamf.unregister_class_loader(loader)

Unregisters a class loader.

Parameter:loader (callable) – The object to be unregistered
Raises LookupError:
 The loader was not registered.
pyamf.add_error_class(klass, code)

Maps an exception class to a string code. Used to map remoting onStatus objects to an exception class so that an exception can be built to represent that error.

Raises TypeError:
 klass must be a class type.
Raises TypeError:
 Error classes must subclass the __builtin__.Exception class.
Raises ValueError:
 Code is already registered.

An example:

>>> class AuthenticationError(Exception):
...     pass
...
>>> pyamf.add_error_class(AuthenticationError, 'Auth.Failed')
>>> print pyamf.ERROR_CLASS_MAP
{'TypeError': <type 'exceptions.TypeError'>, 'IndexError': <type 'exceptions.IndexError'>,
'Auth.Failed': <class '__main__.AuthenticationError'>, 'KeyError': <type 'exceptions.KeyError'>,
'NameError': <type 'exceptions.NameError'>, 'LookupError': <type 'exceptions.LookupError'>}
pyamf.remove_error_class(klass)

Removes a class from ERROR_CLASS_MAP.

Raises ValueError:
 Code is not registered.
Raises ValueError:
 Class is not registered.
Raises TypeError:
 Invalid type, expected class or string.

An example:

>>> class AuthenticationError(Exception):
...     pass
...
>>> pyamf.add_error_class(AuthenticationError, 'Auth.Failed')
>>> pyamf.remove_error_class(AuthenticationError)
pyamf.add_type(type_, func=None)

Adds a custom type to TYPE_MAP. A custom type allows fine grain control of what to encode to an AMF data stream.

Raises TypeError:
 Unable to add as a custom type (expected a class or callable).
Raises KeyError:
 Type already exists.
pyamf.remove_type(type_)

Removes the custom type declaration.

Returns:Custom type declaration.
pyamf.register_package(module=None, package=None, separator='.', ignore=[], strict=True)

This is a helper function that takes the concept of Actionscript packages and registers all the classes in the supplied Python module under that package. It auto-aliased all classes in module based on package.

Parameters:
  • module (module or dict) – The Python module that will contain all the classes to auto alias.
  • package (str or unicode or None) – The base package name. e.g. ‘com.example.app’. If this is None then the value is inferred from module.__name__.
  • separator (str) – The separator used to append to package to form the complete alias.
  • ignore (iterable) – To give fine grain control over what gets aliased and what doesn’t, supply a list of classes that you do not want to be aliased.
  • strict (bool) – If this value is True then only classes that originate from module will be registered, all others will be left in peace.
Raises TypeError:
 

Cannot get list of classes from module

Returns:

A collection of all the classes that were registered and their respective ClassAlias objects.

Since:

0.5

pyamf.AMF0
Specifies that objects are serialized using AMF for ActionScript 1.0 and 2.0 that were introduced in the Adobe Flash Player 6.
pyamf.AMF3
Specifies that objects are serialized using AMF for ActionScript 3.0 that was introduced in the Adobe Flash Player 9.
pyamf.DEFAULT_ENCODING
Default encoding
pyamf.version

PyAMF version number.

>>> pyamf.__version__ is pyamf.version
True
>>> pyamf.version
(0, 6, 1, 'rc1')
>>> str(pyamf.version)
'0.6.1rc1'
See:pyamf.versions

1.1. Type Maps

pyamf.CLASS_CACHE

Class mapping support.

>>> pyamf.CLASS_CACHE
{<class 'pyamf.ASObject'>: <ClassAlias alias= class=<class 'pyamf.ASObject'> @ 0x100568a50>}
See:register_class(), unregister_class() and register_package()
pyamf.ALIAS_TYPES

Alias mapping support

>>> pyamf.ALIAS_TYPES
{<class 'pyamf.TypedObjectClassAlias'>: (<class 'pyamf.TypedObject'>,),
 <class 'pyamf.ErrorAlias'>: (<type 'exceptions.Exception'>,)}
See:get_class_alias(), register_alias_type() and unregister_alias_type()
pyamf.ERROR_CLASS_MAP

Maps error classes to string codes.

>>> pyamf.ERROR_CLASS_MAP
{'LookupError': <type 'exceptions.LookupError'>, 'NameError': <type 'exceptions.NameError'>,
 'TypeError': <type 'exceptions.TypeError'>, 'IndexError': <type 'exceptions.IndexError'>,
 'KeyError': <type 'exceptions.KeyError'>}
See:add_error_class() and remove_error_class()
pyamf.TYPE_MAP

Custom type map.

>>> pyamf.TYPE_MAP
{<type 'array.array'>: <function to_list at 0x101345ed8>,
 <type 'collections.deque'>: <function to_list at 0x101345ed8>,
 <type 'collections.defaultdict'>: <function to_dict at 0x101349b18>}
See:get_type(), add_type() and remove_type()
pyamf.CLASS_LOADERS

Class loaders.

>>> pyamf.CLASS_LOADERS
[<function flex_loader at 0x100569e60>, <function blaze_loader at 0x100569de8>]
See:register_class_loader() and unregister_class_loader()
pyamf.ENCODING_TYPES

Supported AMF encoding types.

>>> pyamf.ENCODING_TYPES
(0, 3)
See:AMF0, AMF3, and DEFAULT_ENCODING

1.2. Exception Types

exception pyamf.BaseError

Base AMF Error.

All AMF related errors should be subclassed from this class.

exception pyamf.DecodeError
Raised if there is an error in decoding an AMF data stream.
exception pyamf.EOStream
Raised if the data stream has come to a natural end.
exception pyamf.ReferenceError
Raised if an AMF data stream refers to a non-existent object or string reference.
exception pyamf.EncodeError

Raised if the element could not be encoded to the stream.

Bug:See Docuverse blog (external) for more info about the empty key string array bug.
exception pyamf.ClassAliasError
Generic error for anything class alias related.
exception pyamf.UnknownClassAlias

Raised if the AMF stream specifies an Actionscript class that does not have a Python class alias.

See:register_class()

1.3. Other Types

pyamf.Undefined
class pyamf.ClassAlias(klass, alias=None, **kwargs)

Class alias. Provides class/instance meta data to the En/Decoder to allow fine grain control and some performance increases.

Variable bases:A list of (class, alias) for all bases of this alias.