-
Notifications
You must be signed in to change notification settings - Fork 192
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: gracefully exit if no profile exists #5874
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cheers, thanks for your contribution @zahid47 !
Just one minor comment
@sphuber not for this PR, but we may want to generalize the concept of commands that require an active profile (and exit if no profile is found without the command having to implement this) |
I agree. We should probably have a |
That sounds like the right solution |
Am I on the right track? (I'm not very familiar with the codebase, and this is my first time "contributing" to open source, so any help is appreciated!!) def requires_profile():
"""Function decorator to check that a command requires a profile to be defined and loaded.
Example::
@requires_profile()
def mycommand():
pass
"""
@decorator
def wrapper(wrapped, _, args, kwargs):
"""Check if a profile is set up and loaded. If not, echo a critical error and abort."""
manager = get_manager()
config = manager.get_config()
# not checking using manager.get_profile() because we only care about at least one profile to be defined
profiles = [profile for profile in config.profiles if not profile.is_test_profile]
# do I need to check if the profile is loaded here somehow?
if not profiles:
echo.echo_critical('No profiles defined.')
return wrapped(*args, **kwargs)
return wrapper |
You are not very far off @zahid47 . I think in general, most commands that would need such a decorator just need to check that a profile is loaded. So I would implement as the following: def requires_loaded_profile():
"""Function decorator for CLI command that requires a profile to be loaded.
Example::
@requires_loaded_profile()
def create_node():
pass
If no profile has been loaded, the command will exit with a critical error. Most ``verdi`` commands will
automatically load the default profile. So if this error is hit, it is most likely that either no profile have been
defined at all or the default is unspecified.
"""
from wrapt import decorator
@decorator
def wrapper(wrapped, _, args, kwargs):
"""If daemon pid file is not found / empty, echo message and call decorated function."""
from aiida.manage.configuration import get_profile
if get_profile() is None:
echo.echo_critical('No profile loaded: make sure at least one profile is configured and a default is set.')
return wrapped(*args, **kwargs)
return wrapper Which basically just checks that a profile has been loaded. If you can add this in |
…into no-profile-fix
for more information, see https://pre-commit.ci
Looks like the installation of the pre-commit hook itself failed? Never seen that before. In any case, I believe this is good to merge. Thank you for your contribution @zahid47, we will make good use of this decorator across the verdi cli. Welcome to the world of open-source contributors! |
It's a problem with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the contribution @zahid47
requested changes have been addressed
fixes #5869