Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Creating Applications with Mozilla already provides a tutorial for making a simple JavaScript or C++ component (implementing the nsISimple interface). Here is how to make the same component in Python using PyXPCOM.

(Note that some details may be missing.)

Preparation

If a binary of PyXPCOM is not available, you will need to build it - see Building PyXPCOM.

Tip: you can achieve a copy of binary of PyXPCOM from PythonExt; simply unpack xpi and take everything you need.

If you wish to use PyXPCOM from a normal Python executable, you will need to tell Python where it can find the PyXPCOM library. This is probably best done by setting a PYTHONPATH variable pointing at the 'bin/python' directory in the application. This is not necessary when your components are loaded by Mozilla as the Python loader modifies sys.path.

Then you can

import xpcom

in any Python module (mostly, in your component).

Defining the interface

Make a file named "nsIPySimple.idl" to define the interface:

#include "nsISupports.idl"
[scriptable, uuid(2b324e9d-a322-44a7-bd6e-0d8c83d94883)]
interface nsIPySimple : nsISupports
{
    attribute string yourName;
    void write( );
    void change(in string aValue);
};

This is the same as the nsISimple interface used here. Theoretically, because several components can share an interface, the same file could be used.

Pay special attention to types here - Python and JavaScript are both loosely-typed, so it's fairly easy to pass information from one to the other.

Note: There are exceptions; see this discussion for information on the use of string and wstring for unicode transfer. See here for info on describing interfaces, and on which types can be used.

Registering the interface

In the "components" directory, execute :

../xpidl -m typelib -w -v -I /usr/share/idl/mozilla/ nsIPySimple.idl

On Windows you must point to the idl directory as part of your Mozilla build. For example:

xpidl.exe -m typelib -w -v -I C:\source\mozilla\obj-i686-pc-mingw32\dist\idl foo.idl

xpidl will then create nsIPySimple.xpt, which should be placed correctly (e.g., in the 'components' directory).

Implementing the component

Unlike C++, PyXPCOM does much of the work for you.

Make a file named "py_simple.py" for the actual code (again, in the 'components' directory):

from xpcom import components, verbose

class PySimple: #PythonTestComponent
    _com_interfaces_ = components.interfaces.nsIPySimple
    _reg_clsid_ = "{c456ceb5-f142-40a8-becc-764911bc8ca5}"
    _reg_contractid_ = "@mozilla.org/PySimple;1"
    def __init__(self):
        self.yourName = "a default name" # or mName ?

    def __del__(self):
        if verbose:
            print "PySimple: __del__ method called - object is destructing"

    def write(self):
        print self.yourName

    def change(self, newName):
        self.yourName = newName

Then register your component; the procedure is the same for any component, but will not work if Python components weren't enabled.

To register the component, touch the .autoreg (a hidden file) in the bin directory, or delete xpti.dat. Then, the next time Mozilla starts, it will rebuild the index of components, including any new one in the 'components' directory. It is helpful to then start Mozilla from the command line to see if new components register successfully.

Generating implementation templates

The module xpcom.xpt is used internally to process type information, but it has a nice feature - it can spit out a template for a Python implementation of any interface.

Just execute this file as a script with the interface name as a param. For example:

% cd c:\mozilla\bin\python\xpcom
% python xpt.py nsISample
class nsISample:
    _com_interfaces_ = xpcom.components.interfaces.nsISample
    # If this object needs to be registered, the following 2 are also needed.
    # _reg_clsid_ = "{a new clsid generated for this object}"
    # _reg_contractid_ = "The.Object.Name"

    def get_value( self ):
        # Result: string
        pass
    def set_value( self, param0 ):
        # Result: void - None
        # In: param0: string
        pass

As you can see, the output is valid Python code, with basic signatures and useful comments for each of the methods.

Testing it

To see this work, you will have to start Firefox from the command line, since that'll be where the stuff will be printed out.

Document Tags and Contributors

 Last updated by: kscarfone,