-
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
Verdi: Warn for already existing code@computer label
#5194
Verdi: Warn for already existing code@computer label
#5194
Conversation
…e setup`` and ``duplicate`` Implement reprompting mechanism in ``verdi code setup`` for code label/computer combinations that are already in the database. The Computer option gains callback checking the DB for the set code label/computer combination If it is found the user is asked if he would like to change the label. If that is the case the label is set to `None`, which triggers a second label argument, which will perform the same check as the computer option but it will raise an error and reprompt instead. This second label argument is ``hidden=True, expose_value=False`` to avoid duplicate help messages and name clashes with the other label option Works both for remote and local codes. For non-interactive commands an error is thrown
Codecov Report
@@ Coverage Diff @@
## develop #5194 +/- ##
===========================================
+ Coverage 81.00% 81.02% +0.03%
===========================================
Files 535 535
Lines 37401 37453 +52
===========================================
+ Hits 30292 30342 +50
- Misses 7109 7111 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Hey @janssenhenning thanks for the contribution! I have started to look into the modifications and I have one general question before going more in depth: why did you need to create the If that is the only reason I would be inclined to suggest you just switch the order and have the computer be the first thing to be prompted. This would also require to change the "Installed on target computer?" question too, but I think it makes sense to ask this first, then the computer/path and then the label. I can check with the other devs if they see any problem with this change, but would this solve the issue of the duplicated option? |
Yes, If I remember correctly this was the only reason for the Should I update the PR to change the order or should I wait if there are any problems with this? |
I would say wait for just a bit, both so I can check this (although I'm pretty sure it would be ok), but more importantly because they are about to merge some changes in click (see PR #5111 ) and you may have to adapt something for that as well, so no point in doing double work. |
Thanks for the PR @janssenhenning . PR #5111 has now been merged. However, I am not sure that we should go in the direction of adding a new parameter. It makes things a lot more complex and I am hoping there is a simpler solution. Ideally the validation is done in a Consider the following test script simplifying the situation: #!/usr/bin/env python
import click
from aiida.cmdline.params.options.interactive import InteractiveOption
from aiida.cmdline.params.types import ComputerParamType
from aiida.orm import Code, Computer, QueryBuilder
def validate_label(ctx, param, value):
computer = ctx.params.get('computer', None)
if computer is not None:
query = QueryBuilder()
query.append(Computer, filters={'id': computer.pk}, tag='computer')
query.append(Code, with_computer='computer', filters={'label': value})
if query.first() is not None:
raise click.BadParameter(f'the code `{value}@{computer.label}` already exists.')
return value
@click.option('--label', type=str, cls=InteractiveOption, prompt='Label', callback=validate_label)
@click.option('--computer', type=ComputerParamType(), cls=InteractiveOption, prompt='Computer')
@click.command()
def main(label, computer):
print(label, computer)
if __name__ == '__main__':
main() It seems that by specifying
It fails the validation and then reprompts, so this works. It also work in non-interactive mode:
However, this is only because the
So this is the only remaining problem here. One possible solution is to manually call the validator again in the command body: @click.option('--label', type=str, cls=InteractiveOption, prompt='Label', callback=validate_label)
@click.option('--computer', type=ComputerParamType(), cls=InteractiveOption, prompt='Computer')
@click.command()
@click.pass_context
def main(ctx, label, computer):
validate_label(ctx, None, label)
print(label, computer) Now the validation is called independent of the order in which the parameters are specified:
The only downside I can see in this approach is that it calls the validator twice, but the first time will be very cheap, so the overhead should be minimal. What do you guys think? Is there a problem that I missed? |
There may be even a more elegant solution to the parsing order of the non-interactive case. There is the option |
Worked out my idea in an alternative implementation in #5205 @janssenhenning and @ramirezfranciscof |
Hey @janssenhenning , sorry but it seems that apparently there were some other details that needed to be sorted out together with this issue 😅 (see for example #5204) so we ended up having to take more personal control of this feature, I hope that is ok. We anyways appreciate a lot the contribution and the initiative to participate in this project! Feel free to check the merged PR (#5205) in case there is any aspect you find still missing from that (and if not, then I'll let you close this PR). |
@janssenhenning I had written a message here before merging #5205 but apparently that never made it to the server, not sure what happened. Anyway, I wrote to thank you for your contribution and that the reason I opened an alternative PR was not my original intention. When @ramirezfranciscof asked me whether the suggested approach was feasible, I remembered having looked into this and found issues due to how |
@ramirezfranciscof @sphuber Absolutely no problem. I had this lying around for quite some time. I used it to get some practice with Looking at #5205 what happens if you store the code in the database so |
You are absolutely right @janssenhenning that is something we missed. This distinction between "remote" and "local" codes has always been confusing, especially when you have "remote" codes on localhost... I have opened #5215 with a potential fix. It would be great if you could have a look and see if you can find any problems. |
See #3303
Implement reprompting mechanism in
verdi code setup
andverdi code duplicate
for code label/computer combinations that are already in the database. The Computer option gains callback checking the DB for the set code label/computer combinationIf it is found the user is asked if he would like to change the label. If that is the case the label is set to
None
, which triggers a second label argument, which will perform the same check as the computer option but it will raise an error and reprompt instead. This second label argument ishidden=True, expose_value=False
to avoid duplicate help messages and name clashes with the other label optionWorks both for remote and local codes. For non-interactive commands an error is thrown