11import json
22import os
3- import platform
43import shutil
5- from importlib import resources
64from pathlib import Path
7- from typing import Any , Dict , List , Set , Union
85
9- # JSON-serializable types
10- JSONValue = Union [str , int , float , bool , None , Dict [str , Any ], List [Any ]]
11- JSONDict = Dict [str , JSONValue ]
6+ from pkg_resources import resource_filename
127
13- from .utils import ensure_folder
8+ from .utils import *
149
15- APP_NAME : str = "com.twardoch.pedalboard-pluginary"
16- PLUGINS_CACHE_FILENAME_BASE : str = "plugins" # To identify the plugins cache file
10+ APP_NAME = "com.twardoch.pedalboard-pluginary"
1711
1812
19- def get_cache_path (cache_name : str , extension : str = "json" ) -> Path :
13+ def get_cache_path (cache_name ) :
2014 """Get the path to a cache file."""
21- os_name = platform .system ()
22- if os_name == "Windows" :
23- app_data_env = os .getenv ("APPDATA" )
24- if app_data_env is None :
25- app_data_dir = (
26- Path (os .path .expanduser ("~" )) / "AppData" / "Roaming" / APP_NAME
27- )
28- else :
29- app_data_dir = Path (app_data_env ) / APP_NAME
30- elif os_name == "Darwin" : # macOS
31- app_data_dir = Path .home () / "Library" / "Application Support" / APP_NAME
32- else : # Linux and other Unix-like systems
33- xdg_cache_home_env = os .getenv ("XDG_CACHE_HOME" )
34- if xdg_cache_home_env :
35- app_data_dir = Path (xdg_cache_home_env ) / APP_NAME
36- else :
37- app_data_dir = Path .home () / ".cache" / APP_NAME
15+ if os .name == "nt" :
16+ cache_folder = Path (os .getenv ("APPDATA" )) / APP_NAME
17+ else :
18+ cache_folder = Path .home () / "Library" / "Application Support" / APP_NAME
19+ return cache_folder / f"{ cache_name } .json"
3820
39- app_data_dir .mkdir (parents = True , exist_ok = True ) # Ensure base app dir exists
40- return app_data_dir / f"{ cache_name } .{ extension } "
4121
42-
43- def get_sqlite_cache_path (cache_name : str ) -> Path :
44- """Get the path to a SQLite cache database."""
45- return get_cache_path (cache_name , "db" )
46-
47-
48- def load_json_file (file_path : Path ) -> Any :
22+ def load_json_file (file_path ):
4923 """Load JSON data from a file."""
50- if not file_path .exists ():
51- return {}
52-
53- with open (file_path , "r" , encoding = "utf-8" ) as file :
54- try :
55- raw_data = json .load (file )
56- except json .JSONDecodeError :
57- return {} # Return empty dict if JSON is corrupted
24+ if file_path .exists ():
25+ with open (file_path ) as file :
26+ return json .load (file )
27+ return {}
5828
59- return raw_data
6029
61-
62- def save_json_file (data : Any , file_path : Path ) -> None :
30+ def save_json_file (data , file_path ):
6331 """Save JSON data to a file."""
64- ensure_folder (file_path . parent )
65- with open (file_path , "w" , encoding = "utf-8" ) as file :
32+ ensure_folder (file_path )
33+ with open (file_path , "w" ) as file :
6634 json .dump (data , file , indent = 4 )
6735
6836
69- def load_ignores (ignores_path : Path ) -> Set [str ]:
70- """Load ignores data (list of strings) from the file."""
71- content = load_json_file (ignores_path )
72- if isinstance (content , list ): # Expects a list of strings
73- return set (item for item in content if isinstance (item , str ))
74- return set ()
37+ def load_ignores (ignores_path ):
38+ """Load ignores data from the file."""
39+ return set (load_json_file (ignores_path ))
7540
7641
77- def save_ignores (ignores : Set [ str ] , ignores_path : Path ) -> None :
42+ def save_ignores (ignores , ignores_path ) :
7843 """Save ignores data to the file."""
7944 save_json_file (sorted (list (ignores )), ignores_path )
8045
8146
82- def copy_default_ignores (destination_path : Path ) -> None :
47+ def copy_default_ignores (destination_path ) :
8348 """Copy the default ignores file to the destination if it does not exist."""
84- try :
85- import importlib .resources
86-
87- default_ignores_src_path = importlib .resources .files (
88- "pedalboard_pluginary.resources"
89- ).joinpath ("default_ignores.json" )
90-
91- if not destination_path .exists ():
92- ensure_folder (destination_path .parent )
93- with importlib .resources .as_file (
94- default_ignores_src_path
95- ) as src_file_on_fs :
96- if src_file_on_fs .exists ():
97- shutil .copy (src_file_on_fs , destination_path )
98- else :
99- save_json_file ([], destination_path )
100- except (ImportError , FileNotFoundError , TypeError ) as e :
101- print (
102- f"Warning: Could not copy default ignores using importlib.resources: { e } . Creating empty ignores file."
103- )
104- if not destination_path .exists ():
105- ensure_folder (destination_path .parent )
106- save_json_file ([], destination_path )
49+ default_ignores_path = resource_filename (__name__ , "resources/default_ignores.json" )
50+ if not destination_path .exists ():
51+ ensure_folder (destination_path )
52+ shutil .copy (default_ignores_path , destination_path )
0 commit comments