Skip to content

How to configure custom dynamic metadataカスタム動的メタデータの設定方法


If you have project metadata that is not appropriate for static entry into pyproject.toml you will need to provide a custom metadata hook to apply such data during builds.もし、プロジェクトメタデータpyproject.tomlに静的に入力するのに適していない場合、ビルド中にそのようなデータを適用するためにカスタムメタデータフックを提供する必要があります。

Alternatives代替案

Dynamic metadata is a way to have a single source of truth that will be available at build time and at run time. Another way to achieve that is to enter the build data statically and then look up the same information dynamically in the program or package, using importlib.metadata.動的メタデータは、ビルド時と実行時に利用可能な真実の単一の情報源を持つ方法です。これを達成する別の方法は、ビルドデータを静的に入力し、その後プログラムやパッケージ内で同じ情報を動的に参照することです。importlib.metadataを使用します。

If the version field is the only metadata of concern, Hatchling provides a few built-in ways such as the regex version source and also third-party plugins. The approach here will also work, but is more complex.もしversion fieldが唯一の関心のあるメタデータであれば、Hatchlingはregex version sourceサードパーティプラグインなど、いくつかの組み込みの方法を提供します。ここでのアプローチも機能しますが、より複雑です。

Update project metadataプロジェクトメタデータの更新

Change the [project] section of pyproject.toml:[project]セクションをpyproject.tomlの中で変更します:

  1. Define the dynamic field as an array of all the fields you will set dynamically e.g. dynamic = ["version", "license", "authors", "maintainers"]dynamic fieldを、動的に設定するすべてのフィールドの配列として定義します。例えば、dynamic = ["version", "license", "authors", "maintainers"]
  2. If any of those fields have static definitions in pyproject.toml, delete those definitions. It is verboten to define a field statically and dynamically.これらのフィールドのいずれかにpyproject.toml内で静的な定義がある場合、それらの定義を削除してください。フィールドを静的に定義し、かつ動的に定義することは禁じられています。

Add a section to trigger loading of dynamic metadata plugins: [tool.hatch.metadata.hooks.custom]. Use exactly that name, regardless of the name of the class you will use or its PLUGIN_NAME. There doesn't need to be anything in the section.動的メタデータプラグインの読み込みをトリガーするセクションを追加します: [tool.hatch.metadata.hooks.custom]。使用するクラスの名前やそのPLUGIN_NAMEに関係なく、正確にその名前を使用してください。このセクションには何も含める必要はありません。

If your plugin requires additional third-party packages to do its work, add them to the requires array in the [build-system] section of pyproject.toml.プラグインが作業を行うために追加のサードパーティパッケージを必要とする場合、それらをpyproject.toml[build-system]セクションのrequires配列に追加します。

Implement hookフックの実装

The dynamic lookup must happen in a custom plugin that you write. The default expectation is that it is in a hatch_build.py file at the root of the project. Subclass MetadataHookInterface and implement update(); for example, here's plugin that reads metadata from a JSON file:動的なルックアップは、あなたが書くカスタムプラグイン内で行う必要があります。デフォルトの期待値は、それがプロジェクトのルートにあるhatch_build.pyファイル内にあることです。MetadataHookInterfaceをサブクラス化し、update()を実装します。例えば、以下はJSONファイルからメタデータを読み取るプラグインの例です:

import json
import os

from hatchling.metadata.plugin.interface import MetadataHookInterface


class JSONMetaDataHook(MetadataHookInterface):
    def update(self, metadata):
        src_file = os.path.join(self.root, "gnumeric", ".constants.json")
        with open(src_file) as src:
            constants = json.load(src)
            metadata["version"] = constants["__version__"]
            metadata["license"] = constants["__license__"]
            metadata["authors"] = [
                {"name": constants["__author__"], "email": constants["__author_email__"]},
            ]
  1. You must import the MetadataHookInterface to subclass it.MetadataHookInterfaceをインポートして、それをサブクラス化する必要があります。
  2. Do your operations inside the update method.update メソッド内で操作を行ってください。
  3. metadata refers to project metadata.metadataプロジェクトメタデータ を指します。
  4. When writing to metadata, use list for TOML arrays. Note that if a list is expected, it is required even if there is a single element.メタデータに書き込む際は、TOML配列には list を使用してください。リストが期待される場合は、単一の要素であっても必須です。
  5. Use dict for TOML tables e.g. authors.TOMLテーブルには dict を使用してください。例: authors

If you want to store the hook in a different location, set the path option:フックを別の場所に保存したい場合は、path オプションを設定してください:

[tool.hatch.metadata.hooks.custom]
path = "some/where.py"
[metadata.hooks.custom]
path = "some/where.py"