diff --git a/pylipd/classes/calibration.py b/pylipd/classes/calibration.py index 947bf68..80b9a0f 100644 --- a/pylipd/classes/calibration.py +++ b/pylipd/classes/calibration.py @@ -287,6 +287,74 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Calibration': + self = Calibration() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "datasetRange": + value = pvalue + obj = value + self.datasetRange = obj + elif key == "doi": + value = pvalue + obj = value + self.dOI = obj + elif key == "equation": + value = pvalue + obj = value + self.equation = obj + elif key == "equationIntercept": + value = pvalue + obj = value + self.datasetRange = obj + elif key == "equationR2": + value = pvalue + obj = value + self.datasetRange = obj + elif key == "equationSlope": + value = pvalue + obj = value + self.datasetRange = obj + elif key == "equationSlopeUncertainty": + value = pvalue + obj = value + self.datasetRange = obj + elif key == "hasSeasonality": + value = pvalue + obj = value + self.seasonality = obj + elif key == "method": + value = pvalue + obj = value + self.datasetRange = obj + elif key == "methodDetail": + value = pvalue + obj = value + self.datasetRange = obj + elif key == "notes": + value = pvalue + obj = value + self.notes = obj + elif key == "proxyDataset": + value = pvalue + obj = value + self.proxyDataset = obj + elif key == "targetDataset": + value = pvalue + obj = value + self.targetDataset = obj + elif key == "uncertainty": + value = pvalue + obj = value + self.uncertainty = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/changelog.py b/pylipd/classes/changelog.py index 9f3a2c3..4dbdedb 100644 --- a/pylipd/classes/changelog.py +++ b/pylipd/classes/changelog.py @@ -123,6 +123,26 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'ChangeLog': + self = ChangeLog() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "changes": + value = pvalue + obj = value + self.changes = obj + elif key == "notes": + value = pvalue + obj = value + self.notes = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/chrondata.py b/pylipd/classes/chrondata.py index 566b8c4..7c160ca 100644 --- a/pylipd/classes/chrondata.py +++ b/pylipd/classes/chrondata.py @@ -120,19 +120,13 @@ def to_json(self): if len(self.measurementTables): data["measurementTable"] = [] for value_obj in self.measurementTables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["measurementTable"].append(obj) if len(self.modeledBy): data["model"] = [] for value_obj in self.modeledBy: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["model"].append(obj) for key in self.misc: @@ -141,6 +135,26 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'ChronData': + self = ChronData() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "measurementTable": + for value in pvalue: + obj = DataTable.from_json(value) + self.measurementTables.append(obj) + elif key == "model": + for value in pvalue: + obj = Model.from_json(value) + self.modeledBy.append(obj) + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/compilation.py b/pylipd/classes/compilation.py index 8c46ab2..43a7172 100644 --- a/pylipd/classes/compilation.py +++ b/pylipd/classes/compilation.py @@ -125,6 +125,26 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Compilation': + self = Compilation() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "compilationName": + value = pvalue + obj = value + self.name = obj + elif key == "compilationVersion": + value = pvalue + obj = value + self.version = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/dataset.py b/pylipd/classes/dataset.py index fca67fb..53a620f 100644 --- a/pylipd/classes/dataset.py +++ b/pylipd/classes/dataset.py @@ -436,71 +436,47 @@ def to_json(self): if len(self.chronData): data["chronData"] = [] for value_obj in self.chronData: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["chronData"].append(obj) if len(self.creators): data["creator"] = [] for value_obj in self.creators: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["creator"].append(obj) if len(self.fundings): data["funding"] = [] for value_obj in self.fundings: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["funding"].append(obj) if len(self.investigators): data["investigator"] = [] for value_obj in self.investigators: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["investigator"].append(obj) if len(self.paleoData): data["paleoData"] = [] for value_obj in self.paleoData: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["paleoData"].append(obj) if len(self.publications): data["pub"] = [] for value_obj in self.publications: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["pub"].append(obj) if self.archiveType: value_obj = self.archiveType - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["archiveType"] = obj if self.changeLog: value_obj = self.changeLog - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["changelog"] = obj if self.collectionName: @@ -515,10 +491,7 @@ def to_json(self): if self.contributor: value_obj = self.contributor - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["dataContributor"] = obj if self.dataSource: @@ -533,10 +506,7 @@ def to_json(self): if self.location: value_obj = self.location - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["geo"] = obj if self.name: @@ -570,6 +540,94 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Dataset': + self = Dataset() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "archiveType": + value = pvalue + obj = ArchiveType.from_synonym(re.sub("^.*?#", "", value)) + self.archiveType = obj + elif key == "changelog": + value = pvalue + obj = ChangeLog.from_json(value) + self.changeLog = obj + elif key == "chronData": + for value in pvalue: + obj = ChronData.from_json(value) + self.chronData.append(obj) + elif key == "collectionName": + value = pvalue + obj = value + self.collectionName = obj + elif key == "collectionYear": + value = pvalue + obj = value + self.collectionYear = obj + elif key == "creator": + for value in pvalue: + obj = Person.from_json(value) + self.creators.append(obj) + elif key == "dataContributor": + value = pvalue + obj = Person.from_json(value) + self.contributor = obj + elif key == "dataSetName": + value = pvalue + obj = value + self.name = obj + elif key == "dataSetVersion": + value = pvalue + obj = value + self.version = obj + elif key == "dataSource": + value = pvalue + obj = value + self.dataSource = obj + elif key == "datasetId": + value = pvalue + obj = value + self.datasetId = obj + elif key == "funding": + for value in pvalue: + obj = Funding.from_json(value) + self.fundings.append(obj) + elif key == "geo": + value = pvalue + obj = Location.from_json(value) + self.location = obj + elif key == "googleSpreadSheetKey": + value = pvalue + obj = value + self.spreadsheetLink = obj + elif key == "investigator": + for value in pvalue: + obj = Person.from_json(value) + self.investigators.append(obj) + elif key == "notes": + value = pvalue + obj = value + self.notes = obj + elif key == "originalDataURL": + value = pvalue + obj = value + self.originalDataUrl = obj + elif key == "paleoData": + for value in pvalue: + obj = PaleoData.from_json(value) + self.paleoData.append(obj) + elif key == "pub": + for value in pvalue: + obj = Publication.from_json(value) + self.publications.append(obj) + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/datatable.py b/pylipd/classes/datatable.py index b9017c8..ebd4afa 100644 --- a/pylipd/classes/datatable.py +++ b/pylipd/classes/datatable.py @@ -135,10 +135,7 @@ def to_json(self): if len(self.variables): data["columns"] = [] for value_obj in self.variables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["columns"].append(obj) if self.fileName: @@ -157,6 +154,30 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'DataTable': + self = DataTable() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "columns": + for value in pvalue: + obj = Variable.from_json(value) + self.variables.append(obj) + elif key == "filename": + value = pvalue + obj = value + self.fileName = obj + elif key == "missingValue": + value = pvalue + obj = value + self.missingValue = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/funding.py b/pylipd/classes/funding.py index e5cffa4..0a24038 100644 --- a/pylipd/classes/funding.py +++ b/pylipd/classes/funding.py @@ -156,10 +156,7 @@ def to_json(self): if len(self.investigators): data["investigator"] = [] for value_obj in self.investigators: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["investigator"].append(obj) if self.fundingAgency: @@ -178,6 +175,34 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Funding': + self = Funding() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "agency": + value = pvalue + obj = value + self.fundingAgency = obj + elif key == "country": + value = pvalue + obj = value + self.fundingCountry = obj + elif key == "grant": + for value in pvalue: + obj = value + self.grants.append(obj) + elif key == "investigator": + for value in pvalue: + obj = Person.from_json(value) + self.investigators.append(obj) + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/independentvariable.py b/pylipd/classes/independentvariable.py index 8ac3358..1dbd040 100644 --- a/pylipd/classes/independentvariable.py +++ b/pylipd/classes/independentvariable.py @@ -169,6 +169,34 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'IndependentVariable': + self = IndependentVariable() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "basis": + value = pvalue + obj = value + self.relevantQuote = obj + elif key == "direction": + value = pvalue + obj = value + self.interpretationDirection = obj + elif key == "mathematicalRelation": + value = pvalue + obj = value + self.equation = obj + elif key == "rank": + value = pvalue + obj = value + self.rank = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/integrationtime.py b/pylipd/classes/integrationtime.py index 8aadae0..29f8ce4 100644 --- a/pylipd/classes/integrationtime.py +++ b/pylipd/classes/integrationtime.py @@ -134,10 +134,7 @@ def to_json(self): if len(self.independentVariables): data["independentVariable"] = [] for value_obj in self.independentVariables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["independentVariable"].append(obj) if self.relevantQuote: @@ -147,10 +144,7 @@ def to_json(self): if self.units: value_obj = self.units - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["units"] = obj for key in self.misc: @@ -159,6 +153,30 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'IntegrationTime': + self = IntegrationTime() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "basis": + value = pvalue + obj = value + self.relevantQuote = obj + elif key == "independentVariable": + for value in pvalue: + obj = IndependentVariable.from_json(value) + self.independentVariables.append(obj) + elif key == "units": + value = pvalue + obj = PaleoUnit.from_synonym(re.sub("^.*?#", "", value)) + self.units = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/interpretation.py b/pylipd/classes/interpretation.py index 8f75859..5c02fde 100644 --- a/pylipd/classes/interpretation.py +++ b/pylipd/classes/interpretation.py @@ -343,10 +343,7 @@ def to_json(self): if self.integrationTime: value_obj = self.integrationTime - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["integrationTime"] = obj if self.interpretationDirection: @@ -381,34 +378,22 @@ def to_json(self): if self.seasonality: value_obj = self.seasonality - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["seasonality"] = obj if self.seasonalityGeneral: value_obj = self.seasonalityGeneral - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["seasonalityGeneral"] = obj if self.seasonalityOriginal: value_obj = self.seasonalityOriginal - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["seasonalityOriginal"] = obj if self.variable: value_obj = self.variable - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["variable"] = obj if self.variableDetail: @@ -432,6 +417,78 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Interpretation': + self = Interpretation() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "basis": + value = pvalue + obj = value + self.basis = obj + elif key == "direction": + value = pvalue + obj = value + self.interpretationDirection = obj + elif key == "integrationTime": + value = pvalue + obj = IntegrationTime.from_json(value) + self.integrationTime = obj + elif key == "local": + value = pvalue + obj = value + self.local = obj + elif key == "mathematicalRelation": + value = pvalue + obj = value + self.mathematicalRelation = obj + elif key == "notes": + value = pvalue + obj = value + self.notes = obj + elif key == "rank": + value = pvalue + obj = value + self.rank = obj + elif key == "scope": + value = pvalue + obj = value + self.scope = obj + elif key == "seasonality": + value = pvalue + obj = InterpretationSeasonality.from_synonym(re.sub("^.*?#", "", value)) + self.seasonality = obj + elif key == "seasonalityGeneral": + value = pvalue + obj = InterpretationSeasonality.from_synonym(re.sub("^.*?#", "", value)) + self.seasonalityGeneral = obj + elif key == "seasonalityOriginal": + value = pvalue + obj = InterpretationSeasonality.from_synonym(re.sub("^.*?#", "", value)) + self.seasonalityOriginal = obj + elif key == "variable": + value = pvalue + obj = InterpretationVariable.from_synonym(re.sub("^.*?#", "", value)) + self.variable = obj + elif key == "variableDetail": + value = pvalue + obj = value + self.variableDetail = obj + elif key == "variableGeneral": + value = pvalue + obj = value + self.variableGeneral = obj + elif key == "variableGeneralDirection": + value = pvalue + obj = value + self.variableGeneralDirection = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/isotopeinterpretation.py b/pylipd/classes/isotopeinterpretation.py index 9436cce..0c5cf30 100644 --- a/pylipd/classes/isotopeinterpretation.py +++ b/pylipd/classes/isotopeinterpretation.py @@ -120,18 +120,12 @@ def to_json(self): if len(self.independentVariables): data["independentVariable"] = [] for value_obj in self.independentVariables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["independentVariable"].append(obj) if self.integrationTime: value_obj = self.integrationTime - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["integrationTime"] = obj for key in self.misc: @@ -140,6 +134,26 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'IsotopeInterpretation': + self = IsotopeInterpretation() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "independentVariable": + for value in pvalue: + obj = IndependentVariable.from_json(value) + self.independentVariables.append(obj) + elif key == "integrationTime": + value = pvalue + obj = IntegrationTime.from_json(value) + self.integrationTime = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/location.py b/pylipd/classes/location.py index 47b39da..2206320 100644 --- a/pylipd/classes/location.py +++ b/pylipd/classes/location.py @@ -387,6 +387,74 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Location': + self = Location() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "continent": + value = pvalue + obj = value + self.continent = obj + elif key == "coordinates": + value = pvalue + obj = value + self.coordinates = obj + elif key == "coordinatesFor": + value = pvalue + obj = value + self.coordinatesFor = obj + elif key == "country": + value = pvalue + obj = value + self.country = obj + elif key == "countryOcean": + value = pvalue + obj = value + self.countryOcean = obj + elif key == "description": + value = pvalue + obj = value + self.description = obj + elif key == "elevation": + value = pvalue + obj = value + self.elevation = obj + elif key == "geometryType": + value = pvalue + obj = value + self.geometryType = obj + elif key == "latitude": + value = pvalue + obj = value + self.latitude = obj + elif key == "locationName": + value = pvalue + obj = value + self.locationName = obj + elif key == "longitude": + value = pvalue + obj = value + self.longitude = obj + elif key == "notes": + value = pvalue + obj = value + self.notes = obj + elif key == "ocean": + value = pvalue + obj = value + self.ocean = obj + elif key == "siteName": + value = pvalue + obj = value + self.siteName = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/model.py b/pylipd/classes/model.py index ca47b36..c0e71a0 100644 --- a/pylipd/classes/model.py +++ b/pylipd/classes/model.py @@ -156,28 +156,19 @@ def to_json(self): if len(self.distributionTables): data["distributionTable"] = [] for value_obj in self.distributionTables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["distributionTable"].append(obj) if len(self.ensembleTables): data["ensembleTable"] = [] for value_obj in self.ensembleTables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["ensembleTable"].append(obj) if len(self.summaryTables): data["summaryTable"] = [] for value_obj in self.summaryTables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["summaryTable"].append(obj) if self.code: @@ -191,6 +182,34 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Model': + self = Model() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "distributionTable": + for value in pvalue: + obj = DataTable.from_json(value) + self.distributionTables.append(obj) + elif key == "ensembleTable": + for value in pvalue: + obj = DataTable.from_json(value) + self.ensembleTables.append(obj) + elif key == "method": + value = pvalue + obj = value + self.code = obj + elif key == "summaryTable": + for value in pvalue: + obj = DataTable.from_json(value) + self.summaryTables.append(obj) + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/paleodata.py b/pylipd/classes/paleodata.py index 676d6d1..f66ac83 100644 --- a/pylipd/classes/paleodata.py +++ b/pylipd/classes/paleodata.py @@ -137,19 +137,13 @@ def to_json(self): if len(self.measurementTables): data["measurementTable"] = [] for value_obj in self.measurementTables: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["measurementTable"].append(obj) if len(self.modeledBy): data["model"] = [] for value_obj in self.modeledBy: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["model"].append(obj) if self.name: @@ -163,6 +157,30 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'PaleoData': + self = PaleoData() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "measurementTable": + for value in pvalue: + obj = DataTable.from_json(value) + self.measurementTables.append(obj) + elif key == "model": + for value in pvalue: + obj = Model.from_json(value) + self.modeledBy.append(obj) + elif key == "paleoDataName": + value = pvalue + obj = value + self.name = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/person.py b/pylipd/classes/person.py index 34765cf..60a0e7c 100644 --- a/pylipd/classes/person.py +++ b/pylipd/classes/person.py @@ -103,6 +103,22 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Person': + self = Person() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "name": + value = pvalue + obj = value + self.name = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/physicalsample.py b/pylipd/classes/physicalsample.py index 872ab1d..fdb5952 100644 --- a/pylipd/classes/physicalsample.py +++ b/pylipd/classes/physicalsample.py @@ -147,6 +147,30 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'PhysicalSample': + self = PhysicalSample() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "hasidentifier": + value = pvalue + obj = value + self.iGSN = obj + elif key == "hasname": + value = pvalue + obj = value + self.name = obj + elif key == "housedat": + value = pvalue + obj = value + self.housedAt = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/publication.py b/pylipd/classes/publication.py index ee6add8..2f3f875 100644 --- a/pylipd/classes/publication.py +++ b/pylipd/classes/publication.py @@ -391,10 +391,7 @@ def to_json(self): if len(self.authors): data["author"] = [] for value_obj in self.authors: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["author"].append(obj) if len(self.dataUrls): @@ -431,10 +428,7 @@ def to_json(self): if self.firstAuthor: value_obj = self.firstAuthor - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["firstauthor"] = obj if self.institution: @@ -493,6 +487,90 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Publication': + self = Publication() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "abstract": + value = pvalue + obj = value + self.abstract = obj + elif key == "author": + for value in pvalue: + obj = Person.from_json(value) + self.authors.append(obj) + elif key == "citation": + value = pvalue + obj = value + self.citation = obj + elif key == "citeKey": + value = pvalue + obj = value + self.citeKey = obj + elif key == "dataUrl": + for value in pvalue: + obj = value + self.dataUrls.append(obj) + elif key == "doi": + value = pvalue + obj = value + self.dOI = obj + elif key == "firstauthor": + value = pvalue + obj = Person.from_json(value) + self.firstAuthor = obj + elif key == "institution": + value = pvalue + obj = value + self.institution = obj + elif key == "issue": + value = pvalue + obj = value + self.issue = obj + elif key == "journal": + value = pvalue + obj = value + self.journal = obj + elif key == "pages": + value = pvalue + obj = value + self.pages = obj + elif key == "publisher": + value = pvalue + obj = value + self.publisher = obj + elif key == "report": + value = pvalue + obj = value + self.report = obj + elif key == "title": + value = pvalue + obj = value + self.title = obj + elif key == "type": + value = pvalue + obj = value + self.publicationType = obj + elif key == "url": + for value in pvalue: + obj = value + self.urls.append(obj) + elif key == "volume": + value = pvalue + obj = value + self.volume = obj + elif key == "year": + value = pvalue + obj = value + self.year = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/resolution.py b/pylipd/classes/resolution.py index e556586..2fcb6fb 100644 --- a/pylipd/classes/resolution.py +++ b/pylipd/classes/resolution.py @@ -183,10 +183,7 @@ def to_json(self): if self.units: value_obj = self.units - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["units"] = obj for key in self.misc: @@ -195,6 +192,38 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Resolution': + self = Resolution() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "hasMaxValue": + value = pvalue + obj = value + self.maxValue = obj + elif key == "hasMeanValue": + value = pvalue + obj = value + self.meanValue = obj + elif key == "hasMedianValue": + value = pvalue + obj = value + self.medianValue = obj + elif key == "hasMinValue": + value = pvalue + obj = value + self.minValue = obj + elif key == "units": + value = pvalue + obj = PaleoUnit.from_synonym(re.sub("^.*?#", "", value)) + self.units = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/uncertainty.py b/pylipd/classes/uncertainty.py index fa3b1c9..8c07c96 100644 --- a/pylipd/classes/uncertainty.py +++ b/pylipd/classes/uncertainty.py @@ -103,6 +103,22 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Uncertainty': + self = Uncertainty() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "uncertaintyType": + value = pvalue + obj = value + self.uncertaintyType = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/classes/variable.py b/pylipd/classes/variable.py index 46038f3..f5bbc32 100644 --- a/pylipd/classes/variable.py +++ b/pylipd/classes/variable.py @@ -588,27 +588,18 @@ def to_json(self): if len(self.calibratedVias): data["calibration"] = [] for value_obj in self.calibratedVias: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["calibration"].append(obj) if len(self.interpretations): data["interpretation"] = [] for value_obj in self.interpretations: - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["interpretation"].append(obj) if self.archiveType: value_obj = self.archiveType - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["archiveType"] = obj if self.columnNumber: @@ -631,16 +622,6 @@ def to_json(self): obj = value_obj data["description"] = obj - if self.foundInDataset: - value_obj = self.foundInDataset - obj = value_obj - data["foundInDataset"] = obj - - if self.foundInTable: - value_obj = self.foundInTable - obj = value_obj - data["foundInTable"] = obj - if self.instrument: value_obj = self.instrument obj = value_obj @@ -683,10 +664,7 @@ def to_json(self): if self.partOfCompilation: value_obj = self.partOfCompilation - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["inCompilationBeta"] = obj if self.physicalSample: @@ -701,50 +679,32 @@ def to_json(self): if self.proxy: value_obj = self.proxy - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["proxy"] = obj if self.proxyGeneral: value_obj = self.proxyGeneral - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["proxyGeneral"] = obj if self.resolution: value_obj = self.resolution - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["resolution"] = obj if self.standardVariable: value_obj = self.standardVariable - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["hasStandardVariable"] = obj if self.uncertainty: value_obj = self.uncertainty - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["uncertainty"] = obj if self.units: value_obj = self.units - if hasattr(value_obj, "to_json"): - obj = value_obj.to_json() - else: - obj = value_obj + obj = value_obj.to_json() data["units"] = obj if self.values: @@ -768,6 +728,134 @@ def to_json(self): return data + @staticmethod + def from_json(data) -> 'Variable': + self = Variable() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue + elif key == "TSid": + value = pvalue + obj = value + self.variableId = obj + elif key == "archiveType": + value = pvalue + obj = ArchiveType.from_synonym(re.sub("^.*?#", "", value)) + self.archiveType = obj + elif key == "calibration": + for value in pvalue: + obj = Calibration.from_json(value) + self.calibratedVias.append(obj) + elif key == "compilation_nest": + value = pvalue + obj = value + self.compilationNest = obj + elif key == "description": + value = pvalue + obj = value + self.description = obj + elif key == "foundInDataset": + value = pvalue + obj = value + self.foundInDataset = obj + elif key == "foundInTable": + value = pvalue + obj = value + self.foundInTable = obj + elif key == "hasMaxValue": + value = pvalue + obj = value + self.maxValue = obj + elif key == "hasMeanValue": + value = pvalue + obj = value + self.meanValue = obj + elif key == "hasMedianValue": + value = pvalue + obj = value + self.medianValue = obj + elif key == "hasMinValue": + value = pvalue + obj = value + self.minValue = obj + elif key == "hasStandardVariable": + value = pvalue + obj = PaleoVariable.from_synonym(re.sub("^.*?#", "", value)) + self.standardVariable = obj + elif key == "hasValues": + value = pvalue + obj = value + self.values = obj + elif key == "inCompilationBeta": + value = pvalue + obj = Compilation.from_json(value) + self.partOfCompilation = obj + elif key == "interpretation": + for value in pvalue: + obj = Interpretation.from_json(value) + self.interpretations.append(obj) + elif key == "isComposite": + value = pvalue + obj = value + self.composite = obj + elif key == "isPrimary": + value = pvalue + obj = value + self.primary = obj + elif key == "measurementInstrument": + value = pvalue + obj = value + self.instrument = obj + elif key == "missingValue": + value = pvalue + obj = value + self.missingValue = obj + elif key == "notes": + value = pvalue + obj = value + self.notes = obj + elif key == "number": + value = pvalue + obj = value + self.columnNumber = obj + elif key == "physicalSample": + value = pvalue + obj = value + self.physicalSample = obj + elif key == "proxy": + value = pvalue + obj = PaleoProxy.from_synonym(re.sub("^.*?#", "", value)) + self.proxy = obj + elif key == "proxyGeneral": + value = pvalue + obj = PaleoProxyGeneral.from_synonym(re.sub("^.*?#", "", value)) + self.proxyGeneral = obj + elif key == "resolution": + value = pvalue + obj = Resolution.from_json(value) + self.resolution = obj + elif key == "uncertainty": + value = pvalue + obj = Uncertainty.from_json(value) + self.uncertainty = obj + elif key == "units": + value = pvalue + obj = PaleoUnit.from_synonym(re.sub("^.*?#", "", value)) + self.units = obj + elif key == "variableName": + value = pvalue + obj = value + self.name = obj + elif key == "variableType": + value = pvalue + obj = value + self.variableType = obj + else: + self.set_non_standard_property(key, pvalue) + + return self + def set_non_standard_property(self, key, value): if key not in self.misc: self.misc[key] = value diff --git a/pylipd/globals/blacklist.py b/pylipd/globals/blacklist.py index 5a91ddd..b7e6dc9 100644 --- a/pylipd/globals/blacklist.py +++ b/pylipd/globals/blacklist.py @@ -12,8 +12,8 @@ } REVERSE_BLACKLIST = { - 'inferredFrom' : 1 + 'inferredFrom' : 1, + 'foundInTable' : 1, + 'foundInDataset' : 1 #'takenAtDepth' : 1 - #'foundInTable' : 1, - #'foundInDataset' : 1, } \ No newline at end of file diff --git a/pylipd/utils/create_classes.py b/pylipd/utils/create_classes.py index 6f0dfed..972ca9d 100644 --- a/pylipd/utils/create_classes.py +++ b/pylipd/utils/create_classes.py @@ -1,5 +1,7 @@ from pylipd.globals.schema import SCHEMA, SYNONYMS from pylipd.globals.urls import ONTONS, NSURL +from pylipd.globals.blacklist import REVERSE_BLACKLIST + import re import os @@ -7,7 +9,7 @@ CLASSDIR = os.path.realpath(f"{SCRIPTDIR}/../classes") if not os.path.exists(CLASSDIR): os.makedirs(CLASSDIR) - +TEMPLATEDIR = f"{CLASSDIR}/templates" def get_fromdata_item(ptype, range, is_enum): fromdataitem = f""" @@ -58,8 +60,34 @@ def get_todata_item(type, range): data = value_obj.to_data(data)""" return todataitem +def get_tojson_item(type): + todataitem = "" + if type == "literal": + todataitem += f""" + obj = value_obj""" + elif type == "object": + todataitem += f""" + obj = value_obj.to_json()""" + return todataitem + +def get_fromjson_item(ptype, range, is_enum): + fromjsonitem = "" + if ptype == "object" and range is not None: + if is_enum: + fromjsonitem += f""" + obj = {range}.from_synonym(re.sub("^.*?#", "", value))""" + else: + fromjsonitem += f""" + obj = {range}.from_json(value)""" + elif range: + fromjsonitem += f""" + obj = value""" + else: + fromjsonitem += f""" + obj = value""" + return fromjsonitem -def get_python_snippet_for_multi_value_property(propid, pname, ptype, ont_range, python_range, getter, setter, adder, is_enum): +def get_python_snippet_for_multi_value_property(pid, propid, pname, ptype, ont_range, python_range, getter, setter, adder, is_enum): # Create the python snippet for initialzing property variables initvar = f"self.{pname}: list[{python_range}] = []" @@ -78,6 +106,23 @@ def get_python_snippet_for_multi_value_property(propid, pname, ptype, ont_range, elif key == "{propid}":{fromdataitem} self.{pname}.append(obj)""" + # Create the python function snippet for this property to convert the class to json (tojson) + if pid not in REVERSE_BLACKLIST: + tojsonitem = get_tojson_item(ptype) + tojson = f""" + + if len(self.{pname}): + data["{pid}"] = [] + for value_obj in self.{pname}:{tojsonitem} + data["{pid}"].append(obj)""" + + # Create the python function snippet for this property to convert from json dictionary to a class (fromjson) + fromjsonitem = get_fromjson_item(ptype, python_range, is_enum) + fromjson = f""" + elif key == "{pid}": + for value in pvalue:{fromjsonitem} + self.{pname}.append(obj)""" + # Create the python snippet for getter/setter/adders fns = f""" def {getter}(self) -> list[{python_range}]: @@ -89,10 +134,10 @@ def {setter}(self, {pname}:list[{python_range}]): def {adder}(self, {pname}:{python_range}): self.{pname}.append({pname}) """ - return (initvar, todata, fromdata, fns) + return (initvar, todata, fromdata, tojson, fromjson, fns) -def get_python_snippet_for_property(propid, pname, ptype, ont_range, python_range, getter, setter, is_enum): +def get_python_snippet_for_property(pid, propid, pname, ptype, ont_range, python_range, getter, setter, is_enum): # Create the python snippet for initialzing property variables initvar = f"self.{pname}: {python_range} = None" @@ -110,6 +155,23 @@ def get_python_snippet_for_property(propid, pname, ptype, ont_range, python_rang elif key == "{propid}":{fromdataitem} self.{pname} = obj""" + # Create the python function snippet for this property to convert the class to json (tojson) + tojson = "" + if pid not in REVERSE_BLACKLIST: + tojsonitem = get_tojson_item(ptype) + tojson += f""" + + if self.{pname}: + value_obj = self.{pname}{tojsonitem} + data["{pid}"] = obj""" + + # Create the python function snippet for this property to convert from json dictionary to a class (fromjson) + fromjsonitem = get_fromjson_item(ptype, python_range, is_enum) + fromjson = f""" + elif key == "{pid}": + value = pvalue{fromjsonitem} + self.{pname} = obj""" + # Create the python snippet for getter/setter/adders fns = f""" def {getter}(self) -> {python_range}: @@ -118,7 +180,7 @@ def {getter}(self) -> {python_range}: def {setter}(self, {pname}:{python_range}): self.{pname} = {pname} """ - return (initvar, todata, fromdata, fns) + return (initvar, todata, fromdata, tojson, fromjson, fns) def generate_enum_classes(): @@ -164,7 +226,11 @@ def to_data(self, data={{}}): ] }} return data - + + def to_json(self): + data = self.label + return data + @classmethod def from_synonym(cls, synonym): if synonym.lower() in {clsid}.synonyms: @@ -182,11 +248,41 @@ def from_synonym(cls, synonym): done[synid] = True outf.write(f""" {synid} = {clsid}("{synobj['id']}", "{synobj['label']}")""") - -def generate_class_file(clsid, import_snippets, initvar_snippets, todata_snippets, fromdata_snippets, fn_snippets): +def fetch_extra_template_functions(clsid): + imports = [] + fns = [] + + curfn = "" + fn_ongoing = False + tplfilename = f"{TEMPLATEDIR}/{clsid.lower()}.py" + if os.path.exists(tplfilename): + with open(tplfilename, "r") as inf: + for line in inf.readlines(): + #line = line.strip() + if re.match("^import ", line, re.I) or re.match("^from .+ import ", line, re.I): + imports.append(line) + if re.match("^#.*START TEMPLATE FUNCTION", line, re.I): + fn_ongoing = True + elif re.match("^#.*END TEMPLATE FUNCTION", line, re.I): + fn_ongoing = False + if curfn: + fns.append("\n" + curfn) + curfn = "" + elif fn_ongoing: + curfn += " " + line + return (imports, fns) + + +def generate_class_file(clsid, import_snippets, initvar_snippets, + todata_snippets, fromdata_snippets, + tojson_snippets, fromjson_snippets, + fn_snippets): + + (ximports, xfn_snippets) = fetch_extra_template_functions(clsid) + fn_snippets = fn_snippets + xfn_snippets + filename = f"{CLASSDIR}/{clsid.lower()}.py" - with open(filename, "w") as outf: # Write the header outf.write(""" @@ -197,14 +293,18 @@ def generate_class_file(clsid, import_snippets, initvar_snippets, todata_snippet """) # Write the imports outf.write(f"import re\n") + for im in ximports: + outf.write(im) outf.write("from pylipd.utils import uniqid\n") for im in import_snippets: outf.write(f"from pylipd.classes.{im.lower()} import {im}\n") outf.write("\n") + # Write the class header outf.write(f"class {clsid}:\n") + # Write the init function outf.write(f""" def __init__(self):""") @@ -219,6 +319,7 @@ def __init__(self):""") self.id = self.ns + "/" + uniqid("{clsid}.")""") outf.write("\n") + # Write the from_data function outf.write(f""" @staticmethod @@ -235,7 +336,6 @@ def from_data(id, data) -> '{clsid}': for snippet in fromdata_snippets: outf.write(f""" {snippet}""") - outf.write(f""" else: for val in value: @@ -248,7 +348,8 @@ def from_data(id, data) -> '{clsid}': return self""") outf.write("\n") - + + # Write the to_data function outf.write(f""" def to_data(self, data={{}}): @@ -259,10 +360,8 @@ def to_data(self, data={{}}): "@type": "uri" }} ]""") - for snippet in todata_snippets: outf.write(f"""{snippet}""") - outf.write(f""" for key in self.misc: value = self.misc[key] @@ -288,8 +387,45 @@ def to_data(self, data={{}}): }}) return data""") + outf.write("\n") + + # Write the to_json function + outf.write(f""" + def to_json(self): + data = {{ + "@id": self.id + }}""") + for snippet in tojson_snippets: + outf.write(f"""{snippet}""") outf.write("\n") + outf.write(f""" + for key in self.misc: + value = self.misc[key] + data[key] = value + + return data""") + outf.write("\n") + + + # Write the from_json function + outf.write(f""" + @staticmethod + def from_json(data) -> '{clsid}': + self = {clsid}() + for key in data: + pvalue = data[key] + if key == "@id": + self.id = pvalue""") + for snippet in fromjson_snippets: + outf.write(f"""{snippet}""") + outf + outf.write(f""" + else: + self.set_non_standard_property(key, pvalue) + + return self""") + outf.write("\n") # Write the functions to handle non standard properties outf.write(f""" @@ -323,6 +459,8 @@ def generate_lipd_classes(): initvar_snippets = set() fromdata_snippets = set() todata_snippets = set() + tojson_snippets = set() + fromjson_snippets = set() fn_snippets = set() # Check all properties @@ -395,11 +533,11 @@ def generate_lipd_classes(): # Get python snippets for initialization function, todata function, fromdata from, and the setter/getter functions if multiple: - (initvar, todata, fromdata, fns) = get_python_snippet_for_multi_value_property(propid, mpname, ptype, + (initvar, todata, fromdata, tojson, fromjson, fns) = get_python_snippet_for_multi_value_property(pid, propid, mpname, ptype, ont_range, python_range, getter, setter, adder, is_enum) else: - (initvar, todata, fromdata, fns) = get_python_snippet_for_property(propid, mpname, ptype, + (initvar, todata, fromdata, tojson, fromjson, fns) = get_python_snippet_for_property(pid, propid, mpname, ptype, ont_range, python_range, getter, setter, is_enum) @@ -410,12 +548,15 @@ def generate_lipd_classes(): initvar_snippets.add(initvar) todata_snippets.add(todata) fromdata_snippets.add(fromdata) + tojson_snippets.add(tojson) + fromjson_snippets.add(fromjson) fn_snippets.add(fns) # Write outputs generate_class_file(clsid, sorted(import_snippets), sorted(initvar_snippets), - sorted(todata_snippets), sorted(fromdata_snippets), sorted(fn_snippets)) - + sorted(todata_snippets), sorted(fromdata_snippets), + sorted(tojson_snippets), sorted(fromjson_snippets), + sorted(fn_snippets)) if __name__ == "__main__": generate_enum_classes() diff --git a/pylipd/utils/rdf_to_lipd.py b/pylipd/utils/rdf_to_lipd.py index b18b68d..f822bbe 100644 --- a/pylipd/utils/rdf_to_lipd.py +++ b/pylipd/utils/rdf_to_lipd.py @@ -12,14 +12,14 @@ import tempfile import zipfile -from rdflib.graph import ConjunctiveGraph, URIRef +from rdflib.graph import URIRef from ..globals.urls import NSURL from ..globals.blacklist import REVERSE_BLACKLIST from ..globals.schema import SCHEMA from ..globals.synonyms import RSYNONYMS -from .utils import ucfirst, lcfirst, unCamelCase, unzip_string +from .utils import ucfirst, lcfirst, unzip_string class RDFToLiPD: """