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

Support YAML in metadata - metadata.yaml #713

Closed
simonw opened this issue Apr 2, 2020 · 6 comments
Closed

Support YAML in metadata - metadata.yaml #713

simonw opened this issue Apr 2, 2020 · 6 comments

Comments

@simonw
Copy link
Owner

simonw commented Apr 2, 2020

I was originally going to do this with a plugin - see #357 - but the more I work with metadata.json the more I want it to just accept YAML as an optional alternative to JSON.

The best example why is still this one: https://github.com/simonw/russian-ira-facebook-ads-datasette/blob/master/russian-ads-metadata.yaml

YAML is just SO much better than JSON for multi-line strings - in particular HTML and SQL, both of which are common in metadata.json files.

@simonw
Copy link
Owner Author

simonw commented Apr 2, 2020

I can add https://pypi.org/project/PyYAML/ as a dependency for this.

One open question: how best to tell the difference between a JSON and a YAML file? I'd rather not do it based on a file extension.

Since I'm going to read the whole file into memory anyway (rather than try to stream it) and I only load it once at startup, maybe I try to parse with one and, if there's a parsing error, try the other one before giving up?

@simonw
Copy link
Owner Author

simonw commented Apr 2, 2020

I'm going to depend on ~=5.3 https://github.com/yaml/pyyaml/blob/master/CHANGES - though
I could probably depend on a much wider set of versions (or maybe even no pinned minimum version at all).

@simonw
Copy link
Owner Author

simonw commented Apr 2, 2020

I'm going to try to parse as JSON first, then fall back to YAML, then error.

@simonw
Copy link
Owner Author

simonw commented Apr 2, 2020

Don't forget to fix the code in publish that loads and modifies metadata, e.g.:

if metadata:
metadata_content = json.load(metadata)
else:
metadata_content = {}
for key, value in extra_metadata.items():
if value:
metadata_content[key] = value

And:

if metadata:
metadata_content = json.load(metadata)
else:
metadata_content = {}
for key, value in extra_metadata.items():
if value:
metadata_content[key] = value
try:
os.chdir(tmp.name)
if metadata_content:
open("metadata.json", "w").write(json.dumps(metadata_content, indent=2))

@simonw
Copy link
Owner Author

simonw commented Apr 2, 2020

def parse_metadata(content):
# content can be JSON or YAML
try:
return json.loads(content)
except json.JSONDecodeError:
try:
return yaml.safe_load(content)
except yaml.YAMLError:
raise BadMetadataError("Metadata is not valid JSON or YAML")

@simonw
Copy link
Owner Author

simonw commented Apr 2, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant