Class Mapping

Mini-AMF 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.

1
2
3
4
5
6
7
8
class User(object):
    def __init__(self, name, password):
        self.name = name
        self.password = password

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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;
    }
}

Registering Classes

Classes can be registered and removed using the following tools:

  • miniamf.register_class()
  • miniamf.unregister_class()
  • miniamf.get_class_alias()
  • miniamf.register_package()

Continue reading for examples of these APIs.

Single Class

To register a class alias for a single class:

>>> miniamf.register_class("org.miniamf.User", User)

Find the alias registered to the class:

>>> print miniamf.get_class_alias(User)
org.miniamf.User

And to unregister by alias:

>>> miniamf.unregister_class("org.miniamf.User")

Or unregister by class:

>>> miniamf.unregister_class(User)

Multiple Classes

If you want to register multiple classes at the same time, or all classes in a module:

>>> import mymodule
>>> miniamf.register_package(mymodule, 'org.miniamf')

Now all instances of mymodule.User will appear in Actionscript under the alias ‘org.miniamf.User’. Same goes for mymodule.Permission - the Actionscript alias is ‘org.miniamf.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:

>>> miniamf.register_package('org.miniamf')

You can also supply a list of classes to register. An example, taking the example classes:

>>> miniamf.register_package([User, Permission], 'org.miniamf')