「翻訳ドキュメント/WritingPlugins」の編集履歴(バックアップ)一覧に戻る

翻訳ドキュメント/WritingPlugins - (2009/02/11 (水) 18:18:46) の編集履歴(バックアップ)


このページは Bazaar 公式 Using Bazaar WritingPlugins の日本語訳です。




( Bazaar 開発者ドキュメントの Plugin API も参照してください)

プラグインを作成する

プラグインは、bzr のコード変更なしに bzr の機能の拡張、または bzr の振る舞いの置き換えをすることができます。プラグインを書くことは、新しいコマンドを作成したり現在のコマンドの振る舞いを置き換えるための推奨されている方法です。

通常、プラグインは特定のユーザの ~/.bazaar/plugins/ にインストールされています。
これらのプラグインはたいがいシンプルな Python モジュール ( 例. foo.py ) か、Python パッケージ ( ディレクトリの中に __init__.py ファイルが含まれている ) です。
一般に、私たちはそれらがディレクトリであることをお勧めしています、そしてあなたのプラグインのバージョンコントロールをすることができます ( もちろん Bazaar を使用して ) :

cd ~/.bazaar
mkdir plugins
bzr init foo
cd foo
touch __init__.py

新しいコマンドを定義する

Bzr コマンドは、bzrlib.commands.Command クラスのサブクラスとして定義されます。コマンド名はサブクラス名によって指定されます。そしてそれらをインポートするときに、bzrlib.commands.register_command を使用して bzr に登録しなければなりません。

foo-bar コマンドを bzr に定義する:
from bzrlib.commands import Command, register_command

class cmd_foo_bar(Command):
  # see bzrlib/builtins.py for information about what to put here
  pass

register_command(cmd_foo_bar)

もしクラス名の
If the class name starts with cmd, the prefix will get dropped and _ will be replaced by - characters.

コマンドクラスの属性

Base class for commands. Commands are the heart of the command-line bzr interface.

The command object mostly handles the mapping of command-line parameters into one or more bzrlib operations, and of the results into textual output.

Commands normally don't have any state. All their arguments are passed in to the run method. (Subclasses may take a different policy if the behaviour of the instance needs to depend on e.g. a shell plugin and not just its Python class.)

The docstring for an actual command should give a single-line summary, then a complete description of the command. A grammar description will be inserted.

aliases
   Other accepted names for this command.
takes_args

   List of argument forms, marked with whether they are optional, repeated, etc.

   For example: ['to_location', 'from_branch?', 'file*'] means:

       * 'to_location' is required
       * 'from_branch' is optional
       * 'file' can be specified 0 or more times

takes_options
   List of options that may be given for this command. These can be either strings, referring to globally-defined options, or option objects. Retrieve through options().
hidden
   If true, this command isn't advertised. This is typically for commands intended for expert users.

Command.run メソッド

Actually run the command.

This is invoked with the options and arguments bound to keyword parameters.

Return 0 or None if the command was successful, or a non-zero shell error code if not. It's OK for this method to allow an exception to raise up.

register_command 機能

Utility function to help register a command.

param cmd
   Command subclass to register
param decorate
   If true, allow overriding an existing command of the same name; the old command is returned by this function. Otherwise it is an error to try to override an existing command.

プラグインのテスト

When you run bzr selftest, Bazaar will scan all its plugins to see if they contain a function named test_suite(). For each plugin that does, it calls the function and adds any resulting tests to the master test suite.

プラグインと起動時間

bzr は起動時にすべてのプラグインをインポートしますので、使われていないプラグインがあると性能を下げてしまいます。しかしながらサブモジュールはロードされず、main name だけがロードされます。

このスローダウンを回避する一つの方法は、コードの大部分をサブモジュールに入れてしまい、プラグイン自体を小さくしてしまうことです。

One way you can avoid this slowdown is by putting most of your code in sub-modules, so that the plugin, itself, is small. All you really need in the __init__.py is the plugin's Command classes, the commands to register them, and the optional test_suite().

Another way to reduce your plugin's overhead is to use the bzrlib lazy_import functionality. That looks something like this:

from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
from bzrlib import (
    branch as _mod_branch,
    option,
    workingtree,
    )
""")

Lazy importing only works for packages and modules, not classes or functions. It defers the import until you actually need it.

進んだ設定

The directory where user plugins are looked for (~/.bazaar/plugins, by default) can be overridden by setting the BZR_PLUGIN_PATH environment variable.

Plugins can be installed system wide in bzrlib/plugins directory of the bzr installation.

コールバックでプラグインの機能を起動する

There are many hooks available from Branch as of bzr 0.15. Adding a new hook is done with:

import bzrlib.branch
bzrlib.branch.Branch.hooks.install_hook(HOOK_NAME, MY_FUNCTION)

現在の Hooks


さらなる情報

Integrating_with_Bazaar explains how to do such operations as add, commit, log and more.
人気記事ランキング
目安箱バナー