1- from omi .base import get_metadata_specification
1+ """Conversion functions for metadata version "OEP-1.6.0" to "OEMetadata-2.0"."""
2+
23from copy import deepcopy
34
4- # TODO: use utils.find_spatial_resolution_value_and_unit
5+ from omi .base import get_metadata_specification
6+
7+ # use utils.find_spatial_resolution_value_and_unit
58from omi .conversions .utils import find_temporal_resolution_value_and_unit
69
710
@@ -22,9 +25,9 @@ def convert_oep_160_to_20(metadata: dict) -> dict:
2225 metadata_v2 = deepcopy (get_metadata_specification ("OEMetadata-2.0" ).template )
2326
2427 # Update to v2 context URL
25- metadata_v2 ["@context" ] = (
26- "https://raw.githubusercontent.com/OpenEnergyPlatform/oemetadata/production/oemetadata/v2/v20/ context.json "
27- )
28+ metadata_v2 [
29+ "@ context"
30+ ] = "https://raw.githubusercontent.com/OpenEnergyPlatform/oemetadata/production/oemetadata/v2/v20/context.json"
2831 metadata_v2 ["name" ] = metadata_v2 ["title" ] = metadata_v2 ["description" ] = ""
2932
3033 metadata_v2 ["@id" ] = None
@@ -48,7 +51,7 @@ def ___v2_ensure_resource_entry(metadata_v2: dict, index: int) -> dict:
4851 return metadata_v2 ["resources" ][index ]
4952
5053
51- def ___v2_populate_resource_v2 (resource_v2 : dict , metadata : dict , resource : dict ) -> None :
54+ def ___v2_populate_resource_v2 (resource_v2 : dict , metadata : dict , resource : dict ) -> None : # noqa: C901
5255 """Populate resource_v2 fields based on metadata and resource from v1.6."""
5356 # Bulk update keys without
5457 resource_v2 .update (
@@ -100,7 +103,7 @@ def ___v2_populate_resource_v2(resource_v2: dict, metadata: dict, resource: dict
100103 if resolution :
101104 parts = resolution .split (" " , 1 )
102105
103- if len (parts ) == 2 :
106+ if len (parts ) == 2 : # noqa: PLR2004
104107 resource_v2 ["spatial" ]["extent" ]["resolutionValue" ] = parts [0 ]
105108 resource_v2 ["spatial" ]["extent" ]["resolutionUnit" ] = parts [1 ]
106109 elif len (parts ) == 1 and parts [0 ]:
@@ -141,7 +144,6 @@ def ___v2_populate_subjects(resource_v2: list, subjects: list) -> None:
141144
142145def ___v2_populate_temporal (resource_v2 : list , temporal : dict ) -> None :
143146 """Populate temporal in resource_v2 from temporal in v1.6."""
144-
145147 if isinstance (temporal .get ("referenceDate" ), str ):
146148 resource_v2 ["temporal" ]["referenceDate" ] = temporal ["referenceDate" ]
147149
@@ -171,16 +173,15 @@ def ___v2_populate_temporal(resource_v2: list, temporal: dict) -> None:
171173 )
172174
173175
174- # TODO: sort out the code related to spatial information from above
176+ # sort out the code related to spatial information from above
177+
175178# to add it to a new function, see below:
176179# def ___v2_populate_spatial(resource_v2: list, subjects: list) -> None:
177180# """Populate licenses in source_v2 from licenses in v1.6."""
178181# if not subjects:
179- # resource_v2["subject"][0]["@id"] = None
180182
181183# for i_subject, subject_entry in enumerate(subjects):
182184# if i_subject >= len(resource_v2["subject"]):
183- # resource_v2["subject"].append(deepcopy(resource_v2["subject"][0]))
184185
185186
186187def ___v2_populate_sources (resource_v2 : dict , sources : list ) -> None :
@@ -232,7 +233,7 @@ def ___v2_populate_contributors(resource_v2: dict, contributors: list) -> None:
232233 last_contributor = resource_v2 ["contributors" ][len (resource_v2 ["contributors" ]) - 1 ]
233234 if (
234235 isinstance (last_contributor .get ("title" ), str )
235- and not last_contributor .get ("title" , "" ) in "Open Energy Platform oemetadata conversion to v2"
236+ and last_contributor .get ("title" , "" ) not in "Open Energy Platform oemetadata conversion to v2"
236237 ):
237238 resource_v2 ["contributors" ].append (
238239 {
@@ -243,7 +244,8 @@ def ___v2_populate_contributors(resource_v2: dict, contributors: list) -> None:
243244 "date" : "2021-09-01" ,
244245 "object" : "conversion of all metadata to oemetadata version 2.0.4" ,
245246 "comment" : "The conversion was done by the OpenEnergyFamily team using the OMI software."
246- "We did our best to mitigate data loss. Most unexpected or incorrect metadata property entries will be lost." ,
247+ "We did our best to mitigate data loss. Most unexpected or incorrect metadata property"
248+ "entries will be lost." ,
247249 },
248250 )
249251
@@ -280,21 +282,21 @@ def ___v2_populate_schema_fields(resource_v2: dict, resource: dict) -> None:
280282 for i in schema_field_v2 .get ("isAbout" , []) or []:
281283 i .update (rename_path_to_id (i ))
282284
283- if not schema_field_v2 .get ("isAbout" ):
285+ if not schema_field_v2 .get ("isAbout" ): # noqa: SIM114
284286 schema_field_v2 ["isAbout" ] = []
285287 elif "isAbout" not in field .keys ():
286288 schema_field_v2 ["isAbout" ] = []
287289
288290 for i in schema_field_v2 .get ("valueReference" , []) or []:
289291 i .update (rename_path_to_id (i ))
290292
291- if not schema_field_v2 .get ("valueReference" ):
293+ if not schema_field_v2 .get ("valueReference" ): # noqa: SIM114
292294 schema_field_v2 ["valueReference" ] = []
293295 elif "valueReference" not in field .keys ():
294296 schema_field_v2 ["valueReference" ] = []
295297
296298
297- def rename_path_to_id (annotation_object : dict ):
299+ def rename_path_to_id (annotation_object : dict ) -> dict :
298300 """Rename 'path' to '@id' in obj."""
299301 if "path" in annotation_object :
300302 annotation_object ["@id" ] = annotation_object .pop ("path" )
@@ -314,7 +316,6 @@ def ___v2_populate_schema_primary_keys(resource_v2: dict, resource: dict) -> Non
314316
315317def ___v2_populate_schema_foreign_keys (resource_v2 : dict , resource : dict ) -> None :
316318 """Populate schema fields in resource_v2 from resource in v1.6."""
317-
318319 for i_fk , fk in enumerate (resource .get ("schema" , {}).get ("foreignKeys" , [])):
319320 if i_fk >= len (resource_v2 ["schema" ]["foreignKeys" ]):
320321 resource_v2 ["schema" ]["foreignKeys" ].append (deepcopy (resource_v2 ["schema" ]["foreignKeys" ][0 ]))
0 commit comments