Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix aiida.cmdline.utils.decorators.load_backend_if_not_loaded #4878

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions aiida/cmdline/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
code branch gets visited and possibly avoiding the overhead if not

"""

from contextlib import contextmanager

from click_spinner import spinner
Expand All @@ -33,18 +32,26 @@


def load_backend_if_not_loaded():
"""Load the current profile if necessary while running the spinner to show command hasn't crashed."""
from aiida.manage.configuration import load_profile, PROFILE
"""Load the database backend environment for the currently loaded profile.

If no profile has been loaded yet, the default profile will be loaded first. A spinner will be shown during both
actions to indicate that the function is working and has not crashed, since loading can take a second.
"""
from aiida.manage.configuration import get_profile, load_profile
from aiida.manage.manager import get_manager

if PROFILE is None:
manager = get_manager()

if get_profile() is None or not manager.backend_loaded:
with spinner():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, do we really have to load two spinners now.
Maybe just do if get_profile() is None or not manager.backend_loaded

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then always call load_profile relying on its internal shortcut to not load again if already loaded?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeh that better cheers

load_profile()
get_manager().get_backend()
load_profile() # This will load the default profile if no profile has already been loaded
manager.get_backend() # This will load the backend of the loaded profile, if not already loaded


def with_dbenv():
"""Function decorator that will load the database environment only when the function is called.
"""Function decorator that will load the database environment for the currently loaded profile.

.. note:: if no profile has been loaded yet, the default profile will be loaded first.

Example::

Expand Down