"pkgutil" — パッケージ拡張ユーティリティ
****************************************

バージョン 2.3 で追加.

**ソースコード:** Lib/pkgutil.py

======================================================================

このモジュールはインポートシステムの、特にパッケージサポートに関するユ
ーティリティです。

pkgutil.extend_path(path, name)

   パッケージを構成するモジュールの検索パスを拡張します。パッケージの
   "__init__.py" で次のように書くことを意図したものです:

      from pkgutil import extend_path
      __path__ = extend_path(__path__, __name__)

   上記はパッケージの "__path__" に "sys.path" の全ディレクトリのサブ
   ディレクトリとしてパッケージ名と同じ名前を追加します。これは1つの論
   理的なパッケージの異なる部品を複数のディレクトリに分けて配布したい
   ときに役立ちます。

   同時に "*.pkg" の "*" の部分が *name* 引数に指定された文字列に一致
   するファイルの検索もおこないます。この機能は "import" で始まる特別
   な行がないことを除き "*.pth" ファイルに似ています ("site" の項を参
   照)。 "*.pkg" は重複のチェックを除き、信頼できるものとして扱われま
   す。 "*.pkg" ファイルの中に見つかったエントリはファイルシステム上に
   実在するか否かを問わず、そのまますべてパスに追加されます。(このよう
   な仕様です。)

   入力パスがリストでない場合(フリーズされたパッケージのとき)は何もせ
   ずにリターンします。入力パスが変更されていなければ、アイテムを末尾
   に追加しただけのコピーを返します。

   It is assumed that "sys.path" is a sequence.  Items of "sys.path"
   that are not (Unicode or 8-bit) strings referring to existing
   directories are ignored.  Unicode items on "sys.path" that cause
   errors when used as filenames may cause this function to raise an
   exception (in line with "os.path.isdir()" behavior).

class pkgutil.ImpImporter(dirname=None)

   **PEP 302** Importer that wraps Python’s 「classic」 import
   algorithm.

   If *dirname* is a string, a **PEP 302** importer is created that
   searches that directory.  If *dirname* is "None", a **PEP 302**
   importer is created that searches the current "sys.path", plus any
   modules that are frozen or built-in.

   "ImpImporter" は現在のところ "sys.meta_path" に配置しての利用をサポ
   ートしていないことに注意してください。

class pkgutil.ImpLoader(fullname, file, filename, etc)

   **PEP 302** Loader that wraps Python’s 「classic」 import
   algorithm.

pkgutil.find_loader(fullname)

   Find a **PEP 302** 「loader」 object for *fullname*.

   If *fullname* contains dots, path must be the containing package’s
   "__path__".  Returns "None" if the module cannot be found or
   imported. This function uses "iter_importers()", and is thus
   subject to the same limitations regarding platform-specific special
   import locations such as the Windows registry.

pkgutil.get_importer(path_item)

   Retrieve a **PEP 302** importer for the given *path_item*.

   The returned importer is cached in "sys.path_importer_cache" if it
   was newly created by a path hook.

   If there is no importer, a wrapper around the basic import
   machinery is returned.  This wrapper is never inserted into the
   importer cache ("None" is inserted instead).

   キャッシュ (やその一部) は、 "sys.path_hooks" のリスキャンが必要に
   なった場合は手動でクリアすることができます。

pkgutil.get_loader(module_or_name)

   Get a **PEP 302** 「loader」 object for *module_or_name*.

   module か package が通常の import 機構によってアクセスできる場合、
   その機構の該当部分に対するラッパーを返します。モジュールが見つから
   なかったり import できない場合は "None" を返します。その名前のモジ
   ュールがまだ import されていない場合、そのモジュールを含むパッケー
   ジが(あれば)そのパッケージの "__path__" を確立するために import さ
   れます。

   This function uses "iter_importers()", and is thus subject to the
   same limitations regarding platform-specific special import
   locations such as the Windows registry.

pkgutil.iter_importers(fullname='')

   Yield **PEP 302** importers for the given module name.

   If fullname contains a 『.』, the importers will be for the package
   containing fullname, otherwise they will be importers for
   "sys.meta_path", "sys.path", and Python’s 「classic」 import
   machinery, in that order.  If the named module is in a package,
   that package is imported as a side effect of invoking this
   function.

   Non-**PEP 302** mechanisms (e.g. the Windows registry) used by the
   standard import machinery to find files in alternative locations
   are partially supported, but are searched *after* "sys.path".
   Normally, these locations are searched *before* "sys.path",
   preventing "sys.path" entries from shadowing them.

   For this to cause a visible difference in behaviour, there must be
   a module or package name that is accessible via both "sys.path" and
   one of the non-**PEP 302** file system mechanisms.  In this case,
   the emulation will find the former version, while the builtin
   import mechanism will find the latter.

   Items of the following types can be affected by this discrepancy:
   "imp.C_EXTENSION", "imp.PY_SOURCE", "imp.PY_COMPILED",
   "imp.PKG_DIRECTORY".

pkgutil.iter_modules(path=None, prefix='')

   Yields "(module_loader, name, ispkg)" for all submodules on *path*,
   or, if path is "None", all top-level modules on "sys.path".

   *path* は "None" か、モジュールを検索する path のリストのどちらかで
   なければなりません。

   *prefix* は出力の全てのモジュール名の頭に出力する文字列です。

pkgutil.walk_packages(path=None, prefix='', onerror=None)

   Yields "(module_loader, name, ispkg)" for all modules recursively
   on *path*, or, if path is "None", all accessible modules.

   *path* は "None" か、モジュールを検索する path のリストのどちらかで
   なければなりません。

   *prefix* は出力の全てのモジュール名の頭に出力する文字列です。

   この関数は与えられた *path* 上の全ての *パッケージ* (全てのモジュー
   ル *ではない*) を、サブモジュールを検索するのに必要な "__path__" 属
   性にアクセスするために import します。

   *onerror* は、パッケージを import しようとしたときに何かの例外が発
   生した場合に、 1つの引数 (import しようとしていたパッケージの名前)
   で呼び出される関数です。 *onerror* 関数が提供されない場合、
   "ImportError" は補足され無視されます。それ以外の全ての例外は伝播し
   、検索を停止させます。

   例:

      # list all modules python can access
      walk_packages()

      # list all submodules of ctypes
      walk_packages(ctypes.__path__, ctypes.__name__ + '.')

pkgutil.get_data(package, resource)

   パッケージからリソースを取得します。

   This is a wrapper for the **PEP 302** loader "get_data()" API.  The
   *package* argument should be the name of a package, in standard
   module format ("foo.bar").  The *resource* argument should be in
   the form of a relative filename, using "/" as the path separator.
   The parent directory name ".." is not allowed, and nor is a rooted
   name (starting with a "/").

   この関数が返すのは指定されたリソースの内容であるバイナリ文字列です
   。

   ファイルシステム中に位置するパッケージで既にインポートされているも
   のに対しては、次と大体同じです:

      d = os.path.dirname(sys.modules[package].__file__)
      data = open(os.path.join(d, resource), 'rb').read()

   If the package cannot be located or loaded, or it uses a **PEP
   302** loader which does not support "get_data()", then "None" is
   returned.

   バージョン 2.6 で追加.
