用PYTHON编写XPCOM组件的方法

Compiling

First you need to pull the source:

cvs -z3 -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co -r DOM_AGNOSTIC2_BRANCH mozilla/client.mk
cd mozilla
gmake -f client.mk pull_all BUILD_MODULES=xpcom MOZ_CO_MODULE=mozilla/extensions/python/xpcom,mozilla/extensions/Makefile.in

(note that I'm using the DOM_AGNOSTIC2 branch; at the time of writing (2005-12-14), the pyxpcom module in the trunk fails to build)

Then, you build it.

./configure --enable-application=standalone --enable-standalone-modules=xpcom --enable-extensions=python/xpcom
make 
make BUILD_MODULES=all tier_42_dirs=extensions tier_42

If everything goes well, you can try a few tests:

cd dist/bin
export LD_LIBRARY_PATH=.
export PYTHONPATH=python
./regxpcom
python python/xpcom/tools/tracer_demo.py
[ edit]

Browser integration

At this point you're ready to play with the basic PyXPCOM. However, most XPCOM components will be unavailable, since you've only built the standalone xpcom libraries. For serious work, you'll probably want to integrate PyXPCOM into the browser.

I've tested adding PyXPCOM from the latest DOM_AGNOSTIC2_BRANCH (2005-12-14) to Firefox 1.5 without problems. I assume it will also work with Mozilla Suite.

I've copied the following files from dist/bin to the Firefox directory (preserving their relative paths):

  • libpyxpcom.so
  • components/libpyloader.so
  • python/*

Then you need to convince Firefox to rescan the components. For me, this meant removing compreg.dat and xpti.dat from my profile. If you then start Firefox from the commandline, it should report "Registering 'moz.pyloader.1' (libpyloader.so)".

[ edit]

Testing

Here's a sample component I wrote for a quick test. I've placed it in $(firefox)/components/pyabout.py

from xpcom import components, verbose

class AboutPython:
        _com_interfaces_ = components.interfaces.nsIAboutModule
        _reg_contractid_ = '@mozilla.org/network/protocol/about;1?what=python'
        _reg_clsid_ = '{6d5d462e-6de7-4bca-bbc6-c488d481351b}'
        _reg_desc_ = "about:python handler"

        def __init__(self):
                pass

        def newChannel(self, aURI):
                ioService = components.classes["@mozilla.org/network/io-service;1"] /
                        .getService(components.interfaces.nsIIOService);
                uri = ioService.newURI("http://www.python.org/", "utf-8", aURI)
                return ioService.newChannelFromURI(uri)
 

See above on how to refresh the component database. If it works, typing "about:python" in the address bar should load python.org.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值