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.3.312

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.3.312
File Size Uploaded Provenance
otsucfgmng-1.4.3.312.tar.gz 12.5 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 24.6 kB

Release files / otsucfgmng-1.4.3.312.tar.gz

Download URL otsucfgmng-1.4.3.312.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
da4777bdae593935dc3ddf07942d910fc21ec5382c520cd166fa418eabfc44c2
BLAKE2b-256 checksum
How to use checksums
6cc2a19fc1e9218917ebbf806736a470bc2990ca2012e4ddb43d8f310d6654ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

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

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

Security for otsucfgmng 1.4.3.312

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