1
- from typing import Any , Dict , List
1
+ from typing import IO , Any , Dict , List
2
2
from dataclasses import field , dataclass
3
3
4
4
import simplejson as json
@@ -26,7 +26,7 @@ def walk(self, obj: Dict[str, Any] | List[Any]) -> Dict[str, Any] | List[Any]:
26
26
if isinstance (obj , dict ):
27
27
return self .walk_dict (obj )
28
28
elif isinstance (obj , list ):
29
- return [ self .walk ( item ) for item in obj ]
29
+ return list ( map ( self .walk , obj ))
30
30
else :
31
31
return obj
32
32
@@ -36,23 +36,22 @@ def convert(self, obj: Dict[str, Any] | List[Any]):
36
36
return new_obj , self .has_done_something
37
37
38
38
39
+ def _migrate (input_oi : IO [str ], output_oi : IO [str ], indent : int | None = None ):
40
+ obj = json .load (input_oi )
41
+ new_obj , has_done_something = JSONWalker ().convert (obj )
42
+
43
+ if has_done_something :
44
+ json .dump (new_obj , output_oi , indent = indent )
45
+
46
+
39
47
def migrate (
40
48
filename : str ,
41
49
indent : int | None = None ,
42
50
overwrite : bool = False ,
43
51
):
44
-
45
- with open (filename , "r" ) as io :
46
- obj = json .load (io )
47
- walker = JSONWalker ()
48
- new_obj , has_done_something = walker .convert (obj )
49
-
50
- if has_done_something :
51
- new_filename = (
52
- filename if overwrite else filename .replace (".json" , "-analog.json" )
53
- )
54
- with open (new_filename , "w" ) as io :
55
- json .dump (new_obj , io , indent = indent )
52
+ new_filename = filename if overwrite else filename .replace (".json" , "-analog.json" )
53
+ with open (filename , "r" ) as in_io , open (new_filename , "w" ) as out_io :
54
+ _migrate (in_io , out_io , indent )
56
55
57
56
58
57
def _entry ():
0 commit comments