Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.
Création d'applications avec Mozilla prévoit déjà un tutoriel pour faire un composant simple JavaScript ou C (mise en œuvre de l'Interface nsISimple
) . Voici comment faire le même composant en Python en utilisant PyXPCOM .
(Notez que certains détails peuvent être manquants.)
Préparation
Si un binaire de PyXPCOM n'est pas disponibles, vous aurez besoin de le construire - voir Compilation de PyXPCOM .
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.
External links
- Creating XPCOM components, on which this short tutorial is based.
- A three-part tutorial on ibm developerWorks:
- Getting to know PyXPCOM - info on building PyXPCOM (and maybe Mozilla) to get it to work.
- Getting started with PyXPCOM, part 2 - accessing xpcom from Python.
- Getting started with PyXPCOM, part 3 - Creating your own components. The problem with this one is that the sample code they give is slightly broken (it ends with a "retu" which shouldn't be there. I haven't tried it, but it looks like nothing is missing, so getting rid of the "retu" should make it work fine.).