Ejemplo n.º 1
0
def update_config():
    """Add AiiDA profile from environment variables, if specified"""
    from aiida.manage.configuration import load_config
    from aiida.manage.configuration.profile import Profile

    profile_name = os.getenv("AIIDA_PROFILE")
    config = load_config(create=True)
    if profile_name and profile_name not in config.profile_names:
        profile = Profile(
            profile_name, {
                "database_hostname":
                os.getenv("AIIDADB_HOST"),
                "database_port":
                os.getenv("AIIDADB_PORT"),
                "database_engine":
                os.getenv("AIIDADB_ENGINE"),
                "database_name":
                os.getenv("AIIDADB_NAME"),
                "database_username":
                os.getenv("AIIDADB_USER"),
                "database_password":
                os.getenv("AIIDADB_PASS"),
                "database_backend":
                os.getenv("AIIDADB_BACKEND"),
                "default_user":
                os.getenv("default_user_email"),
                "repository_uri":
                "file://{}/.aiida/repository/{}".format(
                    os.getenv("AIIDA_PATH"), profile_name),
            })
        config.add_profile(profile)
        config.set_default_profile(profile_name)
        config.store()

    return config
Ejemplo n.º 2
0
def update_config():
    """Add AiiDA profile from environment variables, if specified"""
    from aiida.manage.configuration import load_config
    from aiida.manage.configuration.profile import Profile

    profile_name = os.getenv('AIIDA_PROFILE')
    config = load_config(create=True)
    if profile_name and profile_name not in config.profile_names:
        profile = Profile(
            profile_name, {
                'database_hostname':
                os.getenv('AIIDADB_HOST'),
                'database_port':
                os.getenv('AIIDADB_PORT'),
                'database_engine':
                os.getenv('AIIDADB_ENGINE'),
                'database_name':
                os.getenv('AIIDADB_NAME'),
                'database_username':
                os.getenv('AIIDADB_USER'),
                'database_password':
                os.getenv('AIIDADB_PASS'),
                'database_backend':
                os.getenv('AIIDADB_BACKEND'),
                'default_user':
                os.getenv('default_user_email'),
                'repository_uri':
                'file://{}/.aiida/repository/{}'.format(
                    os.getenv('AIIDA_PATH'), profile_name),
            })
        config.add_profile(profile)
        config.set_default_profile(profile_name)
        config.store()

    return config
Ejemplo n.º 3
0
def test_merge_deprecated_yaml(tmp_path):
    """Test that an existing 'cache_config.yml' is correctly merged into the main config.

    An AiidaDeprecationWarning should also be raised.
    """
    from aiida.common.warnings import AiidaDeprecationWarning
    from aiida.manage import configuration
    from aiida.manage.configuration import settings, load_profile, reset_profile, get_config_option

    # Store the current configuration instance and config directory path
    current_config = configuration.CONFIG
    current_config_path = current_config.dirpath
    current_profile_name = configuration.PROFILE.name

    try:
        reset_profile()
        configuration.CONFIG = None

        # Create a temporary folder, set it as the current config directory path
        settings.AIIDA_CONFIG_FOLDER = str(tmp_path)
        config_dictionary = json.loads(
            Path(__file__).parent.joinpath(
                'configuration/migrations/test_samples/reference/5.json').
            read_text())
        config_dictionary['profiles']['default'][
            'AIIDADB_REPOSITORY_URI'] = f"file:///{tmp_path/'repo'}"
        cache_dictionary = {
            'default': {
                'default': True,
                'enabled': ['aiida.calculations:quantumespresso.pw'],
                'disabled': ['aiida.calculations:templatereplacer']
            }
        }
        tmp_path.joinpath('config.json').write_text(
            json.dumps(config_dictionary))
        tmp_path.joinpath('cache_config.yml').write_text(
            yaml.dump(cache_dictionary))
        with pytest.warns(AiidaDeprecationWarning, match='cache_config.yml'):
            configuration.CONFIG = configuration.load_config()
        load_profile('default')

        assert get_config_option('caching.default_enabled') is True
        assert get_config_option('caching.enabled_for') == [
            'aiida.calculations:quantumespresso.pw'
        ]
        assert get_config_option('caching.disabled_for') == [
            'aiida.calculations:templatereplacer'
        ]
        # should have now been moved to cache_config.yml.<DATETIME>
        assert not tmp_path.joinpath('cache_config.yml').exists()
    finally:
        # Reset the config folder path and the config instance. Note this will always be executed after the yield no
        # matter what happened in the test that used this fixture.
        reset_profile()
        settings.AIIDA_CONFIG_FOLDER = current_config_path
        configuration.CONFIG = current_config
        load_profile(current_profile_name)
def temporary_config_instance():
    """Create a temporary AiiDA instance."""
    current_config = None
    current_config_path = None
    current_profile_name = None
    temporary_config_directory = None

    from aiida.common.utils import Capturing
    from aiida.manage import configuration
    from aiida.manage.configuration import settings, load_profile, reset_profile

    try:
        from aiida.manage.configuration.settings import create_instance_directories

        # Store the current configuration instance and config directory path
        current_config = configuration.CONFIG
        current_config_path = current_config.dirpath
        current_profile_name = configuration.PROFILE.name

        reset_profile()
        configuration.CONFIG = None

        # Create a temporary folder, set it as the current config directory path and reset the loaded configuration
        profile_name = 'test_profile_1234'
        temporary_config_directory = tempfile.mkdtemp()
        settings.AIIDA_CONFIG_FOLDER = temporary_config_directory

        # Create the instance base directory structure, the config file and a dummy profile
        create_instance_directories()

        # The constructor of `Config` called by `load_config` will print warning messages about migrating it
        with Capturing():
            configuration.CONFIG = configuration.load_config(create=True)

        profile = create_mock_profile(
            name=profile_name, repository_dirpath=temporary_config_directory)

        # Add the created profile and set it as the default
        configuration.CONFIG.add_profile(profile)
        configuration.CONFIG.set_default_profile(profile_name, overwrite=True)
        configuration.CONFIG.store()
        load_profile()

        yield configuration.CONFIG
    finally:
        # Reset the config folder path and the config instance
        reset_profile()
        settings.AIIDA_CONFIG_FOLDER = current_config_path
        configuration.CONFIG = current_config
        load_profile(current_profile_name)

        # Destroy the temporary instance directory
        if temporary_config_directory and os.path.isdir(
                temporary_config_directory):
            shutil.rmtree(temporary_config_directory)
Ejemplo n.º 5
0
def empty_config(tmp_path) -> Config:
    """Create a temporary configuration instance.

    This creates a temporary directory with a clean `.aiida` folder and basic configuration file. The currently loaded
    configuration and profile are stored in memory and are automatically restored at the end of this context manager.

    :return: a new empty config instance.
    """
    from aiida.common.utils import Capturing
    from aiida.manage import configuration
    from aiida.manage.configuration import settings, reset_profile

    # Store the current configuration instance and config directory path
    current_config = configuration.CONFIG
    current_config_path = current_config.dirpath
    current_profile_name = configuration.PROFILE.name

    reset_profile()
    configuration.CONFIG = None

    # Create a temporary folder, set it as the current config directory path and reset the loaded configuration
    settings.AIIDA_CONFIG_FOLDER = str(tmp_path)

    # Create the instance base directory structure, the config file and a dummy profile
    settings.create_instance_directories()

    # The constructor of `Config` called by `load_config` will print warning messages about migrating it
    with Capturing():
        configuration.CONFIG = configuration.load_config(create=True)

    yield get_config()

    # Reset the config folder path and the config instance. Note this will always be executed after the yield no
    # matter what happened in the test that used this fixture.
    reset_profile()
    settings.AIIDA_CONFIG_FOLDER = current_config_path
    configuration.CONFIG = current_config
    load_profile(current_profile_name)
Ejemplo n.º 6
0
import panel as pn
from plots_host_system.host_crystal_structure import create_structure_plot, prepare_plotting_structure
from plots_host_system.bandstructure import plot_bandstruc, add_ef_lines_bandstruc
from plots_host_system.host_dos import plot_dos
from plots_host_system import host_system_header, legend
from about import judit_footer, judit_header

from aiida.manage.configuration import load_config, load_profile
from aiida.manage.configuration.profile import Profile
import os

config = load_config(create=True)

profile_name = os.getenv("AIIDA_PROFILE")

profile = Profile(
    profile_name, {
        "database_hostname":
        os.getenv("AIIDADB_HOST"),
        "database_port":
        os.getenv("AIIDADB_PORT"),
        "database_engine":
        os.getenv("AIIDADB_ENGINE"),
        "database_name":
        os.getenv("AIIDADB_NAME"),
        "database_username":
        os.getenv("AIIDADB_USER"),
        "database_password":
        os.getenv("AIIDADB_PASS"),
        "database_backend":
        os.getenv("AIIDADB_BACKEND"),