Helpers to get specific settings from a particular section of a settings.ini file
Project description
Setup in your package
Create a default/sample settings.ini file in the module directory of your package, with a [default] section and any other [sections] you want ( i.e. app environments)
[default] something = 100 [dev] redis_url = redis://localhost:6379/1 something = 500 [test] redis_url = redis://localhost:6379/9 things = none, true, false, 1, 2.5, dogs
Create a MANIFEST.in file in your package directory with the following
include settings.ini
Update the setup.py file of the package to include the setting.ini file and add settings-helper to install_requires list
from setuptools import setup, find_packages setup( name='package-name', version='0.0.1', ... packages=find_packages(), install_requires=[ 'settings-helper', ... ], include_package_date=True, package_dir={'': '.'}, package_data={ '': ['*.ini'], }, ... )
Note, your package directory tree will be something like the following
package-name ├── .gitignore ├── LICENSE.txt ├── MANIFEST.in ├── README.md ├── README.rst ├── package_name/ │ ├── __init__.py │ └── settings.ini └── setup.py
Usage
Use in __init__.py of package
import settings_helper as sh get_setting = sh.settings_getter(__name__) something = get_setting('something') something_else = get_setting('something_else', 'default_val')
Set APP_ENV environment variable to be one of your section names when starting your Python interpreter/server. APP_ENV defaults to devif it is not set.
The get_setting func will return the value of the requested variable if it is set in the section specified in APP_ENV.
If the variable is not in the section, it will pull the value from the [default] section
If the varialbe is not in the [default] section either, then return the optional fallback value passed in the default keyword argument to get_setting (which defaults to an empty string)
If the requested variable exists in the environment (or its uppercase equivalent), it will be used instead of getting from settings.ini
The value is automatically converted to a bool, None, int, or float if it should be
If the value contains any of (, ; |) then a list of converted values will be returned
The first time that settings_getter func in invoked, it looks for a settings.ini file in ~/.config/package-name/settings.ini.
If it does not find it, it will copy the default settings.ini from the module’s install directory to that location
If the settings.ini file does not exist in the module’s install directory, an exception is raised
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
Built Distribution
Hashes for settings_helper-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86b33105601cf8deb25d579025ff6c01a57bfd59c0fb857a4d02ee87e5240f27 |
|
MD5 | 216890290397a3571a911df29799974f |
|
BLAKE2b-256 | bb6f3438e977ddbbf0698e42f2b07a1e31219ff9c1f62a2dfde44b01cd9f833c |