-
Notifications
You must be signed in to change notification settings - Fork 60
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
Asdftool edit #873
Asdftool edit #873
Conversation
…o larger edited text, the block index at the end will need to be re-computed. Currently, if one exists, it is simply written out, but it is wrong.
…ame or fewer YAML characters as in the original ASDF file.
…t now rewrites small files, with a buffer for future editing, as well as a newly computed block index.
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.
Finally, I have a vague sense that some of this code is similar to what is in AsdfFile and there is going to be unnecessary duplication. On the other hand, what is in AsdfFile doesn't quite meet the needs of this and I think it would need a little reworking to be useful for this context. I wouldn't expect that you would have to do that reworking. Perhaps some future refactoring?
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 is a good start, in general I think we should take more opportunities for code sharing between edit.py and asdf.py/block.py.
Also, just a heads up, we're going to need to figure out some pytest tests for this, but that should wait until we're all agreed on the implementation.
asdf/commands/edit.py
Outdated
bindex = [start] | ||
k = 0 | ||
while len(asdf_blocks) - k > min_header : | ||
hsz = struct.unpack(">H",asdf_blocks[k+bmlen:k+bmlen+2])[0] |
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.
There is a header struct-like thing in the Block class that we can reuse here.
asdf/commands/edit.py
Outdated
return False | ||
|
||
|
||
def validate_asdf_file ( fd ) : |
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 function on the whole seems like it can be shared with asdf.py.
…ng, as well as modified file name usage, so each test had its own set of unique file names that may have been causing problems during testings.
… creating the base ASDF file.
…o using 'os.replace'.
…eate the ASDF file to be edited by using asdf.write_to instead of simply copying the base ASDF using shutil.
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 don't think the request to bypass validation on opening should hold up acceptance.
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 streaming block case is a serious problem, and IMO we should still change that padding character. Can you also look back over the comments from previous rounds of review? GitHub marks them as "outdated" when the file changes but some of them haven't actually been resolved.
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 looks really good. Just a couple specific comments below.
And a general comment - it looks like there are a lot of print()
statements here when handling errors, and this is the case with a bunch of the other asdftool
subcommands as well. It might be better to use the logging
module I think, but perhaps that ship has sailed. =)
…es for edited YAML portions smaller or equal to the existing YAML in an ASDF file are complete.
…streaming case. Added a test for pytesting.
…or the first binary block occurs.
@kmacdonald-stsci I discovered a bug in testing the tool, kind of a silly case but I think we should handle it:
import asdf
with asdf.AsdfFile() as af:
af["foo"] = "bar"
af.write_to("test.asdf")
ASDF files without blocks will become more common after we change the ndarray auto-inline threshold to 100, which I think I heard is coming soon. |
…ining no binary blocks. Added tests for these conditions.
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 think this turned out great! Thanks for bearing with all the review. I suspect we'll want the next version (that launches an editor automatically) to be able to work with files without binary blocks, but the current solution is fine for now.
I have added an 'edit' subcommand to the asdftool. It separates the ASDF markings and YAML text into a separate file to be edited, using
Once editing the YAML is complete, use
There may be a gotcha when saving the edited YAML back to ASDF file. If the edited YAML contains for characters than the number of characters before the binary blocks in the ASDF file, an error message is returned.