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

feat: ✨ add write_package_properties() #984

Open
wants to merge 102 commits into
base: main
Choose a base branch
from

Conversation

martonvago
Copy link
Contributor

@martonvago martonvago commented Jan 21, 2025

Description

This PR adds the write_package_properties function. This will be used to complete the package guide.

This PR needs an in-depth review.

Checklist

  • Added or updated tests
  • Updated documentation
  • Ran just run-all

martonvago and others added 30 commits December 19, 2024 15:03
Co-authored-by: Luke W. Johnston <lwjohnst86@users.noreply.github.com>
Co-authored-by: Signe Kirk Brødbæk <40836345+signekb@users.noreply.github.com>
Co-authored-by: Signe Kirk Brødbæk <40836345+signekb@users.noreply.github.com>
@martonvago martonvago self-assigned this Jan 21, 2025
def write_package_properties(path: Path, properties: PackageProperties) -> Path:
"""Writes the specified package properties to the `datapackage.json` file.

Writes a set of package properties (with or without resource properties) back to the
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As we're rewriting the entire file/object, it made sense for me to allow resource properties to be present but to not require them necessarily. Maybe this (and all our recent changes) will allow us to simplify write_resource_properties.

Copy link
Member

Choose a reason for hiding this comment

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

Yea, I'm currently going through the design interface data package section. Does it make sense that this function overwrites the file? It keeps it simple. But how might we keep it from overwriting the resources if there are any? In those situations where they read -> edit -> write.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that with our current setup, you are not expected to use the write_package_properties function to edit AND write, as you could, as you say, accidentally delete your resource properties.
But if you edit THEN write, then it's all good, because edit_package_properties doesn't modify existing resource properties.

More generally, maybe it would make more sense to have a single function for writing to the datapackage.json file. Then it would be very obvious that whatever you give to that function will be written to file as is, no merging, no clever updates. This function pretty much does this already.
I don't think we're likely to implement a write function that touches only the bits of the file we want to change?

Copy link
Member

Choose a reason for hiding this comment

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

So I understand what you're saying. You mean we have a single write_properties() function, that overwrites everything? Yea, that makes sense. Seems that it might be a good idea to include some version controlling here though...!!! That's for later hahah mmmmmm.

And then we have the edit_ function to make edits to specific fields that we give to write_properties()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah!! I put this random comment on another PR that pretty much says the same thing 😁

def test_rewrites_resource_properties_when_file_exists(path, package_properties):
"""Should write properties to file when the file already exists, updating existing
resource properties."""
old_properties = replace(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just found this function, pretty useful!! 🛺

"""Should throw `CheckError`s if there are errors in the package properties."""
package_properties.name = "invalid name with spaces"
package_properties.id = None
write_json(resource_properties.compact_dict, path)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to test that the file is not rewritten when the check fails.

@martonvago martonvago marked this pull request as ready for review January 21, 2025 13:13
@martonvago martonvago requested a review from a team as a code owner January 21, 2025 13:13
Copy link
Member

@lwjohnst86 lwjohnst86 left a comment

Choose a reason for hiding this comment

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

Very nice! Some small comments

Comment on lines +30 to +33
check_properties(
properties,
ignore=[CheckErrorMatcher(validator="required", json_path="resources")],
)
Copy link
Member

Choose a reason for hiding this comment

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

Very neat!

def write_package_properties(path: Path, properties: PackageProperties) -> Path:
"""Writes the specified package properties to the `datapackage.json` file.

Writes a set of package properties (with or without resource properties) back to the
Copy link
Member

Choose a reason for hiding this comment

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

Yea, I'm currently going through the design interface data package section. Does it make sense that this function overwrites the file? It keeps it simple. But how might we keep it from overwriting the resources if there are any? In those situations where they read -> edit -> write.

seedcase_sprout/core/write_package_properties.py Outdated Show resolved Hide resolved
tests/core/test_write_package_properties.py Outdated Show resolved Hide resolved
Co-authored-by: Luke W. Johnston <lwjohnst86@users.noreply.github.com>
@martonvago martonvago requested a review from lwjohnst86 January 22, 2025 11:27
@lwjohnst86 lwjohnst86 changed the title feat: ✨ add write_package_properties feat: ✨ add write_package_properties() Jan 23, 2025
Base automatically changed from refactor/use-check-error-matcher to main January 23, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants