PhilipTrauner / Meh

Python configuration files in Python. ¯\_(ツ)_/¯

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Meh

Meh Banner

Python version support: 2.7, 3.4, 3.5 License: MIT

Meh is a Python configuration utility that uses Python as it's data format. It provides a pleasant and very "pythonic" interface to do all the things a configuration utility does, like default values, validators and comments.

Usage

from meh import Config, Option, ExceptionInConfigError

config = Config()
config.add(Option("food", ["Steak", "Pizza", "Burgers"],
	validator=lambda x: type(x) is list))
config.add(Option("ages", {"@snoato" : 18, "@PhilipTrauner" : 16},
	validator=lambda x: type(x) is dict))
config.add(Option("bytestring", b"test"))
config += Option("another_number", 42.0, comment="hihi")
config -= Option("another_number", 42.0, comment="hihi")
config += Option("another_number", 42.0, comment="hihi")

CONFIG_PATH = "awesome_config.cfg"

try:
	config = config.load(CONFIG_PATH)
except (IOError, ExceptionInConfigError):
	config.dump(CONFIG_PATH)
	config = config.load(CONFIG_PATH)

print(config.food)
print(config.ages)
print(config.bytestring)
print(config.another_number)

# Changing values during runtime
config.food = ["Baked Beans", "Ramen"]
print(config.food)

Outputs:

['Steak', 'Pizza', 'Burgers']
{'@PhilipTrauner': 16, '@snoato': 18}
test
42.0
['Baked Beans', 'Ramen']

Creates:

food = ['Baked Beans', 'Ramen']
ages = {'@PhilipTrauner': 16, '@snoato': 18}
bytestring = "test"
another_number = 42.0 # hihi

Tidbits

Config(options=[], validation_failed=None)

  • options: Provide a list of options instead of calling add for all of them.
  • validation_failed: Provide a function accepting two parameters (name and value) that's called when a validation fails (returns nothing). (Otherwise a ValidationError will be raised)

Option(name, default_value, validator=None, comment="")

  • validator: Provide a function accepting one parameter which checks if a value is correct (returns a boolean).
  • comment: A comment appended to the variable declaration in the config file itself.

Installation

Manual:

git clone https://github.com/PhilipTrauner/Meh
cd Meh
python setup.py install

pip:

pip install meh

Honorable Mentions

To-Do

  • More test cases

About

Python configuration files in Python. ¯\_(ツ)_/¯

License:MIT License


Languages

Language:Python 100.0%