設定ファイルを扱うクラスを生成するライブラリ
Project description
概要
このライブラリは設定ファイルの読み書きを補助するための基底クラスです。
BaseCM
クラスを継承し、__defaults__
, <attributes>
を定義するだけで必要な操作を行えるようになります。
現在違うセクションに同じキーを持つような設定ファイルには対応していません。
# 違うセクションに同じキーを持つ例
{
'app': {'name': 'Hello'},
'default': {'name': 'Python'}
}
インストール
インストール
pip install otsucfgmng
アップデート
pip install -U otsucfgmng
アンインストール
pip uninstall otsucfgmng
使い方
otuvalidator
をインポートし、必要なバリデータ、コンバータを使用できるようにする- 自作クラスなどを使用したい場合にはOtsuValidatorや実際のコードを参考に定義する
otsucfgmng
をインポートし、BaseCM
を使用できるようにするBaseCM
を継承したクラスを定義する- 属性
__defaults__
に辞書形式で利用する属性名とその初期値を与える __defaults__
で宣言した属性名に1.で用意したコンバータを与える
- 属性
- 設定ファイルのパスを与えてインスタンスを作成する
- インスタンスの属性を書き換えて編集を行う
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
Release history Release notifications | RSS feed
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
Close
Hashes for otsucfgmng-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e9a3f6a1ba49044061876529fb973485542aa72bf675633ee21da98c963955f |
|
MD5 | 913165a333ab599529012476825632f8 |
|
BLAKE2b-256 | bb44c6bf2a1c428e23cf2fba382ea1635e51d9173a1a90379c3db5ae26919bbe |