Package pyamf
[hide private]
[frames] | no frames]

Package pyamf

source code

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


Since: October 2007

Status: Production/Stable

Submodules [hide private]

Classes [hide private]
  ASObject
Represents a Flash Actionscript Object (typed or untyped).
  BaseError
Base AMF Error.
  DecodeError
Raised if there is an error in decoding an AMF data stream.
  EOStream
Raised if the data stream has come to a natural end.
  EncodeError
Raised if the element could not be encoded to AMF.
  ErrorAlias
Adapts Python exception objects to Adobe Flash Player error objects.
  MixedArray
Used to be able to specify the mixedarray type.
  ReferenceError
Raised if an AMF data stream refers to a non-existent object or string reference (in the case of AMF3).
  TypedObject
This class is used when a strongly typed object is decoded but there is no registered class to apply it to.
  TypedObjectClassAlias
The meta class for TypedObject used to adapt PyAMF.
  UndefinedType
Represents the undefined value in the Adobe Flash Player client.
Functions [hide private]
 
add_error_class(klass, code)
Maps an exception class to a string code.
source code
 
add_type(type_, func=None)
Adds a custom type to TYPE_MAP.
source code
 
blaze_loader(alias)
Loader for BlazeDS framework compatibility classes, specifically implementing ISmallMessage.
source code
 
decode(stream, *args, **kwargs)
A generator function to decode a datastream.
source code
 
encode(*args, **kwargs)
A helper function to encode an element.
source code
 
flex_loader(alias)
Loader for Flex framework compatibility classes.
source code
 
get_class_alias(klass_or_alias)
Finds the ClassAlias that is registered to klass_or_alias.
source code
 
get_decoder(encoding, *args, **kwargs)
Returns a codec.Decoder capable of decoding AMF[encoding] streams.
source code
 
get_encoder(encoding, *args, **kwargs)
Returns a codec.Encoder capable of encoding AMF[encoding] streams.
source code
 
get_type(type_)
Gets the declaration for the corresponding custom type.
source code
classobj
load_class(alias)
Finds the class registered to the alias.
source code
 
register_alias_type(klass, *args)
This function allows you to map subclasses of ClassAlias to classes listed in args.
source code
 
register_class(klass, alias=None)
Registers a class to be used in the data streaming.
source code
 
register_class_loader(loader)
Registers a loader that is called to provide the class for a specific alias.
source code
 
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.
source code
 
remove_error_class(klass)
Removes a class from the ERROR_CLASS_MAP.
source code
 
remove_type(type_)
Removes the custom type declaration.
source code
 
set_default_etree(etree)
Sets the default interface that will called apon to both de/serialise XML entities.
source code
 
unregister_alias_type(klass)
Removes the klass from the ALIAS_TYPES register.
source code
 
unregister_class(alias)
Opposite of register_class.
source code
 
unregister_class_loader(loader)
Unregisters a class loader.
source code
Variables [hide private]
  ALIAS_TYPES = {<class 'pyamf.TypedObjectClassAlias'>: (<class ...
  AMF0 = 0
  AMF3 = 3
  CLASS_CACHE = {<class 'pyamf.ASObject'>: <pyamf.alias.ClassAli...
  CLASS_LOADERS = set()
  DEFAULT_ENCODING = 3
  ENCODING_TYPES = (0, 3)
  ERROR_CLASS_MAP = {'IndexError': <type 'exceptions.IndexError'...
  TYPE_MAP = {}
  Undefined = pyamf.Undefined
  __package__ = 'pyamf'
  version = (0, 6, 1)

Imports: util, ClassAlias, UnknownClassAlias, _version, adapters, alias, inspect, python, register_adapters, types, versions


Function Details [hide private]

add_error_class(klass, code)

source 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.

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'>}
Parameters:
  • klass - Exception class
  • code (str) - Exception code

See Also: remove_error_class

add_type(type_, func=None)

source code 

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).
  • KeyError - Type already exists.

See Also: get_type and remove_type

blaze_loader(alias)

source code 

Loader for BlazeDS framework compatibility classes, specifically implementing ISmallMessage.

See Also: BlazeDS

Since: 0.5

decode(stream, *args, **kwargs)

source code 

A generator function to decode a datastream.

Parameters:
  • stream (byte data.) - AMF data to be decoded.
  • encoding - AMF encoding type. One of ENCODING_TYPES.
Returns:
A generator that will decode each element in the stream.

encode(*args, **kwargs)

source code 

A helper function to encode an element.

Parameters:
  • args - The python data to be encoded.
  • encoding - AMF encoding type. One of ENCODING_TYPES.
Returns:
A util.BufferedByteStream object that contains the data.

flex_loader(alias)

source code 

Loader for Flex framework compatibility classes.

Raises:

get_class_alias(klass_or_alias)

source code 

Finds the ClassAlias that is registered to klass_or_alias.

If a string is supplied and no related ClassAlias is found, the alias is loaded via load_class.

Raises:

get_decoder(encoding, *args, **kwargs)

source code 

Returns a codec.Decoder capable of decoding AMF[encoding] streams.

Raises:
  • ValueError - Unknown encoding.

get_encoder(encoding, *args, **kwargs)

source code 

Returns a codec.Encoder capable of encoding AMF[encoding] streams.

Raises:
  • ValueError - Unknown encoding.

get_type(type_)

source code 

Gets the declaration for the corresponding custom type.

Raises:
  • KeyError - Unknown type.

See Also: add_type and remove_type

load_class(alias)

source code 

Finds the class registered to the alias.

The search is done in order:

  1. Checks if the class name has been registered via register_class or register_package.
  2. Checks all functions registered via register_class_loader.
  3. Attempts to load the class via standard module loading techniques.
Parameters:
  • alias (string) - The class name.
Returns: classobj
Class registered to the alias.
Raises:

register_alias_type(klass, *args)

source code 

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.

Raises:
  • RuntimeError - alias is already registered
  • TypeError - Value supplied to klass is not a class
  • ValueError -

register_class(klass, alias=None)

source code 

Registers a class to be used in the data streaming. This is the equivalent to the [RemoteClass(alias="foobar")] AS3 metatag.

Returns:
The registered ClassAlias instance.

See Also: unregister_class

register_class_loader(loader)

source code 

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

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

An example:

   def lazy_load_from_my_module(alias):
       if not alias.startswith('foo.bar.'):
           return None

       from foo import bar

       if alias == 'foo.bar.Spam':
           return bar.Spam
       elif alias == 'foo.bar.Eggs':
           return bar.Eggs

   pyamf.register_class_loader(lazy_load_from_my_module)
Raises:
  • TypeError - loader must be callable

register_package(module=None, package=None, separator='.', ignore=[], strict=True)

source code 

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 the parent package.

Parameters:
  • module (module or dict) - The Python module that will contain all the classes to auto alias.
  • package (string or None) - The base package name. e.g. 'com.example.app'. If this is None then the value is inferred from module.__name__.
  • separator - 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 - Whether only classes that originate from module will be registered.
Returns:
A dict of all the classes that were registered and their respective ClassAlias counterparts.
Raises:
  • TypeError - Cannot get a list of classes from module

Since: 0.5

remove_error_class(klass)

source code 

Removes a class from the ERROR_CLASS_MAP.

An example:

  >>> class AuthenticationError(Exception):
  ...     pass
  ...
  >>> pyamf.add_error_class(AuthenticationError, 'Auth.Failed')
  >>> pyamf.remove_error_class(AuthenticationError)

See Also: add_error_class

remove_type(type_)

source code 

Removes the custom type declaration.

Returns:
Custom type declaration.

See Also: add_type and get_type

set_default_etree(etree)

source code 

Sets the default interface that will called apon to both de/serialise XML entities. This means providing both tostring and fromstring functions.

For testing purposes, will return the previous value for this (if any).

unregister_alias_type(klass)

source code 

Removes the klass from the ALIAS_TYPES register.

unregister_class(alias)

source code 

Opposite of register_class.

Raises:

unregister_class_loader(loader)

source code 

Unregisters a class loader.

Parameters:
  • loader - The class loader to be unregistered.
Raises:
  • LookupError - The loader was not registered.

Variables Details [hide private]

ALIAS_TYPES

Value:
{<class 'pyamf.TypedObjectClassAlias'>: (<class 'pyamf.TypedObject'>),
 <class 'pyamf.ErrorAlias'>: (<type 'exceptions.Exception'>),
 <class 'pyamf.adapters._sqlalchemy_orm.SaMappedClassAlias'>: (<functi\
on is_class_sa_mapped at 0x101ec4050>)}

CLASS_CACHE

Value:
{<class 'pyamf.ASObject'>: <pyamf.alias.ClassAlias alias='' class=<cla\
ss 'pyamf.ASObject'> @ 0x101239a90>,
 <class 'pyamf.amf0.RecordSet'>: <pyamf.alias.ClassAlias alias=u'Recor\
dSet' class=<class 'pyamf.amf0.RecordSet'> @ 0x10164b250>,
 <class 'pyamf.amf3.ByteArray'>: <pyamf.alias.ClassAlias alias='' clas\
s=<class 'pyamf.amf3.ByteArray'> @ 0x10166ca50>,
 <class 'pyamf.remoting.ErrorFault'>: <pyamf.alias.ClassAlias alias=''\
 class=<class 'pyamf.remoting.ErrorFault'> @ 0x10212f410>,
...

ERROR_CLASS_MAP

Value:
{'IndexError': <type 'exceptions.IndexError'>,
 'KeyError': <type 'exceptions.KeyError'>,
 'LookupError': <type 'exceptions.LookupError'>,
 'NameError': <type 'exceptions.NameError'>,
 'Server.Call.Failed': <class 'pyamf.remoting.RemotingCallFailed'>,
 'TypeError': <type 'exceptions.TypeError'>,
 'ValueError': <type 'exceptions.ValueError'>}