-
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
CLI: verdi export to a yaml file with keys from code class #5860
Conversation
A mypy error found, how can I fix it? @chrisjsewell
|
I don't see this on the actual CI pre-commit run? |
Ah, my bad. I didn't check the failed pre-commit action very carefully. It is different from I run mypy locally. I know how to fix other issues that fail the pre-commit action. Thanks for help! |
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 @unkcpz , couple comments. As for having a separate command to export, or an option to verdi code show
, I would be ok with both, but I think the choice of this PR might be better in hindsight. Seems more explicit and more easy to find.
filepath_executable: /bin/cat | ||
label: code | ||
prepend_text: '' | ||
use_double_quotes: 'False' |
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.
Will this be correctly interpreted? Should this not be a literal false
or at least without quotes? Ideal would be to have a round-trip test and call verdi code create
with this yaml and see if you get equivalent codes
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.
Yes, the False
as a string is working, I add a round trip test for it.
else: | ||
value = getattr(code, key) | ||
|
||
code_data[key] = str(value) |
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.
Do we need to convert to str
? Should the yaml serializer not take care of this. Then the boolean question would be solved in any case. I don't think we have codes that have any attributes that are not YAML-serializable. I think the data is guaranteed to be JSON-serializable, since it comes from the database, so it should also be YAML-serializable.
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.
The problem comes from the filepath_executable
which I read by code._get_cli_options
. The type is PurePosixPath
which is not yaml-serializable. I can make another elif for this, but I think should be fine that all converted to string?
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.
Yeah I agree, fine to keep the cast to str then as long as the round-trip works
aiida/cmdline/commands/cmd_code.py
Outdated
cli_options = code._get_cli_options() | ||
code_data = {} | ||
|
||
for key in cli_options.keys(): |
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.
cli_options = code._get_cli_options() | |
code_data = {} | |
for key in cli_options.keys(): | |
code_data = {} | |
for key in code._get_cli_options().keys(): |
aiida/cmdline/commands/cmd_code.py
Outdated
code_data[key] = str(value) | ||
|
||
with open(output_file, 'w') as fh: | ||
fh.write(yaml.dump(code_data, indent=4)) |
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.
Since the data structure is flat, the indent seems superfluous
fh.write(yaml.dump(code_data, indent=4)) | |
fh.write(yaml.dump(code_data)) |
8481604
to
210b1f7
Compare
Hi @sphuber, I add a test to setup from the file generated. Fix the pre-commit warning by rename the |
|
||
@classmethod | ||
def _get_cli_options(cls) -> dict: |
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.
This change is incorrect. The idea is that it is private and that subclasses override this. The base class already defines the public get_cli_options
which should be used by users of this class
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.
Ah, I didn't notice that, shame failure. Thanks!
210b1f7
to
d646cdb
Compare
tests/cmdline/commands/test_code.py
Outdated
) | ||
new_code = load_code(new_label) | ||
assert new_code.use_double_quotes is False | ||
assert isinstance(load_code(new_label), InstalledCode) |
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.
assert isinstance(load_code(new_label), InstalledCode) | |
assert isinstance(new_code, InstalledCode) |
tests/cmdline/commands/test_code.py
Outdated
cmd_code.code_create, ['core.code.installed', '--non-interactive', '--config', filepath, '--label', new_label] | ||
) | ||
new_code = load_code(new_label) | ||
assert new_code.use_double_quotes is False |
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.
I take it that assert code == new_code
won't work since the AbstractCode
doesn't implement the __eq__
method. But maybe instead of comparing just this one attribute arbitrarily, we do assert code.base.attributes.all() == new_code.base.attributes.all()
? That should cover at least all attributes, which for at least the InstalledCode
should be all relevant data.
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 @unkcpz some final minor comments
tests/cmdline/commands/test_code.py
Outdated
@@ -236,6 +236,24 @@ def test_code_duplicate_ignore(run_cli_command, aiida_local_code_factory, non_in | |||
assert duplicate.description == '' | |||
|
|||
|
|||
def test_code_export(run_cli_command, aiida_local_code_factory, tmp_path, file_regression): | |||
"""Test export the code setup to str.""" | |||
code = aiida_local_code_factory('core.arithmetic.add', '/bin/cat', label='code') |
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.
Can you please add some data for append_text
and prepend_text
. Ideally some multiline string with maybe some leading whitespace. So e.g.:
prepend_text = 'module load something\n some command'
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.
Yes, added.
fixes aiidateam#3521 Add `verdi code export` command to export code from command line as a ymal file. This is mentioned in usability improvement as well as having a command to export the code and computer setup. Keys of YAML file are read from the cli option of the corresponding code class.
f5dd578
to
48d4e0b
Compare
Hi @sphuber, I also prepare an AEP draft for how I want to tackle the computer/code setup in the end, aiidateam/AEP#37. |
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 for the changes @unkcpz , good to go now
Addresses #3521 partially.
This is mentioned in usability improvement as well as having a command to export the code and computer setup.
Keys of YAML file are read from the cli option of the corresponding code class.
However, for the
prepend_text
andappend_text
I did not implement polish of the yaml output. If they are multiline content, the value of the text in YAML file won't be properly indented. For example, see the output YAML below:where the
prepend_text
of original config yaml file is:When I look at #3616, I find @sphuber tried to put into subcommand
show
format options to dump as YAML. It can also be an option.