Exemplo n.º 1
0
class TvbProfile():
    """
    ENUM-like class with current TVB profile and accepted values.
    """

    LIBRARY_PROFILE = "LIBRARY_PROFILE"
    COMMAND_PROFILE = "COMMAND_PROFILE"
    WEB_PROFILE = "WEB_PROFILE"
    DESKTOP_PROFILE = "DESKTOP_PROFILE"

    TEST_LIBRARY_PROFILE = "TEST_LIBRARY_PROFILE"
    TEST_POSTGRES_PROFILE = "TEST_POSTGRES_PROFILE"
    TEST_SQLITE_PROFILE = "TEST_SQLITE_PROFILE"

    ALL = [
        LIBRARY_PROFILE, COMMAND_PROFILE, WEB_PROFILE, DESKTOP_PROFILE,
        TEST_POSTGRES_PROFILE, TEST_SQLITE_PROFILE, TEST_LIBRARY_PROFILE
    ]

    REGISTERED_PROFILES = {}

    CURRENT_PROFILE_NAME = None

    current = BaseSettingsProfile(False)
    env = Environment()

    _meta_path_copier = MetaPathCopier()
    _meta_path_copier.deepcopy()

    @classmethod
    def set_profile(cls, selected_profile, in_operation=False, run_init=True):
        """
        Sets TVB profile and do related initializations.
        """

        ### Ensure Python is using UTF-8 encoding (otherwise default encoding is ASCII)
        ### We should make sure UTF-8 gets set before reading from any TVB files
        ### e.g. TVB_STORAGE will differ if the .tvb.configuration file contains non-ascii bytes
        ### most of the comments in the simulator are having pieces outside of ascii coverage
        if cls.env.is_development(
        ) and sys.getdefaultencoding().lower() != 'utf-8':
            reload(sys)
            sys.setdefaultencoding('utf-8')

        if selected_profile is not None:
            ## Restore sys.meta_path, as some profiles (Library) are adding something
            cls._meta_path_copier.restore()

            cls._load_framework_profiles(selected_profile)
            cls._build_profile_class(selected_profile, in_operation, run_init)

    @classmethod
    def _build_profile_class(cls,
                             selected_profile,
                             in_operation=False,
                             run_init=True):
        """
        :param selected_profile: Profile name to be loaded.
        """

        if selected_profile in cls.REGISTERED_PROFILES:
            current_class = cls.REGISTERED_PROFILES[selected_profile]

            cls.current = current_class()
            cls.CURRENT_PROFILE_NAME = selected_profile

            if in_operation:
                # set flags IN_OPERATION,  before initialize** calls, to avoid LoggingBuilder being created there
                cls.current.prepare_for_operation_mode()

            if not cls.env.is_development():
                # initialize deployment first, because in case of a contributor setup this tried to reload
                # and initialize_profile loads already too many tvb modules,
                # making the reload difficult and prone to more failures
                cls.current.initialize_for_deployment()
            if run_init:
                cls.current.initialize_profile()

        else:
            msg = "Invalid profile name %r, expected one of %r"
            msg %= (selected_profile, cls.ALL)
            raise Exception(msg)

    @classmethod
    def _load_framework_profiles(cls, new_profile):

        from tvb.basic.config.profile_settings import LibrarySettingsProfile, TestLibraryProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.LIBRARY_PROFILE] = LibrarySettingsProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.TEST_LIBRARY_PROFILE] = TestLibraryProfile

        if not cls.is_library_mode(new_profile):
            try:
                from tvb.config.profile_settings import CommandSettingsProfile, WebSettingsProfile
                from tvb.config.profile_settings import TestPostgresProfile, TestSQLiteProfile

                cls.REGISTERED_PROFILES[
                    TvbProfile.COMMAND_PROFILE] = CommandSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.WEB_PROFILE] = WebSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_POSTGRES_PROFILE] = TestPostgresProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_SQLITE_PROFILE] = TestSQLiteProfile

            except ImportError:
                pass

    @staticmethod
    def is_library_mode(new_profile=None):

        lib_profiles = [
            TvbProfile.LIBRARY_PROFILE, TvbProfile.TEST_LIBRARY_PROFILE
        ]
        result = (new_profile in lib_profiles
                  or (new_profile is None
                      and TvbProfile.CURRENT_PROFILE_NAME in lib_profiles)
                  or not TvbProfile.env.is_framework_present())

        # Make sure default settings are not failing because we are not finding some modules
        if (new_profile is None and TvbProfile.CURRENT_PROFILE_NAME is None
                and not TvbProfile.env.is_framework_present()):
            TvbProfile.set_profile(TvbProfile.LIBRARY_PROFILE)

        return result

    @staticmethod
    def is_first_run():

        return TvbProfile.current.manager.is_first_run()
Exemplo n.º 2
0
class TvbProfile(object):
    """
    ENUM-like class with current TVB profile and accepted values.
    """

    LIBRARY_PROFILE = "LIBRARY_PROFILE"
    COMMAND_PROFILE = "COMMAND_PROFILE"
    WEB_PROFILE = "WEB_PROFILE"
    MATLAB_PROFILE = "MATLAB_PROFILE"

    TEST_LIBRARY_PROFILE = "TEST_LIBRARY_PROFILE"
    TEST_POSTGRES_PROFILE = "TEST_POSTGRES_PROFILE"
    TEST_SQLITE_PROFILE = "TEST_SQLITE_PROFILE"

    ALL = [
        LIBRARY_PROFILE, COMMAND_PROFILE, WEB_PROFILE, MATLAB_PROFILE,
        TEST_POSTGRES_PROFILE, TEST_SQLITE_PROFILE, TEST_LIBRARY_PROFILE
    ]

    REGISTERED_PROFILES = {}

    CURRENT_PROFILE_NAME = None

    current = BaseSettingsProfile(False)
    env = Environment()

    @classmethod
    def set_profile(cls, selected_profile, in_operation=False, run_init=True):
        """
        Sets TVB profile and do related initializations.
        """
        if selected_profile is not None:
            cls._load_framework_profiles(selected_profile)
            cls._build_profile_class(selected_profile, in_operation, run_init)

    @classmethod
    def _build_profile_class(cls,
                             selected_profile,
                             in_operation=False,
                             run_init=True):
        """
        :param selected_profile: Profile name to be loaded.
        """

        if selected_profile in cls.REGISTERED_PROFILES:
            current_class = cls.REGISTERED_PROFILES[selected_profile]

            cls.current = current_class()
            cls.CURRENT_PROFILE_NAME = selected_profile

            if in_operation:
                # set flags IN_OPERATION,  before initialize** calls, to avoid LoggingBuilder being created there
                cls.current.prepare_for_operation_mode()

            if cls.env.is_distribution():
                # initialize deployment first, because in case of a contributor setup this tried to reload
                # and initialize_profile loads already too many tvb modules,
                # making the reload difficult and prone to more failures
                cls.current.initialize_for_deployment()
            if run_init:
                cls.current.initialize_profile()

        else:
            msg = "Invalid profile name %r, expected one of %r"
            msg %= (selected_profile, cls.ALL)
            raise Exception(msg)

    @classmethod
    def _load_framework_profiles(cls, new_profile):

        from tvb.basic.config.profile_settings import LibrarySettingsProfile, TestLibraryProfile, MATLABLibraryProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.LIBRARY_PROFILE] = LibrarySettingsProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.TEST_LIBRARY_PROFILE] = TestLibraryProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.MATLAB_PROFILE] = MATLABLibraryProfile

        if not cls.is_library_mode(new_profile):
            try:
                from tvb.config.profile_settings import CommandSettingsProfile, WebSettingsProfile
                from tvb.config.profile_settings import TestPostgresProfile, TestSQLiteProfile

                cls.REGISTERED_PROFILES[
                    TvbProfile.COMMAND_PROFILE] = CommandSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.WEB_PROFILE] = WebSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_POSTGRES_PROFILE] = TestPostgresProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_SQLITE_PROFILE] = TestSQLiteProfile

            except ImportError:
                pass

    @staticmethod
    def is_library_mode(new_profile=None):

        lib_profiles = [
            TvbProfile.LIBRARY_PROFILE, TvbProfile.TEST_LIBRARY_PROFILE
        ]
        result = (new_profile in lib_profiles
                  or (new_profile is None
                      and TvbProfile.CURRENT_PROFILE_NAME in lib_profiles)
                  or not TvbProfile.env.is_framework_present())

        # Make sure default settings are not failing because we are not finding some modules
        if (new_profile is None and TvbProfile.CURRENT_PROFILE_NAME is None
                and not TvbProfile.env.is_framework_present()):
            TvbProfile.set_profile(TvbProfile.LIBRARY_PROFILE)

        return result

    @staticmethod
    def is_first_run():

        return TvbProfile.current.manager.is_first_run()