-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
231 lines (183 loc) · 6.47 KB
/
settings.py
File metadata and controls
231 lines (183 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from __future__ import annotations
from dataclasses import dataclass, field
import os
from pathlib import Path
import pickle
from app_types import FilePlayMode, SegmentPlayMode
from util import *
@dataclass
class Settings:
"""
Program settings, backed by pickle file (must manually save)
"""
_dir_path: str = ""
_file_name: str = ""
_last_position: float = 0.0 # Playhead position of last file on quit
_fast_mode: bool = False
_start_end_auto: bool = False
_window_width: int = 1000
_window_position: tuple[int, int] = (-1, -1) # -1 means not set/use default
_timeline_zoom: float = 0.5
_play_mode: FilePlayMode | SegmentPlayMode = FilePlayMode.DEFAULT
_compressor_enabled: bool = False
# derived from dir_path; not saved
_file_paths: list[str] = field(default_factory=list)
def __getstate__(self):
state = self.__dict__.copy()
for item in BLACKLIST:
state.pop(item, None)
return state
def __setstate__(self, state):
self.__dict__.update(state)
# Clobber file_paths if exists for good measure
self._file_paths = []
@staticmethod
def load() -> Settings:
"""
Load state from pickle file.
On error, prints feedback and returns new/empty instance.
"""
try:
with open(PICKLE_PATH, "rb") as f:
instance: Settings = pickle.load(f)
except Exception as e:
print(f"* error loading prefs file: {e}")
return Settings()
bad_file = not isinstance(instance, Settings)
if bad_file:
# Bad file
print(f"* bad prefs file: {instance}")
return Settings()
bad_dir_path = instance._dir_path and (not os.path.exists(instance._dir_path) or not os.path.isdir(instance._dir_path))
if bad_dir_path:
print(f"* prefs file - no such directory: [{instance._dir_path}]")
instance._dir_path = ""
# Initialize
instance.window_width = max(instance.window_width, 250)
instance.refresh_paths()
return instance
def refresh_paths(self) -> None:
""" """
self._populate_file_paths()
# Validate that _file_name still exists in _file_paths
if self._file_name and self._file_name not in self._file_paths:
self._file_name = ""
self.save()
@property
def dir_path(self) -> str:
return self._dir_path
@dir_path.setter
def dir_path(self, value: str) -> None:
self._dir_path = value
self._populate_file_paths()
# Reset to first file if available
self._file_name = self._file_paths[0] if self._file_paths else ""
def set_dir_and_print(self, dir_path: str) -> None:
self.dir_path = dir_path
print("Set directory:", self.dir_path, "num items", len(self._file_paths))
@property
def file_name(self) -> str:
return self._file_name
@file_name.setter
def file_name(self, value: str) -> None:
# Allow any filename, no validation
# Do NOT reset _last_position
self._file_name = value
@property
def index(self) -> int:
if not self._file_name or not self._file_paths:
return -1
try:
return self._file_paths.index(self._file_name)
except ValueError:
return -1
@index.setter
def index(self, value: int) -> None:
# Set file_name based on index
# Do NOT reset _last_position
if 0 <= value < len(self._file_paths):
self._file_name = self._file_paths[value]
else:
self._file_name = ""
@property
def current_file_path(self) -> str:
if not self._dir_path or not self._file_name:
return ""
return os.path.join(self._dir_path, self._file_name)
@property
def file_paths(self) -> list[str]:
return self._file_paths
@property
def play_mode(self) -> FilePlayMode | SegmentPlayMode:
return self._play_mode
@play_mode.setter
def play_mode(self, value: FilePlayMode | SegmentPlayMode) -> None:
self._play_mode = value
@property
def fast_mode(self) -> bool:
return self._fast_mode
@fast_mode.setter
def fast_mode(self, value: bool) -> None:
self._fast_mode = value
@property
def compressor_enabled(self) -> bool:
return self._compressor_enabled
@compressor_enabled.setter
def compressor_enabled(self, value: bool) -> None:
self._compressor_enabled = value
@property
def start_end_auto(self) -> bool:
return self._start_end_auto
@start_end_auto.setter
def start_end_auto(self, value: bool) -> None:
self._start_end_auto = value
@property
def window_width(self) -> int:
return self._window_width
@window_width.setter
def window_width(self, value: int) -> None:
self._window_width = value
@property
def window_position(self) -> tuple[int, int]:
return self._window_position
@window_position.setter
def window_position(self, value: tuple[int, int]) -> None:
self._window_position = value
def set_window_geometry(self, x: int, y: int, width: int) -> None:
self._window_position = (x, y)
self._window_width = width
@property
def timeline_zoom(self) -> float:
return self._timeline_zoom
@timeline_zoom.setter
def timeline_zoom(self, value: float) -> None:
self._timeline_zoom = value
@property
def last_position(self) -> float:
return self._last_position
@last_position.setter
def last_position(self, value: float) -> None:
self._last_position = value
# ---
def save(self) -> None:
"""
Saves to pickle file
"""
try:
with open(PICKLE_PATH, "wb") as f:
pickle.dump(self, f)
except Exception as e:
print("* error:", e)
# ---
def _populate_file_paths(self) -> None:
if not self._dir_path:
return
from sound_files_util import SoundFilesUtil
result = SoundFilesUtil.get_sound_file_paths(self._dir_path, and_video=True)
if isinstance(result, str):
print(f"Error: {result}")
self._file_paths = []
else:
self._file_paths = result
PICKLE_PATH = os.path.join(Path.home(), ".wave-edit.pickle")
BLACKLIST = ["file_paths"]