96 lines
3.4 KiB
Python
Executable File
96 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
from typing import List, Optional, Union
|
|
|
|
from pydantic import BaseModel
|
|
from lib_ro_crate_schema.crate.decorators import ro_crate_schema, Field
|
|
from lib_ro_crate_schema.crate.schema_facade import SchemaFacade
|
|
from lib_ro_crate_schema.crate.jsonld_utils import add_schema_to_crate
|
|
from rocrate.rocrate import ROCrate
|
|
from pathlib import Path
|
|
|
|
|
|
@ro_crate_schema(ontology="https://schema.org/PropertyValue")
|
|
class PropertyValue(BaseModel):
|
|
name: str = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/name"})
|
|
value: Union[str, bool, int, float, 'PropertyValue', List['PropertyValue']] = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/value"})
|
|
unitText: Optional[str] = Field(default=None, json_schema_extra={
|
|
"ontology": "https://schema.org/unitText"})
|
|
|
|
|
|
@ro_crate_schema(ontology="https://schema.org/Person")
|
|
class Person(BaseModel):
|
|
name: str = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/name"})
|
|
givenName: str = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/givenName"})
|
|
familyName: str = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/familyName"})
|
|
|
|
|
|
@ro_crate_schema(ontology="https://schema.org/Dataset")
|
|
class Dataset(BaseModel):
|
|
name: str = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/name"})
|
|
description: str = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/description"})
|
|
creator: Union[Person, List[Person]] = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/creator"})
|
|
additionalProperty: Union[PropertyValue, List[PropertyValue]] = Field(json_schema_extra={
|
|
"ontology": "https://schema.org/additionalProperty"})
|
|
|
|
|
|
def main():
|
|
facade = SchemaFacade()
|
|
facade.add_all_registered_models()
|
|
|
|
creator_1 = Person(givenName="John", familyName="Doe", name="Doe, John")
|
|
creator_2 = Person(givenName="Jane", familyName="Doe", name="Doe, Jane")
|
|
|
|
metadata = [
|
|
PropertyValue(name="data_collection", value=[
|
|
PropertyValue(name="wavelength", value=3.14, unitText='angstrom'),
|
|
PropertyValue(name="energy", value=3, unitText='keV'),
|
|
PropertyValue(name="detector", value="Eiger"),
|
|
])
|
|
]
|
|
|
|
dataset = Dataset(name="Aare dataset", description="This dataset comes from a RO-Crate",
|
|
creator=[creator_1, creator_2], additionalProperty=metadata)
|
|
|
|
facade.add_model_instance(dataset, "_:b0")
|
|
|
|
# Create RO-Crate
|
|
crate = ROCrate()
|
|
crate.name = "Minimal Example"
|
|
crate.description = "A minimal RO-Crate with one Person"
|
|
|
|
final_crate = add_schema_to_crate(facade, crate)
|
|
|
|
# Export
|
|
# output_path = Path("output_crates/minimal_example")
|
|
# output_path.mkdir(parents=True, exist_ok=True)
|
|
# final_crate.write(output_path)
|
|
|
|
buffer = b''.join(final_crate.stream_zip())
|
|
import requests
|
|
import os
|
|
response = requests.post(
|
|
"http://pc17166.psi.ch:8080/api/v1/ro-crate/import",
|
|
headers={
|
|
"content-type": "application/zip",
|
|
"accept": "application/json",
|
|
"api-key": os.environ["API_KEY"]
|
|
}, data=final_crate.stream_zip())
|
|
response.raise_for_status()
|
|
print(response.json())
|
|
|
|
# print(f"✓ RO-Crate exported to: {output_path}")
|
|
# print(f"✓ Entities: {len(final_crate.get_entities())}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|