Skip to content

Commit

Permalink
Added config dependency provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Miklos Vincze committed Jan 15, 2017
1 parent aa6b6b8 commit 6737527
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 1 deletion.
15 changes: 15 additions & 0 deletions docs/built_in_dependencies.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.. _built_in_dependencies:

Built-in Dependencies
=====================

Nameko includes some commonly used :ref:`dependency_injection`. This section introduces them and gives brief examples of their usage.

.. _config_dependency:

Config
------

Config is a simple dependency provider for accessing configuration values runtime, see :ref:`running_a_service`.

.. literalinclude:: examples/config_dependency.py
4 changes: 4 additions & 0 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Command Line Interface

Nameko ships with a command line interface to make hosting and interacting with services as easy as possible.

.. _running_a_service:

Running a Service
-----------------

Expand Down Expand Up @@ -43,6 +45,8 @@ and providing a simple YAML configuration file:
The ``LOGGING`` entry is passed to :func:`logging.config.dictConfig` and should conform to the schema for that call.

Config values can be accessed via the built-in :ref:`config_dependency` dependency provider.


Environment variable substitution
---------------------------------
Expand Down
13 changes: 13 additions & 0 deletions docs/examples/config_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from nameko.dependencies import Config
from nameko.rpc import rpc


class Service:

name = "test_config"

config = Config()

@rpc
def get_max_workers(self):
return self.config.get("max_workers")
17 changes: 16 additions & 1 deletion docs/examples/test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from nameko.standalone.events import event_dispatcher
from nameko.standalone.rpc import ClusterRpcProxy, ServiceRpcProxy
from nameko.testing.services import entrypoint_waiter
from nameko.testing.services import entrypoint_waiter, entrypoint_hook


class TestHttp(object):
Expand Down Expand Up @@ -281,3 +281,18 @@ def test_websocket_rpc(self, container_factory, web_config, websocket):

ws = websocket()
assert ws.rpc('echo', value=u"hellø") == u'hellø'


class TestConfig:

def test_can_get_config_value(self, container_factory, empty_config):
from examples.config_dependency import Service

config = empty_config.copy()
config["max_workers"] = 123

container = container_factory(Service, config)
container.start()

with entrypoint_hook(container, "get_max_workers") as get_max_workers:
assert 123 == get_max_workers()
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ This section covers most things you need to know to create and run your own Name
installation
cli
built_in_extensions
built_in_dependencies
community_extensions
testing
writing_extensions
Expand Down
12 changes: 12 additions & 0 deletions nameko/dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
""" Nameko built-in dependencies.
"""

from nameko.extensions import DependencyProvider


class Config(DependencyProvider):
""" Dependency provider for accessing configuration values.
"""

def get_dependency(self, worker_ctx):
return self.container.config.copy()
20 changes: 20 additions & 0 deletions test/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mock import Mock, call, patch
import pytest

from nameko.dependencies import Config


class TestConfig:

@pytest.yield_fixture(autouse=True)
def patch_container(self):
with patch.object(Config, 'container'):
yield

def test_get_dependency(self):
config = Config()

dependency = config.get_dependency(Mock(name='worker_ctx'))

assert config.container.config.copy.return_value == dependency
assert [call()] == config.container.config.copy.call_args_list

0 comments on commit 6737527

Please sign in to comment.