前回に引き続き。
つまり、自分でプラグインを実装するときも、IRequestHandler インタフェースを実装したクラスで、match_requestメソッド で自分が処理できるリクエストかどうか判定し、process_requestメソッドでリクエストを処理してやればいい、ということ。
class HelloWorldPlugin(Component):
implements(IRequestHandler)
# IRequestHandler methods
def match_request(self, req):
return req.path_info == '/helloworld'
def process_request(self, req):
content = 'Hello World!'
req.send_response(200)
req.send_header('Content-Type', 'text/plain')
req.send_header('Content-Length', len(content))
req.end_headers()
req.write(content)赤線がついた Component, implements, IRequestHandler はコンパイルエラーになってるところなので、それぞれ Qiuck Fix (ctrl-1) で適切に import文を追加してやればOK。
そうそう。HTTPレスポンスの書き方は知ってると思うけど念のため。リクエストに対して、以下のようなテキストを返してやれば、Webブラウザが解釈して表示してくれる:
200 OK
Content-Type: text/plain
Content-Length: 0
Hello, World!
Ctrl-N で PyDEV Module を、無名パッケージ、モジュール名 setup で用意して、こんな感じに書く:
from setuptools import find_packages, setup
setup(
name='TracHelloWorld', version='1.0',
packages=find_packages(exclude=['*.tests*']),
entry_points = {
'trac.plugins': [
'helloworld = myplugs.helloworld',
],
},
)
作った setup.py を 引数 egg_info をつけて実行する。ええと、trac起動スクリプトを実行した時と同じように、まず setup.py を選択して Run - Run as で一回実行して(エラーになる)、Run - Run Configurations... から、Arguments に、egg_info を追加して run しておく。
setup.py と同じフォルダに、TracHelloWorld.egg-info フォルダができればOK。

いま Console View は setup.py の実行結果が出ているので、tracdのコンソールに切り替えて、再起動ボタンを押してリスタートしてやる。すると、tracの 管理 - プラグイン設定 で myplugs.helloworld が出てくるので、Enable - Apply Changed して有効にしてやる。
ブラウザから、http://localhost:8080/1.0/helloworld へアクセスすると、しれっと「Hello World」と表示される。
あとは、ソースを編集 - Tracdを再起動 - 動作確認 を繰り返してやればいい。再起動が面倒だったら、tracd に --auto-reload をつけると手間を省ける。