Skip to content

Commit 75229d0

Browse files
committed
Auto-commit: Save local changes
1 parent d866ac8 commit 75229d0

8 files changed

Lines changed: 280 additions & 517 deletions

File tree

src/pedalboard_pluginary/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
finally:
1010
del version, PackageNotFoundError
1111

12-
from .core import PedalboardPluginary
12+
from .core import PedalboardPluginary
Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,43 @@
11
#!/usr/bin/env python3
2-
from __future__ import annotations
3-
4-
from typing import Dict, List, Optional
5-
62
import fire
7-
import yaml
8-
9-
from .data import (
10-
PLUGINS_CACHE_FILENAME_BASE,
11-
get_cache_path,
12-
load_json_file,
13-
save_json_file,
14-
)
15-
from .types import SerializedPlugin
3+
from benedict import benedict as bdict
4+
5+
from .core import PedalboardPluginary
166
from .scanner import PedalboardScanner
177

188

19-
def scan_plugins_cli(extra_folders: Optional[str] = None, verbose: int = 0) -> None:
20-
"""Scans all plugins, optionally including extra folders (comma-separated string)."""
21-
folders_list: List[str] = extra_folders.split(",") if extra_folders else []
22-
scanner = PedalboardScanner(specific_paths=folders_list)
23-
scanner.full_scan() # This updates scanner.plugins
24-
if scanner.plugins: # Only save if we found plugins
25-
cache_file = get_cache_path(PLUGINS_CACHE_FILENAME_BASE)
26-
save_json_file(scanner.plugins, cache_file)
9+
def scan_plugins(extra_folders=None):
10+
if extra_folders:
11+
extra_folders = extra_folders.split(",")
12+
PedalboardScanner().rescan(extra_folders=None)
2713

2814

29-
def update_plugins_cli(extra_folders: Optional[str] = None, verbose: int = 0) -> None:
30-
"""Updates the plugin cache, optionally including extra folders (comma-separated string)."""
31-
scan_plugins_cli(extra_folders, verbose)
15+
def update_plugins(extra_folders=None):
16+
if extra_folders:
17+
extra_folders = extra_folders.split(",")
18+
PedalboardScanner().update(extra_folders=None)
3219

3320

34-
def list_json_cli() -> Dict[str, SerializedPlugin]:
35-
"""Lists all plugins in JSON format."""
36-
cache_file = get_cache_path(PLUGINS_CACHE_FILENAME_BASE)
37-
if not cache_file.exists():
38-
return {}
39-
data = load_json_file(cache_file)
40-
return data if isinstance(data, dict) else {}
21+
def list_json():
22+
return bdict(PedalboardPluginary().plugins).to_json()
4123

4224

43-
def list_yaml_cli() -> str:
44-
"""Lists all plugins in YAML format."""
45-
plugins = list_json_cli()
46-
return yaml.dump(plugins, sort_keys=False, indent=2)
25+
def list_yaml():
26+
return bdict(PedalboardPluginary().plugins).to_yaml()
4727

4828

49-
def main() -> None:
50-
"""Main entry point for the CLI."""
51-
fire.Fire({
52-
"scan": scan_plugins_cli,
53-
"list": list_json_cli,
54-
"json": list_json_cli,
55-
"yaml": list_yaml_cli,
56-
"update": update_plugins_cli,
57-
})
29+
def cli():
30+
fire.core.Display = lambda lines, out: print(*lines, file=out)
31+
fire.Fire(
32+
{
33+
"scan": scan_plugins,
34+
"update": update_plugins,
35+
"list": list_json,
36+
"json": list_json,
37+
"yaml": list_yaml,
38+
}
39+
)
5840

5941

6042
if __name__ == "__main__":
61-
main()
43+
cli()

src/pedalboard_pluginary/core.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
import json
2-
from pathlib import Path
3-
from typing import Dict
42

5-
from .data import get_cache_path
6-
from .models import PluginInfo
3+
from .data import get_cache_path, load_json_file
74
from .scanner import PedalboardScanner
8-
from .serialization import PluginSerializer
95

106

117
class PedalboardPluginary:
12-
"""Main class for the Pedalboard Pluginary application."""
13-
14-
plugins_path: Path
15-
plugins: Dict[str, PluginInfo]
16-
17-
def __init__(self) -> None:
18-
"""Initialize the Pedalboard Pluginary instance."""
8+
def __init__(self):
199
self.plugins_path = get_cache_path("plugins")
20-
self.plugins = {}
2110
self.load_data()
2211

23-
def load_data(self) -> None:
24-
"""Load plugin data from cache or perform a scan if cache doesn't exist."""
12+
def load_data(self):
2513
if not self.plugins_path.exists():
2614
scanner = PedalboardScanner()
27-
scanner.full_scan()
28-
29-
# Load plugins using the serializer
30-
self.plugins = PluginSerializer.load_plugins(self.plugins_path)
15+
scanner.scan()
16+
self.plugins = load_json_file(self.plugins_path)
3117

32-
def list_plugins(self) -> str:
33-
"""Returns a JSON string representation of the plugins."""
34-
# Convert PluginInfo objects to dictionaries for JSON serialization
35-
plugins_dict = {}
36-
for plugin_id, plugin in self.plugins.items():
37-
plugins_dict[plugin_id] = PluginSerializer.plugin_to_dict(plugin)
38-
39-
return json.dumps(plugins_dict, indent=4)
18+
def list_plugins(self):
19+
return json.dumps(self.plugins, indent=4)

src/pedalboard_pluginary/data.py

Lines changed: 26 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,52 @@
11
import json
22
import os
3-
import platform
43
import shutil
5-
from importlib import resources
64
from 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

Comments
 (0)