Skip to content

Commit

Permalink
Make boto3 dependency optional (#30)
Browse files Browse the repository at this point in the history
* Add boto3 to local requirements

* Add optional feature "aws" that depends on boto3

* Raise runtime error if boto3 is not installed

* Add instructions to install optional aws feature

* Test runtime error when boto3 is missing

* Rename fixture

* Add instructions to install aws feature
  • Loading branch information
ronaldotd authored and osantana committed Oct 7, 2019
1 parent 934c08f commit 61b3510
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ First you need to install ``prettyconf`` library:
.. code-block:: sh
pip install prettyconf
The ``AwsParameterStore`` configuration loader depends on the ``boto3`` package.
If you need to use it, install ``prettyconf`` with the optional feature ``aws``:

.. code-block:: sh
pip install prettyconf[aws]
12 changes: 10 additions & 2 deletions prettyconf/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
from configparser import ConfigParser, MissingSectionHeaderError, NoOptionError
from glob import glob

import boto3
import botocore
try:
import boto3
import botocore
except ImportError:
boto3 = None

from .exceptions import InvalidConfigurationFile, InvalidPath, MissingSettingsSection
from .parsers import EnvFileParser
Expand Down Expand Up @@ -308,6 +311,11 @@ def __getitem__(self, item):

class AwsParameterStore(AbstractConfigurationLoader):
def __init__(self, path="/", aws_access_key_id=None, aws_secret_access_key=None, region_name="us-east-1"):
if not boto3:
raise RuntimeError(
'AwsParameterStore requires [aws] feature. Please install it: "pip install prettyconf[aws]"'
)

self.path = path
self.aws_access_key_id = aws_access_key_id
self.aws_secret_access_key = aws_secret_access_key
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
boto3
coveralls
pytest
pytest-cov
pytest-runner
Sphinx
sphinx-rtd-theme
twine
coveralls
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def run(self):
author="Osvaldo Santana Neto", author_email="[email protected]",
license="MIT",
packages=['prettyconf'],
install_requires=['boto3'],
extras_require={'aws': ['boto3']},
platforms='any',
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
16 changes: 16 additions & 0 deletions tests/test_parameterstore.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib
import sys
from unittest import mock

import pytest
Expand Down Expand Up @@ -43,6 +45,20 @@
}


@pytest.fixture
def boto_not_installed():
sys.modules['boto3'] = None
importlib.reload(sys.modules['prettyconf.loaders'])
yield
sys.modules.pop('boto3')
importlib.reload(sys.modules['prettyconf.loaders'])


def test_create_loader_boto_not_installed(boto_not_installed):
with pytest.raises(RuntimeError):
AwsParameterStore()


@mock.patch("prettyconf.loaders.boto3")
def test_basic_config(mock_boto):
mock_boto.client.return_value.get_parameters_by_path.return_value = PARAMETER_RESPONSE
Expand Down

0 comments on commit 61b3510

Please sign in to comment.