ResearchObject / ro-crate-py

Python library for RO-Crate
https://pypi.org/project/rocrate/
Apache License 2.0
48 stars 25 forks source link

Ensure Entity.properties yields ready-to-write JSON #36

Closed simleo closed 3 years ago

simleo commented 3 years ago

This line in the root dataset code auto-converts datePublished into a Python datetime object: https://github.com/ResearchObject/ro-crate-py/blob/4ab8975b9478b7ba2f13fb0d13bd1b3b5287ac54/rocrate/model/root_dataset.py#L28

Things are fine when writing an RO-Crate, since this is handled in the write method of the Medatata class. However, users might want to generate the JSON metadata for the crate without necessarily writing it out. We should return serialized JSON from the properties method.

simleo commented 3 years ago

The above is also inconsistent with Dataset.datePublished, which assumes that the stored date is a string to be parsed:

    @property
    def datePublished(self):
        date = self["datePublished"]
        return date and datetime.datetime.fromisoformat(date)

Therefore:

>>> from rocrate.model.dataset import Dataset
>>> from rocrate.rocrate import ROCrate
>>> crate = ROCrate()
>>> crate.datePublished
datetime.datetime(2020, 11, 12, 10, 8, 25, 326789)
>>> crate.root_dataset.datePublished
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/ro-crate-py/rocrate/model/dataset.py", line 54, in datePublished
    return date and datetime.datetime.fromisoformat(date)
TypeError: fromisoformat: argument must be str

In addition, fromisoformat is only available in Python 3.7+, and we are supporting Python 3.6.

simleo commented 3 years ago

Yet another problem is that only Metadata.write_zip is writing the datetime in ISO format, while Metadata.write is just using str.