Skip to content

Commit

Permalink
Gives 3rd party modules the ability to define the module name morepat…
Browse files Browse the repository at this point in the history
…h should use during autoconfig.

See #319.
  • Loading branch information
Denis Krienbühl committed Jun 4, 2015
1 parent 28a94e9 commit a0faac7
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ develop =
fixture_packages/ns
fixture_packages/ns2
fixture_packages/no_mp_ns
fixture_packages/entry-point
parts = devpython py.test sphinxpython sphinxbuilder releaser
versions = versions
show-picked-versions = true
Expand Down Expand Up @@ -45,6 +46,7 @@ eggs = morepath
ns.real
ns.real2
no_mp_ns
entry-point

[sphinxbuilder]
recipe = collective.recipe.sphinxbuilder
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions fixture_packages/entry-point/entrypoint/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import morepath

app = morepath.App()

class Foo(object):
pass

@app.path(path='bar', model=Foo)
def get_foo():
return Foo()
19 changes: 19 additions & 0 deletions fixture_packages/entry-point/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from setuptools import setup, find_packages

setup(name='entry-point',
version='0.1dev',
description="Entry Point Test Fixture",
author="Martijn Faassen",
author_email="faassen@startifact.com",
license="BSD",
packages=find_packages(),
zip_safe=False,
install_requires=[
'setuptools',
'morepath'
],
entry_points={
'morepath': [
'autoimport = entrypoint',
]
})
37 changes: 36 additions & 1 deletion morepath/autosetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ def autoconfig(ignore=None):
``myapp`` the package must be named ``myapp`` as well (not ``my-app`` or
``MyApp``).
* If the setup.py differs from the package name, it's possible
to specify the module morepath should scan using entry points::
setup(name='some-package',
...
install_requires=[
'setuptools',
'morepath'
],
entry_points={
'morepath': [
'autoimport = somepackage',
]
})
This function creates a :class:`Config` object as with :func:`setup`, but
before returning it scans all packages, looking for those that depend on
Morepath directly or indirectly. This includes the package that
Expand Down Expand Up @@ -153,12 +168,32 @@ def morepath_packages():
yield import_package(distribution)


def get_module_name(distribution):
""" Determines the module name to import from the given distribution.
If an entry point named ``autoimport`` is found in the group ``morepath``,
it's value is used. If not, the project_name is used.
See :func:`autoconfig` for details and an example.
"""
if hasattr(distribution, 'get_entry_map'):
entry_points = distribution.get_entry_map('morepath')
else:
entry_points = None

if entry_points and 'autoimport' in entry_points:
return entry_points['autoimport'].module_name
else:
return distribution.project_name


def import_package(distribution):
""" Takes a pkg_resources distribution and loads the module contained
in it, if it matches the rules layed out in :func:`autoconfig`.
"""
try:
return importlib.import_module(distribution.project_name)
return importlib.import_module(get_module_name(distribution))
except ImportError:
raise AutoImportError(distribution.project_name)
4 changes: 3 additions & 1 deletion morepath/tests/test_autosetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ def setup_module(module):
def test_import():
import base
import sub
import entrypoint
from ns import real
from ns import real2

assert sorted(list(morepath_packages()),
key=lambda module: module.__name__) == [
base, real, real2, sub]
base, entrypoint, real, real2, sub]


def test_autoconfig():
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ deps=
-e{toxinidir}/fixture_packages/ns
-e{toxinidir}/fixture_packages/no_mp_ns
-e{toxinidir}/fixture_packages/ns2
-e{toxinidir}/fixture_packages/entry-point
commands=py.test morepath --cov morepath {posargs}

[testenv:pep8]
Expand Down

0 comments on commit a0faac7

Please sign in to comment.