1- import json
2- from collections import OrderedDict
3- from datetime import datetime
1+ import datetime
42
53from omi import structure
64from omi .dialects .base .compiler import Compiler
75from omi .oem_structures import oem_v15
86
97
8+ def compile_date_or_none (x , format = None ):
9+ if isinstance (x , (datetime .datetime , datetime .date )):
10+ if format :
11+ return x .strftime (format )
12+ else :
13+ return x .isoformat ()
14+ else :
15+ return x
16+
17+
1018class JSONCompiler (Compiler ):
1119 __METADATA_VERSION = "OEP-1.4.0"
1220
13- def _compile_date (self , date : datetime , format ):
14- if date :
15- return date .strftime (format )
16- else :
17- return None
18-
1921 def _construct_dict (self , * args , omit_none = True , ** kwargs ):
2022 """
2123 Accepts a list of arguments of shape (name: str, field: Compileable) and returns a dictionary that maps
@@ -61,7 +63,7 @@ def visit_contribution(self, contribution: structure.Contribution, *args, **kwar
6163 ("email" , contribution .contributor .email ),
6264 ("object" , contribution .object ),
6365 ("comment" , contribution .comment ),
64- ("date" , self . _compile_date (contribution .date , "%Y-%m-%d" )),
66+ ("date" , compile_date_or_none (contribution .date , "%Y-%m-%d" )),
6567 )
6668
6769 def visit_language (self , language : structure .Language , * args , ** kwargs ):
@@ -90,11 +92,14 @@ def visit_temporal(self, temporal: structure.Temporal, *args, **kwargs):
9092 start = None
9193 end = None
9294 if temporal .ts_start is not None :
93- start = self . _compile_date (temporal .ts_start , "%Y-%m-%dT%H:%M%z" )[: - 2 ]
95+ start = compile_date_or_none (temporal .ts_start )
9496 if temporal .ts_end is not None :
95- end = self . _compile_date (temporal .ts_end , "%Y-%m-%dT%H:%M%z" )[: - 2 ]
97+ end = compile_date_or_none (temporal .ts_end )
9698 return self ._construct_dict (
97- ("referenceDate" , self ._compile_date (temporal .reference_date , "%Y-%m-%d" )),
99+ (
100+ "referenceDate" ,
101+ compile_date_or_none (temporal .reference_date , "%Y-%m-%d" ),
102+ ),
98103 timeseries = self ._construct_dict (
99104 ("start" , start ),
100105 ("end" , end ),
@@ -202,7 +207,9 @@ def visit_meta_comment(self, comment: structure.MetaComment, *args, **kwargs):
202207 def visit_metadata (self , metadata : structure .OEPMetadata , * args , ** kwargs ):
203208 publication_date = None
204209 if metadata .publication_date is not None :
205- publication_date = self ._compile_date (metadata .publication_date , "%Y-%m-%d" )
210+ publication_date = compile_date_or_none (
211+ metadata .publication_date , "%Y-%m-%d"
212+ )
206213 return self ._construct_dict (
207214 ("name" , metadata .name ),
208215 ("title" , metadata .title ),
@@ -286,9 +293,9 @@ def visit_timeseries(self, timeseries: oem_v15.Timeseries, *args, **kwargs):
286293 start = None
287294 end = None
288295 if timeseries .ts_start is not None :
289- start = self . _compile_date (timeseries .ts_start , "%Y-%m-%dT%H:%M%z" )[: - 2 ]
296+ start = compile_date_or_none (timeseries .ts_start )
290297 if timeseries .ts_end is not None :
291- end = self . _compile_date (timeseries .ts_end , "%Y-%m-%dT%H:%M%z" )[: - 2 ]
298+ end = compile_date_or_none (timeseries .ts_end )
292299 return self ._construct_dict (
293300 ("start" , start ),
294301 ("end" , end ),
@@ -299,10 +306,13 @@ def visit_timeseries(self, timeseries: oem_v15.Timeseries, *args, **kwargs):
299306
300307 def visit_temporal (self , temporal : oem_v15 .Temporal , * args , ** kwargs ):
301308 return self ._construct_dict (
302- ("referenceDate" , self ._compile_date (temporal .reference_date , "%Y-%m-%d" )),
309+ (
310+ "referenceDate" ,
311+ compile_date_or_none (temporal .reference_date , "%Y-%m-%d" ),
312+ ),
303313 ("timeseries" , temporal .timeseries_collection ),
304314 )
305-
315+
306316 def visit_license (self , lic : oem_v15 .License , * args , ** kwargs ):
307317 return self ._construct_dict (
308318 ("name" , lic .name ),
@@ -347,7 +357,9 @@ def visit_meta_comment(self, comment: oem_v15.MetaComment, *args, **kwargs):
347357 def visit_metadata (self , metadata : oem_v15 .OEPMetadata , * args , ** kwargs ):
348358 publication_date = None
349359 if metadata .publication_date is not None :
350- publication_date = self ._compile_date (metadata .publication_date , "%Y-%m-%d" )
360+ publication_date = compile_date_or_none (
361+ metadata .publication_date , "%Y-%m-%d"
362+ )
351363 return self ._construct_dict (
352364 ("name" , metadata .name ),
353365 ("title" , metadata .title ),
0 commit comments