예제 #1
0
def construct_backend(backend_type=None):
    """
    Construct a concrete backend instance based on the backend_type or use the global backend value if not specified.

    :param backend_type: get a backend instance based on the specified type (or default)
    :return: :class:`Backend`
    """
    if backend_type is None:
        from aiida.backends import settings
        backend_type = settings.BACKEND

    if backend_type == 'django':
        global _DJANGO_BACKEND
        if _DJANGO_BACKEND is None:
            from aiida.orm.implementation.django.backend import DjangoBackend
            _DJANGO_BACKEND = DjangoBackend()
        return _DJANGO_BACKEND
    elif backend_type == 'sqlalchemy':
        global _SQLA_BACKEND
        if _SQLA_BACKEND is None:
            from aiida.orm.implementation.sqlalchemy.backend import SqlaBackend
            _SQLA_BACKEND = SqlaBackend()
        return _SQLA_BACKEND
    else:
        raise ValueError(
            "The specified backend {} is currently not implemented".format(
                backend_type))
예제 #2
0
    def _load_backend(self, schema_check=True):
        """Load the backend for the currently configured profile and return it.

        .. note:: this will reconstruct the `Backend` instance in `self._backend` so the preferred method to load the
            backend is to call `get_backend` which will create it only when not yet instantiated.

        :param schema_check: force a database schema check if the database environment has not yet been loaded
        :return: the database backend
        :rtype: :class:`aiida.orm.implementation.Backend`
        """
        from aiida.backends import BACKEND_DJANGO, BACKEND_SQLA
        from aiida.common import ConfigurationError, InvalidOperation
        from aiida.common.log import configure_logging
        from aiida.manage import configuration

        profile = self.get_profile()

        if profile is None:
            raise ConfigurationError(
                'Could not determine the current profile. Consider loading a profile using `aiida.load_profile()`.'
            )

        if configuration.BACKEND_UUID is not None and configuration.BACKEND_UUID != profile.uuid:
            raise InvalidOperation(
                'cannot load backend because backend of another profile is already loaded'
            )

        backend_type = profile.database_backend

        if backend_type == BACKEND_DJANGO:
            from aiida.backends.djsite.utils import load_dbenv, _load_dbenv_noschemacheck
            load_backend = load_dbenv if schema_check else _load_dbenv_noschemacheck
        elif backend_type == BACKEND_SQLA:
            from aiida.backends.sqlalchemy.utils import load_dbenv, _load_dbenv_noschemacheck
            load_backend = load_dbenv if schema_check else _load_dbenv_noschemacheck
        else:
            raise ConfigurationError(
                'Invalid backend type {} in profile: {}'.format(
                    backend_type, profile.name))

        # Do NOT reload the backend environment if already loaded, simply reload the backend instance after
        if configuration.BACKEND_UUID is None:
            load_backend(profile)
            configuration.BACKEND_UUID = profile.uuid

        # Can only import the backend classes after the backend has been loaded
        if backend_type == BACKEND_DJANGO:
            from aiida.orm.implementation.django.backend import DjangoBackend
            self._backend = DjangoBackend()
        elif backend_type == BACKEND_SQLA:
            from aiida.orm.implementation.sqlalchemy.backend import SqlaBackend
            self._backend = SqlaBackend()

        # Reconfigure the logging with `with_orm=True` to make sure that profile specific logging configuration options
        # are taken into account and the `DbLogHandler` is configured.
        configure_logging(with_orm=True)

        return self._backend
예제 #3
0
 def setUpClass_method(self):
     self.clean_db()
     self.backend = DjangoBackend()