11import json
22import os
3- import platform # Added
3+ import platform
44import shutil
55from importlib import resources
66from pathlib import Path
7- from typing import Any , Dict , List , Optional , Set , Union
7+ from typing import Any , Dict , List , Set , Union
88
9- from .models import PluginInfo , PluginParameter # Added
10-
11- # pkg_resources import removed, will rely on importlib.resources primarily.
12- # Fallback block for pkg_resources in copy_default_ignores will try to use it
13- # and handle NameError if it's not available.
149from .utils import ensure_folder
1510
1611APP_NAME : str = "com.twardoch.pedalboard-pluginary"
@@ -41,8 +36,8 @@ def get_cache_path(cache_name: str) -> Path:
4136 return app_data_dir / f"{ cache_name } .json"
4237
4338
44- def load_json_file (file_path : Path ) -> Dict [ str , Any ] :
45- """Load JSON data from a file. If it's the plugins cache, reconstruct PluginInfo objects. """
39+ def load_json_file (file_path : Path ) -> Any :
40+ """Load JSON data from a file."""
4641 if not file_path .exists ():
4742 return {}
4843
@@ -52,63 +47,12 @@ def load_json_file(file_path: Path) -> Dict[str, Any]:
5247 except json .JSONDecodeError :
5348 return {} # Return empty dict if JSON is corrupted
5449
55- # Check if this is the plugins cache file by its name
56- if file_path .name == f"{ PLUGINS_CACHE_FILENAME_BASE } .json" :
57- if not isinstance (raw_data , dict ):
58- return {} # Corrupted plugin cache
59-
60- reconstructed_plugins : Dict [str , PluginInfo ] = {}
61- for plugin_id , plugin_data_dict in raw_data .items ():
62- if not isinstance (plugin_data_dict , dict ):
63- continue # Skip malformed entries
64-
65- param_dicts = plugin_data_dict .get ("parameters" , {})
66- reconstructed_params : Dict [str , PluginParameter ] = {}
67- if isinstance (param_dicts , dict ):
68- for param_name , param_data_dict in param_dicts .items ():
69- if isinstance (param_data_dict , dict ):
70- try :
71- reconstructed_params [param_name ] = PluginParameter (
72- name = str (param_data_dict .get ("name" , param_name )),
73- value = param_data_dict .get ("value" , 0.0 ),
74- )
75- except TypeError as e :
76- print (
77- f"Warning: Could not reconstruct parameter { param_name } for { plugin_id } : { e } "
78- )
79- continue
80-
81- try :
82- reconstructed_plugins [plugin_id ] = PluginInfo (
83- id = plugin_data_dict .get ("id" , plugin_id ),
84- name = plugin_data_dict .get ("name" , "Unknown Plugin Name" ),
85- path = plugin_data_dict .get ("path" , "" ),
86- filename = plugin_data_dict .get ("filename" , "" ),
87- plugin_type = plugin_data_dict .get ("plugin_type" , "unknown" ),
88- parameters = reconstructed_params ,
89- manufacturer = plugin_data_dict .get ("manufacturer" ),
90- name_in_file = plugin_data_dict .get ("name_in_file" ),
91- )
92- except TypeError as e :
93- print (
94- f"Warning: Could not reconstruct plugin info for { plugin_id } : { e } "
95- )
96- continue
97-
98- return reconstructed_plugins
99-
100- # Handle non-plugin cache files
101- result : Dict [str , Any ] = {}
102- if isinstance (raw_data , dict ):
103- result = {str (k ): v for k , v in raw_data .items ()}
104- elif isinstance (raw_data , list ):
105- result = {str (i ): item for i , item in enumerate (raw_data )}
106- return result
50+ return raw_data
10751
10852
10953def save_json_file (data : Union [Dict [Any , Any ], List [Any ]], file_path : Path ) -> None :
11054 """Save JSON data to a file."""
111- ensure_folder (file_path )
55+ ensure_folder (file_path . parent )
11256 with open (file_path , "w" , encoding = "utf-8" ) as file :
11357 json .dump (data , file , indent = 4 )
11458
@@ -136,7 +80,7 @@ def copy_default_ignores(destination_path: Path) -> None:
13680 ).joinpath ("default_ignores.json" )
13781
13882 if not destination_path .exists ():
139- ensure_folder (destination_path )
83+ ensure_folder (destination_path . parent )
14084 with importlib .resources .as_file (
14185 default_ignores_src_path
14286 ) as src_file_on_fs :
@@ -149,5 +93,5 @@ def copy_default_ignores(destination_path: Path) -> None:
14993 f"Warning: Could not copy default ignores using importlib.resources: { e } . Creating empty ignores file."
15094 )
15195 if not destination_path .exists ():
152- ensure_folder (destination_path )
153- save_json_file ([], destination_path )
96+ ensure_folder (destination_path . parent )
97+ save_json_file ([], destination_path )
0 commit comments