Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
Merge eddacf9 into 8a69a53
Browse files Browse the repository at this point in the history
  • Loading branch information
lnielsen committed Feb 13, 2014
2 parents 8a69a53 + eddacf9 commit 0284ad9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
31 changes: 24 additions & 7 deletions flask_registry/registries/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ class SingletonRegistry(RegistryBase):
"""
Basic registry that just keeps a single object.
>>> from flask_registry import SingletonRegistry
>>> from flask import Flask
>>> from flask_registry import Registry, SingletonRegistry
>>> app = Flask('myapp')
>>> r = Registry(app=app)
>>> r['singleton'] = SingletonRegistry()
Expand Down Expand Up @@ -248,14 +249,28 @@ class ImportPathRegistry(ListRegistry):
flask_registry.registries.pkgresources
flask_registry
When using star imports it is sometimes useful to exclude certain imports:
>>> r['myns2'] = ImportPathRegistry(
... initial=['flask_registry.registries.*', ],
... exclude=['flask_registry.registries.core']
... )
>>> for imp_path in r['myns2']:
... print(imp_path)
flask_registry.registries.appdiscovery
flask_registry.registries.modulediscovery
flask_registry.registries.pkgresources
:param initial: List of initial import paths.
:param exclude: A list of import paths to not register. Useful together
with star imports (``'*'``). Defaults to ``[]``.
:param load_modules: Load the modules instead of just registering the
import path. Defaults to ``False``.
"""
def __init__(self, initial=None, load_modules=False):
def __init__(self, initial=None, exclude=None, load_modules=False):
super(ImportPathRegistry, self).__init__()
self.load_modules = load_modules
self.exclude = exclude or []
if initial:
for import_path in initial:
self.register(import_path)
Expand All @@ -275,13 +290,15 @@ def register(self, import_path):
if import_path.endswith('.*'):
for mod_path in find_modules(import_path[:-2],
include_packages=True):
if mod_path not in self.exclude:
super(ImportPathRegistry, self).register(
self._load_import_path(mod_path)
)
else:
if import_path not in self.exclude:
super(ImportPathRegistry, self).register(
self._load_import_path(mod_path)
self._load_import_path(import_path)
)
else:
super(ImportPathRegistry, self).register(
self._load_import_path(import_path)
)

def unregister(self, *args, **kwargs):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ def test_load_modules(self):
self.app.extensions['registry']['impns'][0], six.string_types
)

def test_exclude(self):
Registry(app=self.app)
self.app.extensions['registry']['impns'] = ImportPathRegistry(
initial=['flask_registry.registries.*'],
exclude=['flask_registry.registries.pkgresources'],
)
assert len(self.app.extensions['registry']['impns']) == 3
assert 'flask_registry.registries.pkgresources' not in \
self.app.extensions['registry']['impns']

def test_unregister(self):
Registry(app=self.app)
self.app.extensions['registry']['impns'] = ImportPathRegistry(
Expand Down

0 comments on commit 0284ad9

Please sign in to comment.