@@ -35,12 +35,20 @@ def load(self):
3535 def _to_sql_str (self , val ):
3636 if val is None :
3737 return "''"
38- return "'" + str (json .dumps (val )).replace ("'" , "''" ) + "'"
38+
39+ # If it's already a JSON string, don't re-encode
40+ if isinstance (val , str ):
41+ try :
42+ json .loads (val )
43+ # It's already JSON, just escape for SQL
44+ return "'" + val .replace ("'" , "''" ) + "'"
45+ except json .JSONDecodeError :
46+ pass
47+ return "'" + json .dumps (val ).replace ("'" , "''" ) + "'"
3948
4049 def _from_sql_str (self , string ):
41- if string .endswith (']' ) and string .startswith ('[' ) and string .count ("'" ) >= 2 :
42- # this is an old style parameters so transform it to json readable form from __repr__
43- string = string .replace ("'" ,'"' )
50+ if string .endswith (']' ) and string .startswith ('[' ) and string .count ("'" ) >= 2 :
51+ string = string .replace ("'" , '"' )
4452 try :
4553 return json .loads (string )
4654 except json .JSONDecodeError :
@@ -67,13 +75,18 @@ def load_parameters(self, context):
6775 # get any context and then override with specific context
6876 for line in sorted (data , key = lambda x : x .get ('date' , '' )): # latest should override
6977 val = self ._from_sql_str (line .get ('value' ))
70- any_parameters [line .get ('parameter' )] = val
78+ key = self ._from_sql_str (line .get ('parameter' ))
79+ any_parameters [key ] = val
7180 if line .get ('context' ) == str (context ):
72- context_parameters [line . get ( 'parameter' ) ] = val
81+ context_parameters [key ] = val
7382 return any_parameters | context_parameters
7483
7584 def dump_parameters (self , params , context = None ):
7685 data = self .load ()
86+ data = [ line # avoid buggy records
87+ for line in data
88+ if '\\ ' not in str (line )
89+ ]
7790 logger .trace (f'loaded parameters { data } ' )
7891 params = dict (params )
7992 date = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
@@ -96,4 +109,10 @@ def dump_parameters(self, params, context=None):
96109 })
97110
98111 logger .trace (f'dumping parameters { data } ' )
99- self .dump (data )
112+ self .dump (data )
113+
114+ if __name__ == '__main__' :
115+ from cid .helpers import Athena
116+ import boto3
117+ pc = ParametersController (Athena (boto3 .session .Session ()))
118+ pc .load_parameters ('aaa' )
0 commit comments