Skip to main content

otsucfgmng

otsucfgmngは、Pythonアプリケーションにおける設定ファイル(JSON)の読み書き・バリデーション・永続化を直感的に扱うためのライブラリです。

クラス定義として設定項目と初期値を記述するだけで、ネストされたJSON設定ファイルの読み込み、型チェック / バリデーション、デフォルト値からの最小保存、コンテキストマネージャによる自動保存などの機能を一元管理できます。


主な特徴

  • クラスベースの宣言型設定: BaseCMを継承し、メタデータや初期値(__defaults__)を記述するだけで簡単に設定クラスを作成できます。
  • バリデータとの連携: otsuvalidatorなどのディスクリプタ(CPath, CInt, CString, CBool等)と組み合わせることで、設定値の設定時に自動でバリデーションや型変換が行われます。
  • ネストされたJSONの自動ハンドリング: 多層構造の辞書(セクション)も自動的に平坦な属性としてマッピングし、出力時には元の構造へ復元します。
  • 差分のみの最小保存: 初期値から変更されたユーザ設定のみをJSONファイルに書き出すため、設定ファイルが冗長になりません(非表示オプションや全出力の制御も可能)。
  • コンテキストマネージャ対応: with構文を利用することで、スコープ脱出時に設定変更を自動でファイルへ保存します。
  • 型補完(Protocol)のサポート: IDEでの保管や静的型チェック(mypy / pyright)用にPBaseCMプロトコルを提供しています。

インストール

依存ライブラリとしてotsutilおよびotsuvalidatorが必要です。

pip install otsucfgmng

基本的な使い方

最終的なコードは./sample_code.pyを参照してください。

1. 設定クラスの定義

BaseCMを継承し、__defaults__に初期値の辞書を定義します。各設定項目にはotsuvalidator等のバリデータ属性を割り当てます。

from pathlib import Path
from typing import Any

from otsuvalidator import CBool, CInt, CPath, CString, VChoice

from otsucfgmng import BaseCM, PBaseCM


class AppConfig(BaseCM):
    # 1. デフォルト設定の定義(ネスト構造に対応)
    __defaults__ = {
        "app": {
            "library": "SampleLibrary.dll",
            "scripts": "SampleScripts.scrpt",
            "title": "Sample Program",
            "fullscreen": False,
        },
        "other": {
            "audio": {
                "bgm": 100,
                "bgs": 100,
                "se": 100,
                "me": 85,
            },
            "graphic": {
                "quality": "high",
            },
        },
    }

    # 2. 保存時に無視・非表示にするオプション(任意)
    __hidden_options__ = ("fullscreen", "me")

    # 3. 各属性に対するバリデータ・型コンバータの設定
    library = CPath("dll")
    scripts = CPath(".scrpt")
    title = CString(1, checker=str.istitle)
    fullscreen = CBool()
    bgm = CInt(0, 100)
    bgs = CInt(0, 100)
    se = CInt(0, 100)
    me = CInt(0, 100)
    quality = VChoice(("high", "low"))

2. コンテキストマネージャによる自動保存

withスコープ内で値を変更すると、スコープを抜ける際に変更内容が自動的にJSONファイルへ保存されます。

# 2. コンテキストマネージャによる自動保存
pf_cfg = Path("./cfg.json")

# コンテキストマネージャで開く
with AppConfig(pf_cfg) as cfg:
    # 属性としてアクセス可能
    print(cfg.bgm)  # => 100(初期値)

    cfg.bgm = 92
    cfg.library = "./dll/TestLibrary.dll"
# スコープ脱出時に`cfg.json`が自動更新される
# 差分(デフォルトと異なる値)のみがJSONに出力されます

3. 設定のリセットとデフォルト値の参照

各設定項目には、自動的に上書き不可の初期値参照属性(default_<属性名>_cm)が生成されます。
また、reset_cm()を呼び出すことでいつでも全設定を初期値に戻せます。

# 3. 設定のリセットとデフォルト値の参照
with AppConfig(pf_cfg) as cfg:
    cfg.bgm = 10
    print(cfg.bgm)  # => 10
    print(cfg.default_bgm_cm)  # => 100(初期値)

    # 初期属性値への直接代入はエラーになります
    # cfg.default_bgm_cm = 50  # AttributeError

    # 初期値へリセット
    cfg.reset_cm()
    print(cfg.bgm)  # => 100

APIリファレンス(BaseCM主要メソッド)

メソッド / 属性 説明
load_cm(**kwargs) 設定ファイルからJSONデータを読み込み、各属性へ値を反映します。
save_cm(export_default_config=False, **kwargs) 現在の設定状況をJSONファイルに保存します。
reset_cm() すべての設定属性を定義時のデフォルト値に初期化します。
defaults_cm() 定義されているデフォルト値の辞書(ディープコピー)を取得します。
user_cm(include_default_config=False) ユーザが変更した設定項目のみの辞書構造を取得します。
cfg_to_str_cm(all_=False) 現在の設定を整形されたJSON文字列として返します(デバッグ・表示用)。
attributes_cm (プロパティ)設定項目として登録されている属性名のセットを取得します。
key_places_cm (プロパティ)各属性が辞書のどの階層セクションに属しているかのマップを取得します。

ライセンス

MIT License

Metadata

Release files for otsucfgmng 1.4.4.313

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for otsucfgmng 1.4.4.313
File Size Uploaded Provenance
otsucfgmng-1.4.4.313.tar.gz 12.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for otsucfgmng 1.4.4.313
File Interpreter ABI Platform
otsucfgmng-1.4.4.313-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / otsucfgmng-1.4.4.313.tar.gz

Download URL otsucfgmng-1.4.4.313.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8dcbede9d20652ccc8b57d42b5d4e91047b0986e2d8bf9990e5db013a4147c43
BLAKE2b-256 checksum
How to use checksums
77d679ba601faa30fd63412bb4f2a3a787e94ba1c77d9181adfb21351ffaa761
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release files / otsucfgmng-1.4.4.313-py3-none-any.whl

Download URL otsucfgmng-1.4.4.313-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6c1a4d5d0c49998337d3095b23bed2e22318f1e47d978c00186a60a75d9d611
BLAKE2b-256 checksum
How to use checksums
5c0cbeb80a45f9ef6622a5599e831bdc41738d0c4fd7d62aae9aa9a119ca337b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Security for otsucfgmng 1.4.4.313

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page