Skip to content

Commit

Permalink
- Supported environment variables for convergerc
Browse files Browse the repository at this point in the history
- Added CHANGELOG for tracking changes better and TODO
  • Loading branch information
shon committed Nov 19, 2019
1 parent 9a540c5 commit c6a736d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 23 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=======
History
=======

1.0.0 (2019-11-19)
------------------

* Supported environment variables for all convergerc directives
* Dropped support for Python 2 and Python < 3.5
File renamed without changes.
18 changes: 16 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ Example:
---------------------------
In case if you want to keep all settings.py files in a directory. Use `SETTINGS_DIR` directive in .convergerc file.

Example
~~~~~~~
Using SETTINGS_DIR
~~~~~~~~~~~~~~~~~~


.. code:: bash
Expand All @@ -229,3 +229,17 @@ This is useful when you have to deploy multiple instances of an app with differe
|
|

Using environment variables
~~~~~~~~~~~~~~~~~~~~~~~~~~~

It is possible use environment variables, which is useful in cases wheere you want to start multiple instances of same app directory.
Any of the supported directive can exported as environment variable.


.. code:: bash
export SETTINGS_DIR='settings/site1'
gunicorn --workers=2 service:app
export SETTINGS_DIR='settings/site2'
gunicorn --workers=2 service:app
2 changes: 2 additions & 0 deletions TODO.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Improve documentation (Make README easier to follow)
- Setup Travis CI
30 changes: 10 additions & 20 deletions converge/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def parse_rc(rc_config):
return rc_config


def parse_osenv(rc_config):
for directive in rc_config:
if directive in os.environ:
rc_config[directive] = os.environ[directive]


def import_settings(name, settings_dir=None, exit_on_err=False):
name += '_settings'
path = name + '.py'
Expand All @@ -71,25 +77,6 @@ def import_settings(name, settings_dir=None, exit_on_err=False):
sys.exit(1)


if sys.version_info.major == 2 or (
sys.version_info.major == 3 and sys.version_info.minor == 4):

def import_settings(name, settings_dir=None, exit_on_err=False):
name += '_settings'
if settings_dir:
name = settings_dir.replace(os.sep, '.') + '.' + name
try:
mod = importlib.import_module(name)
ns.update(dict((name, getattr(mod, name))
for name in dir(mod) if not name.startswith('_')))
print('[INFO] successfully imported: %s' % name)
except Exception as err:
level = 'Error' if exit_on_err else 'Warning'
print('[%s] Could not import "%s": %s' % (level, name, err))
if exit_on_err:
sys.exit(1)


def validate_mode(mode):
supported_app_modes = ('prod', 'test', 'dev', 'staging', 'beta')
if mode not in supported_app_modes:
Expand All @@ -112,9 +99,12 @@ def clone_git_repo(git_url, settings_dir, git_subdir=None):


def main():
rc_config_default = {'APP_MODE': 'dev', 'SETTINGS_DIR': None,
rc_config_default = {'APP_MODE': 'dev',
'SETTINGS_DIR': None,
'GIT_SETTINGS_REPO': None,
'GIT_SETTINGS_SUBDIR': None}

rc_config = parse_osenv(rc_config_default)
rc_config = parse_rc(rc_config_default)
settings_dir = rc_config['SETTINGS_DIR']
git_url = rc_config['GIT_SETTINGS_REPO']
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

setup(
name='converge',
version='0.9.8',
version='1.0.0',
url='http://pypi.python.org/pypi/converge/',
classifiers=[
'Programming Language :: Python :: 3'
],
python_requires='=>3.5',
include_package_data=True,
description='Ultra simple settings management for (only) Python apps',
long_description=open('README.rst').read(),
Expand Down
12 changes: 12 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ def test_git_settings():
assert settings.PROD is True


def test_env_vars():
config = {'SETTINGS_DIR': 'settings'}

os.environ['SETTINGS_DIR'] = 'settings/site1'
settings.parse_osenv(config)
assert config['SETTINGS_DIR'] == os.environ['SETTINGS_DIR']

os.environ['SETTINGS_DIR'] = 'settings/site2'
settings.parse_osenv(config)
assert config['SETTINGS_DIR'] == os.environ['SETTINGS_DIR']


def teardown_module():
py_path = 'default_settings.py'
pyc_path = py_path + 'c'
Expand Down

0 comments on commit c6a736d

Please sign in to comment.