|
PyAMF allows you to register aliases for remote Python classes that can be mapped to their corresponding Actionscript classes.
In this example we use the Python classes below.
class User(object):
def __init__(self, name, pass):
self.name = name
self.pass = pass
class Permission(object):
def __init__(self, type):
self.type = type
With the corresponding Actionscript 3.0 classes that were registered in the Flash Player using the flash.net.registerClassAlias utility:
public class User
{
public var name:String;
public var pass:String;
public function User(name:String, pass:String)
{
this.name = name;
this.pass = pass
}
}
public class Permission
{
public var type:String;
public function Permission(type:String)
{
this.type = type;
}
}
Classes can be registered and removed using the following tools:
Continue reading for examples of these APIs.
To register a class alias for a single class:
>>> pyamf.register_class("org.pyamf.User", User)
Find the alias registered to the class:
>>> print pyamf.get_class_alias(User)
org.pyamf.User
And to unregister by alias:
>>> pyamf.unregister_class("org.pyamf.User")
Or unregister by class:
>>> pyamf.unregister_class(User)
If you want to register multiple classes at the same time, or all classes in a module:
>>> import mymodule
>>> pyamf.register_package(mymodule, 'org.pyamf')
Now all instances of mymodule.User will appear in Actionscript under the alias ‘org.pyamf.User’. Same goes for mymodule.Permission - the Actionscript alias is ‘org.pyamf.Permission’. The reverse is also true, any objects with the correct aliases will now be instances of the relevant Python class.
This function respects the __all__ attribute of the module but you can have further control of what not to auto alias by populating the ignore argument with a list of classes that should be ignored.
This function provides the ability to register the module it is being called in, an example:
>>> pyamf.register_package('org.pyamf')
You can also supply a list of classes to register. An example, taking the example classes:
>>> pyamf.register_package([User, Permission], 'org.pyamf')
Python 2.6 introduced class decorators that help you to avoid writing most of the boilerplate code that is used when registering classes in Python. An example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import pyamf
@RemoteClass(alias="model.MyClass")
class MyClass:
def __init__(self, *args, **kwargs):
self.a = args[0]
self.b = args[1]
class RemoteClass(object):
def __init__(self, alias):
self.alias = alias
def __call__(self, klass):
pyamf.register_class(klass, self.alias)
return klass
|