"mimetypes" --- ファイル名を MIME タイプへマップする
****************************************************

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

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

"mimetypes" モジュールは、ファイル名あるいは URL と、ファイル名拡張子
に関連付けられた MIME 型とを変換します。ファイル名から MIME 型へと、
MIME 型からファイル名拡張子への変換が提供されます; 後者の変換では符号
化方式はサポートされていません。

このモジュールは、一つのクラスと多くの便利な関数を提供します。これらの
関数がこのモジュールへの標準のインターフェースですが、アプリケーション
によっては、そのクラスにも関係するかもしれません。

以下で説明されている関数は、このモジュールへの主要なインターフェースを
提供します。たとえモジュールが初期化されていなくても、もしこれらの関数
が、 "init()" がセットアップする情報に依存していれば、これらの関数は、
"init()" を呼びます。

mimetypes.guess_type(url, strict=True)

   *url* で与えられるファイル名あるいは URL に基づいて、ファイルの型を
   推定します。URL は文字列または *path-like object* です。

   戻り値は、タプル "(type, encoding)" です、ここで  *type* は、もし型
   が(拡張子がないあるいは未定義のため)推定できない場合は、 "None" を
   、あるいは、 MIME *content-type* ヘッダ  に利用できる、
   "'type/subtype'" の形の文字列です。

   *encoding* は、符合化方式がない場合は "None" を、あるいは、符号化に
   使われるプログラムの名前 (たとえば、 **compress** あるいは
   **gzip**)です。符号化方式は *Content-Encoding* ヘッダとして使うのに
   適しており、 *Content-Transfer-Encoding* ヘッダには適して **いませ
   ん** 。マッピングはテーブル駆動です。符号化方式のサフィックスは大/
   小文字を区別します; データ型サフィックスは、最初大/小文字を区別して
   試し、それから大/小文字を区別せずに試します。

   The optional *strict* argument is a flag specifying whether the
   list of known MIME types is limited to only the official types
   registered with IANA. When *strict* is "True" (the default), only
   the IANA types are supported; when *strict* is "False", some
   additional non-standard but commonly used MIME types are also
   recognized.

   バージョン 3.8 で変更: *url* に *path-like object* を指定できるよう
   になりました。

   Soft deprecated since version 3.13: Passing a file path instead of
   URL is *soft deprecated*. Use "guess_file_type()" for this.

mimetypes.guess_file_type(path, *, strict=True)

   *path* で指定されたパスに基づき、ファイルの型を推定します。
   "guess_type()" に似ていますが、URL ではなくパスを受け付けます。 パ
   スには文字列、bytes オブジェクト、*path-like object* を指定できます
   。

   Added in version 3.13.

mimetypes.guess_all_extensions(type, strict=True)

   *type* で指定された MIME 型に基づき、ファイルの拡張子を推定します。
   戻り値は、先頭のドット ("'.'") を含む、候補となるすべてのファイル拡
   張子の文字列リストです。 拡張子が特定のデータストリームに関連付けら
   れていたことは保証されませんが、"guess_type()" と
   "guess_file_type()" に渡すと MIME 型 *type* に対応付けられます。

   省略可能な *strict* 引数は "guess_type()" 関数のものと同じ意味を持
   ちます。

mimetypes.guess_extension(type, strict=True)

   *type* で指定された MIME 型に基づき、ファイルの拡張子を推定します。
   戻り値は、先頭のドット ("'.'") を含むファイル拡張子の文字列です。
   拡張子が特定のデータストリームに関連付けられていたことは保証されま
   せんが、"guess_type()" と "guess_file_type()" に渡すと MIME 型
   *type* に対応付けられます。 *type* の拡張子を推定できなければ
   "None" を返します。

   省略可能な *strict* 引数は "guess_type()" 関数のものと同じ意味を持
   ちます。

モジュールの動作を制御するために、いくつかの追加の関数とデータ項目が利
用できます。

mimetypes.init(files=None)

   内部のデータ構造を初期化します。もし  *files* が与えられていれば、
   これはデフォルトの type map を増やすために使われる、一連のファイル
   名でなければなりません。もし省略されていれば、使われるファイル名は
   "knownfiles" から取られます。 Windows であれば、現在のレジストリの
   設定が読み込まれます。 *files* あるいは "knownfiles" 内の各ファイル
   名は、それ以前に現れる名前より優先されます。繰り返し "init()" を呼
   び出すことは許されています。

   *files* に空リストを与えることで、システムのデフォルトが適用される
   のを避けることが出来ます; 組み込みのリストから well-known な値だけ
   が取り込まれます。

   *files* が "None" の場合、内部のデータ構造は初期のデフォルト値に完
   全に再構築されます。 これは安定な操作であり、複数回呼び出されたとき
   は同じ結果になります。

   バージョン 3.2 で変更: 前のバージョンでは、 Windows のレジストリの
   設定は無視されていました。

mimetypes.read_mime_types(file)

   Load the type map given in the file named by *file*, if it exists.
   *file* must be a string specifying the name of the file to read.
   The type map is returned as a dictionary mapping file extensions,
   including the leading dot ("'.'"), to strings of the form
   "'type/subtype'".  If the file does not exist or cannot be read,
   "None" is returned.

mimetypes.add_type(type, ext, strict=True)

   MIME 型 *type* からのマッピングを拡張子 *ext* に追加します。拡張子
   がすでに既知であれば、新しい型が古いものに置き替わります。その型が
   すでに既知であれば、その拡張子が、既知の拡張子のリストに追加されま
   す。

   *strict* が "True" の時(デフォルト)は、そのマッピングは正式な MIME
   型に、そうでなければ、非標準の MIME 型に追加されます。

mimetypes.inited

   グローバルなデータ構造が初期化されているかどうかを示すフラグ。これ
   は "init()" により "True" に設定されます。

mimetypes.knownfiles

   共通にインストールされた型マップファイル名のリスト。これらのファイ
   ルは、普通 "mime.types" という名前であり、パッケージごとに異なる場
   所にインストールされます。

mimetypes.suffix_map

   サフィックスをサフィックスにマップする辞書。これは、符号化方式と型
   が同一拡張子で示される符号化ファイルが認識できるように使用されます
   。例えば、 ".tgz" 拡張子は、符号化と型が別個に認識できるように
   ".tar.gz" にマップされます。

mimetypes.encodings_map

   ファイル名拡張子を符号化方式型にマッピングする辞書。

mimetypes.types_map

   ファイル名拡張子を MIME 型にマップする辞書。

mimetypes.common_types

   ファイル名拡張子を非標準ではあるが、一般に使われている MIME 型にマ
   ップする辞書。

モジュールの使用例:

   >>> import mimetypes
   >>> mimetypes.init()
   >>> mimetypes.knownfiles
   ['/etc/mime.types', '/etc/httpd/mime.types', ... ]
   >>> mimetypes.suffix_map['.tgz']
   '.tar.gz'
   >>> mimetypes.encodings_map['.gz']
   'gzip'
   >>> mimetypes.types_map['.tgz']
   'application/x-tar-gz'


MimeTypes Objects
=================

"MimeTypes" クラスは一つ以上の MIME 型データベースが欲しいアプリケーシ
ョンにとって有用でしょう。これは "mimetypes" モジュールのそれと似たイ
ンターフェースを提供します。

class mimetypes.MimeTypes(filenames=(), strict=True)

   This class represents a MIME-types database.  By default, it
   provides access to the same database as the rest of this module.
   The initial database is created from Python's built-in MIME type
   tables. It may be extended by loading additional "mime.types"-style
   files into the database using the "read()" or "readfp()" methods.
   The mapping dictionaries may also be cleared before loading
   additional data if the default data is not desired.

   オプションの *filenames* 引数を使うと、デフォルトのデータベースに追
   加のファイルを読み込ませることができます。

   suffix_map

      Dictionary mapping suffixes to suffixes.  This is used to allow
      recognition of encoded files for which the encoding and the type
      are indicated by the same extension.  For example, the ".tgz"
      extension is mapped to ".tar.gz" to allow the encoding and type
      to be recognized separately. This is initialized with some
      predefined values.

   encodings_map

      Dictionary mapping filename extensions to encoding types. This
      is initialized with some predefined values.

   types_map

      Tuple containing two dictionaries, mapping filename extensions
      to MIME types: the first dictionary is for the non-standards
      types and the second one is for the standard types. They are
      initialized with some predefined values and MIME type
      information loaded from files specified by the *filenames*
      argument.

   types_map_inv

      Tuple containing two dictionaries, mapping MIME types to a list
      of filename extensions: the first dictionary is for the non-
      standards types and the second one is for the standard types.
      They are initialized with some predefined values and MIME type
      information loaded from files specified by the *filenames*
      argument.

   guess_extension(type, strict=True)

      "guess_extension()" 関数と同様ですが、オブジェクトに保存されたテ
      ーブルを使用します。

   guess_type(url, strict=True)

      "guess_type()" 関数と同様ですが、オブジェクトに保存されたテーブ
      ルを使用します。

   guess_file_type(path, *, strict=True)

      "guess_file_type()" 関数に似ていますが、オブジェクト内に保存され
      たテーブルを使います。

      Added in version 3.13.

   guess_all_extensions(type, strict=True)

      "guess_all_extensions()" と同様ですが、オブジェクトに保存された
      テーブルを参照します。

   read(filename, strict=True)

      MIME 情報を、 *filename* という名のファイルからロードします。こ
      れはファイルを解析するのに "readfp()" を使用します。

      *strict* が "True" の時 (デフォルト) は、そのマッピングは標準
      MIME 型のリストに、そうでなければ、非標準 MIME 型のリストに追加
      されます。

   readfp(fp, strict=True)

      MIME 型情報を、オープンしたファイル *fp* からロードします。ファ
      イルは、標準の "mime.types" ファイルの形式でなければなりません。

      *strict* が "True" の時 (デフォルト) は、そのマッピングは標準
      MIME 型のリストに、そうでなければ、非標準 MIME 型のリストに追加
      されます。

   read_windows_registry(strict=True)

      MIME type 情報を Windows のレジストリから読み込みます。

      利用可能な環境: Windows.

      *strict* が "True" の時 (デフォルト) は、そのマッピングは標準
      MIME 型のリストに、そうでなければ、非標準 MIME 型のリストに追加
      されます。

      Added in version 3.2.

   add_type(type, ext, strict=True)

      MIME 型 *type* からのマッピングを拡張子 *ext* に追加します。拡張
      子がすでに既知であれば、新しい型が古いものに置き替わります。その
      型がすでに既知であれば、その拡張子が、既知の拡張子のリストに追加
      されます。

      *strict* が "True" の時(デフォルト)は、そのマッピングは正式な
      MIME 型に、そうでなければ、非標準の MIME 型に追加されます。
