Skip to main content

設定ファイルを扱うクラスを生成するライブラリ

Project description

概要

このライブラリは設定ファイルの読み書きを補助するための基底クラスです。
BaseCMクラスを継承し、__defaults__, <attributes>を定義するだけで必要な操作を行えるようになります。

現在違うセクションに同じキーを持つような設定ファイルには対応していません。

# 違うセクションに同じキーを持つ例
{
    'app': {'name': 'Hello'},
    'default': {'name': 'Python'}
}

インストール

インストール

pip install otsucfgmng

アップデート

pip install -U otsucfgmng

アンインストール

pip uninstall otsucfgmng

使い方

  1. otuvalidatorをインポートし、必要なバリデータ、コンバータを使用できるようにする
    • 自作クラスなどを使用したい場合にはOtsuValidatorや実際のコードを参考に定義する
  2. otsucfgmngをインポートし、BaseCMを使用できるようにする
  3. BaseCMを継承したクラスを定義する
    1. 属性__defaults__に辞書形式で利用する属性名とその初期値を与える
    2. __defaults__で宣言した属性名に1.で用意したコンバータを与える
  4. 設定ファイルのパスを与えてインスタンスを作成する
  5. インスタンスの属性を書き換えて編集を行う
  6. save_cmを呼び出せば設定ファイルが出力される

実行例

作成

cfg.jsonという設定ファイルを作成していきます。

# 1.
from otsuvalidator import CBool, CInt, CPath, CString

# 2.
from otsucfgmng import BaseCM


# 3.
class ConfigurationManager(BaseCM):
    # 3.1.
    __defaults__ = {
        'app': {
            'library': 'SampleLibrary.dll',
            'scripts': 'SampleScripts.scrpt',
            'title': 'Sample Program',
            'fullscreen': False
        },
        'audio': {
            'bgm': 100,
            'bgs': 100,
            'se': 100,
            'me': 85
        }
    }

    # 3.2.
    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)


# 4.
cm = ConfigurationManager('cfg.json')

# 5.
cm.bgm = 99
cm.bgs = 50

# 6.
cm.save_cm()

上記の処理で作成されたcfg.jsonの中身は以下の通りです。
実行するたびにキーの並びは異なります。

{
    "app": {},
    "audio": {
        "bgm": 99,
        "bgs": 50
    }
}

読み込み

正しい形式で出力された設定ファイルをインスタンスの生成時に与えると自動で設定を読み込みます。

from otsuvalidator import CBool, CInt, CPath, CString

from otsucfgmng import BaseCM


class ConfigurationManager(BaseCM):
    __defaults__ = {
        'app': {
            'library': 'SampleLibrary.dll',
            'scripts': 'SampleScripts.scrpt',
            'title': 'Sample Program',
            'fullscreen': False
        },
        'audio': {
            'bgm': 100,
            'bgs': 100,
            'se': 100,
            'me': 85
        }
    }
    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)

# コンテキストマネージャを使用すると自動でsave_cmされます。
with ConfigurationManager('cfg.json') as cm:
    print(cm.user_cm())
    cm.fullscreen = 'yes'
    print(cm.user_cm())
### 出力は以下のようになります (実行するたびにキーの並びは異なります) ###
{'app': {}, 'audio': {'bgs': 50, 'bgm': 99}}
{'app': {'fullscreen': True}, 'audio': {'bgs': 50, 'bgm': 99}}

上記の処理で作成されたcfg.jsonの中身は以下の通りです。
実行するたびにキーの並びは異なります。

{
    "app": {
        "fullscreen": true
    },
    "audio": {
        "bgs": 50,
        "bgm": 99
    }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

otsucfgmng-1.0.0-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page