@@ -15,6 +15,17 @@ def valid_chronologies(cur, yml_dict, csv_file):
1515 ValueError: If there is an issue with the extracted parameters.
1616 AssertionError: If the date format in the CSV file is incorrect.
1717 """
18+ def collapse_into_chronology (data , chron_k = 'chronologies' ):
19+ chron = data .get (chron_k , {})
20+ if len (chron ) == 1 :
21+ key = next (iter (chron ))
22+ inner = chron [key ]
23+ for k , v in data .items ():
24+ if k != chron_k :
25+ inner [k ] = v
26+ return {chron_k : {key : inner }}
27+ return data
28+
1829 response = ChronResponse ()
1930
2031 params = ['ageboundolder' , 'ageboundyounger' , 'agemodel' , #'chronologyname', 'isdefault', <- don't need anymore because we use a dictionary that retrieves this from yml
@@ -38,28 +49,31 @@ def valid_chronologies(cur, yml_dict, csv_file):
3849 new_date = None
3950 else :
4051 new_date = None
41- if 'age' in params :
42- params .remove ('age' )
43- inputs = nh .pull_params (params , yml_dict , csv_file , "ndb.chronologies" )
44- inputs ['age' ] = new_date
45- response .valid .append (True )
52+ if 'age' in params :
53+ params .remove ('age' )
54+ inputs = nh .pull_params (params , yml_dict , csv_file , "ndb.chronologies" )
55+ inputs ['age' ] = new_date
56+ response .valid .append (True )
57+ else :
58+ inputs = {}
4659 except Exception as inner_e :
60+ inputs = {}
4761 response .validAll = False
4862 response .message .append (f"Chronology parameters cannot be properly extracted. { e } \n "
4963 f"{ str (inner_e )} " )
5064 return response
51-
65+
66+ inputs = collapse_into_chronology (inputs )
5267 if len (inputs ['chronologies' ]) > 1 :
5368 response .message .append ("✔ File with multiple chronologies" )
5469 response .message .append (f"{ list (inputs ['chronologies' ].keys ())} " )
55-
5670 for chron in inputs ['chronologies' ]:
5771 ch = inputs ['chronologies' ][chron ]
58- if ch .get ("agetype" , inputs [ 'agetype' ] ) is not None :
59- ch .get ("agetype" , inputs [ 'agetype' ] ).replace ("cal yr BP" , 'Calendar years BP' )
72+ if ch .get ("agetype" , inputs . get ( 'agetype' ) ) is not None :
73+ ch .get ("agetype" , inputs . get ( 'agetype' ) ).replace ("cal yr BP" , 'Calendar years BP' )
6074 agetype_query = """SELECT agetypeid FROM ndb.agetypes
6175 WHERE LOWER(agetype) = %(agetype)s"""
62- cur .execute (agetype_query , {'agetype' : ch .get ("agetype" , inputs [ 'agetype' ] ).lower ()})
76+ cur .execute (agetype_query , {'agetype' : ch .get ("agetype" , inputs . get ( 'agetype' ) ).lower ()})
6377 id = cur .fetchone ()
6478 if id :
6579 ch ['agetypeid' ] = id [0 ]
@@ -94,15 +108,23 @@ def valid_chronologies(cur, yml_dict, csv_file):
94108 elif isinstance (ch .get ('age' , inputs .get ('age' )), list ):
95109 ch ['age' ] = [1950 - value .year if isinstance (value , datetime ) else 1950 - value
96110 for value in ch .get ('age' , inputs .get ('age' ))]
97- c ["ageboundolder" ]= int (max ([num for num in ch .get ('ageboundolder' , ch .get ('age' )) if num is not None ]))
98- c ["ageboundyounger" ]= int (min ([num for num in ch .get ('ageboundyounger' , ch .get ('age' )) if num is not None ]))
111+ for param in ['ageboundolder' , 'ageboundyounger' ]:
112+ if param in ch :
113+ if isinstance (ch [param ], list ):
114+ c [param ]= int (min ([num for num in ch .get (param , ch .get ('age' )) if num is not None ])) if param == 'ageboundyounger' else int (max ([num for num in ch .get (param , ch .get ('age' )) if num is not None ]))
115+ else :
116+ c [param ]= ch [param ]
117+ else :
118+ if isinstance (ch ['age' ], list ):
119+ c [param ]= int (min ([num for num in ch .get ('age' ) if num is not None ])) if param == 'ageboundyounger' else int (max ([num for num in ch .get ('age' ) if num is not None ]))
120+ else :
121+ c [param ]= None
99122 Chronology (** c )
100123 response .valid .append (True )
101124 response .message .append ("✔ Chronology can be created" )
102125 except Exception as e :
103126 response .valid .append (False )
104127 response .message .append (f"✗ Chronology cannot be created: { e } " )
105-
106128 response .validAll = all (response .valid )
107129 response .message = list (set (response .message ))
108130 return response
0 commit comments