Skip to content

Commit

Permalink
plugins whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Mar 21, 2017
1 parent 1459a7b commit 0123185
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
21 changes: 18 additions & 3 deletions kore/containers/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,31 @@ def __init__(
self.component_factory = component_factory
self.component_registrar = component_registrar

def create(self, **components):
def create(self, config, **initial):
log.debug("Creating container")
container = Container(components)
kore_config = config.get('kore', {})

for namespace, component_class in self.components_provider.all():
container = Container(initial)
container.update({
'config': config,
'kore.config': kore_config,
})

components_dict = self.provide_components(kore_config)
for namespace, component_class in components_dict:
component = self.create_component(component_class)
self.component_registrar.register(container, component, namespace)

return container

def provide_components(self, kore_config):
components_whitelist = kore_config.get('plugins', [])

if not components_whitelist:
return self.components_provider.all()

return self.components_provider.filter(*components_whitelist)

def create_component(self, component_class):
log.debug("Creating `%s` plugin", component_class.__name__)
return self.component_factory.create(component_class)
7 changes: 7 additions & 0 deletions kore/plugins/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def get(self, name):
def all(self):
return self.plugins.items()

def filter(self, *whitelist):
filtered = {}
for name, plugin in self.plugins.items():
if name in whitelist:
filtered[name] = plugin
return filtered.items()

@property
def plugins(self):
if self._plugins_cache is None:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/test_container_factory.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class TestContainerFactory(object):

def test_initial_components(self, container_factory):
config = {}

components = {
'config': config,
'foo': 'bar',
'bar': 'baz',
}

container = container_factory.create(**components)

assert container('config') == config
assert container('foo') == 'bar'
assert container('bar') == 'baz'
25 changes: 25 additions & 0 deletions tests/integration/test_plugins_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class TestPuglinsProviderAll(object):

def test_returns_list(self, component_plugins_provider, component_plugin):
result = component_plugins_provider.all()

assert list(result) == [
(component_plugin.name, component_plugin.plugin),
]


class TestPuglinsProviderFilter(object):

def test_not_filtered(self, component_plugins_provider, component_plugin):
filtered = component_plugin.name + '123'
result = component_plugins_provider.filter(filtered)

assert list(result) == []

def test_filtered(self, component_plugins_provider, component_plugin):
filtered = component_plugin.name
result = component_plugins_provider.filter(filtered)

assert list(result) == [
(component_plugin.name, component_plugin.plugin),
]

0 comments on commit 0123185

Please sign in to comment.