diff --git a/CHANGELOG b/CHANGELOG index af5691ca61..0230f9a33f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Allow to import backend from entry points * Add reversed operators to PYSON expressions * Use CURRENT_TIMESTAMP instead of now * Use Constraint instance in ModelSQL._sql_constraints diff --git a/trytond/backend/__init__.py b/trytond/backend/__init__.py index 44264ef2cf..31f1bf16dd 100644 --- a/trytond/backend/__init__.py +++ b/trytond/backend/__init__.py @@ -2,6 +2,12 @@ # this repository contains the full copyright notices and license terms. import sys import urlparse +import os +import imp +try: + import pkg_resources +except ImportError: + pkg_resources = None from trytond.config import config @@ -15,6 +21,16 @@ def name(): def get(prop): db_type = name() modname = 'trytond.backend.%s' % db_type - __import__(modname) + if modname not in sys.modules: + try: + __import__(modname) + except ImportError: + if not pkg_resources: + raise + ep, = pkg_resources.iter_entry_points('trytond.backend', db_type) + mod_path = os.path.join(ep.dist.location, + *ep.module_name.split('.')[:-1]) + fp, pathname, description = imp.find_module(db_type, [mod_path]) + imp.load_module(modname, fp, pathname, description) module = sys.modules[modname] return getattr(module, prop)