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_data=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
``dev`` if 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 is 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
Alternate Usage
---------------
::
import settings_helper as sh
settings = sh.get_all_settings(__name__)
or
::
import settings_helper as sh
settings = sh.get_all_settings(__name__).get(sh.APP_ENV, {})
The ``get_all_settings`` func returns a dict containing all sections
other than ‘default’.
- If a setting is defined in ‘default’, but not in a particular
section, the setting in ‘default’ will appear under the section
- If a setting (or upper-case equivalent) is defined as an environment
variable, that value will be used for all sections that use it
Tip
---
In your ``<package-name>/tests/__init__.py`` file, add the following so
the ``test`` section of settings is automatically used
::
import os
os.environ['APP_ENV'] = 'test'
Raw data
{
"_id": null,
"home_page": "https://github.com/kenjyco/settings-helper",
"name": "settings-helper",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "settings,config,environment variables,ini,ini file,ini parser,helper,kenjyco",
"author": "Ken",
"author_email": "kenjyco@gmail.com",
"download_url": "https://github.com/kenjyco/settings-helper/tarball/v0.0.12",
"platform": null,
"description": "Setup in your package\n---------------------\n\nCreate a default/sample ``settings.ini`` file in the module directory of\nyour package, with a ``[default]`` section and any other ``[sections]``\nyou want (i.e. app environments)\n\n::\n\n [default]\n something = 100\n\n [dev]\n redis_url = redis://localhost:6379/1\n something = 500\n\n [test]\n redis_url = redis://localhost:6379/9\n things = none, true, false, 1, 2.5, dogs\n\nCreate a ``MANIFEST.in`` file in your package directory with the\nfollowing\n\n::\n\n include settings.ini\n\nUpdate the ``setup.py`` file of the package to include the\n``setting.ini`` file and add ``settings-helper`` to ``install_requires``\nlist\n\n::\n\n from setuptools import setup, find_packages\n\n setup(\n name='package-name',\n version='0.0.1',\n ...\n packages=find_packages(),\n install_requires=[\n 'settings-helper',\n ...\n ],\n include_package_data=True,\n package_dir={'': '.'},\n package_data={\n '': ['*.ini'],\n },\n ...\n )\n\nNote, your package directory tree will be something like the following\n\n::\n\n package-name\n \u251c\u2500\u2500 .gitignore\n \u251c\u2500\u2500 LICENSE.txt\n \u251c\u2500\u2500 MANIFEST.in\n \u251c\u2500\u2500 README.md\n \u251c\u2500\u2500 README.rst\n \u251c\u2500\u2500 package_name/\n \u2502\u00a0\u00a0 \u251c\u2500\u2500 __init__.py\n \u2502\u00a0\u00a0 \u2514\u2500\u2500 settings.ini\n \u2514\u2500\u2500 setup.py\n\nUsage\n-----\n\nUse in ``__init__.py`` of package\n\n::\n\n import settings_helper as sh\n\n get_setting = sh.settings_getter(__name__)\n something = get_setting('something')\n something_else = get_setting('something_else', 'default_val')\n\nSet ``APP_ENV`` environment variable to be one of your section names\nwhen starting your Python interpreter/server. **``APP_ENV`` defaults to\n``dev`` if it is not set.**\n\n- The ``get_setting`` func will return the value of the requested\n variable if it is set in the section specified in ``APP_ENV``.\n- If the variable is not in the section, it will pull the value from\n the ``[default]`` section\n- If the varialbe is not in the ``[default]`` section either, then\n return the optional fallback value passed in the ``default`` keyword\n argument to ``get_setting`` (which defaults to an empty string)\n- **If the requested variable exists in the environment (or its\n uppercase equivalent), it will be used instead of getting from\n settings.ini**\n- The value is automatically converted to a bool, None, int, or float\n if it should be\n- If the value contains any of (, ; \\|) then a list of converted values\n will be returned\n\nThe first time that ``settings_getter`` func is invoked, it looks for a\n``settings.ini`` file in ``~/.config/<package-name>/settings.ini``.\n\n- If it does not find it, it will copy the default settings.ini from\n the module\u2019s install directory to that location\n- If the settings.ini file does not exist in the module\u2019s install\n directory, an exception is raised\n\nAlternate Usage\n---------------\n\n::\n\n import settings_helper as sh\n\n settings = sh.get_all_settings(__name__)\n\nor\n\n::\n\n import settings_helper as sh\n\n settings = sh.get_all_settings(__name__).get(sh.APP_ENV, {})\n\nThe ``get_all_settings`` func returns a dict containing all sections\nother than \u2018default\u2019.\n\n- If a setting is defined in \u2018default\u2019, but not in a particular\n section, the setting in \u2018default\u2019 will appear under the section\n- If a setting (or upper-case equivalent) is defined as an environment\n variable, that value will be used for all sections that use it\n\nTip\n---\n\nIn your ``<package-name>/tests/__init__.py`` file, add the following so\nthe ``test`` section of settings is automatically used\n\n::\n\n import os\n\n os.environ['APP_ENV'] = 'test'\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Helpers to get specific settings from a particular section of a settings.ini file",
"version": "0.0.12",
"project_urls": {
"Download": "https://github.com/kenjyco/settings-helper/tarball/v0.0.12",
"Homepage": "https://github.com/kenjyco/settings-helper"
},
"split_keywords": [
"settings",
"config",
"environment variables",
"ini",
"ini file",
"ini parser",
"helper",
"kenjyco"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2ce58d0563312b333db87696005b6f52d211b511a25d4aa02033826c81f9485a",
"md5": "2b7a38b21ef6392f3d7013dd8c818271",
"sha256": "bba323591998e2e96fc3280896176dc02f8267865b3fac2398ee6d255c91d8c1"
},
"downloads": -1,
"filename": "settings_helper-0.0.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2b7a38b21ef6392f3d7013dd8c818271",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6766,
"upload_time": "2022-08-28T21:30:27",
"upload_time_iso_8601": "2022-08-28T21:30:27.561313Z",
"url": "https://files.pythonhosted.org/packages/2c/e5/8d0563312b333db87696005b6f52d211b511a25d4aa02033826c81f9485a/settings_helper-0.0.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-08-28 21:30:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kenjyco",
"github_project": "settings-helper",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "bg-helper",
"specs": []
},
{
"name": "input-helper",
"specs": []
}
],
"lcname": "settings-helper"
}