refactor(config): update access methods for configuration properties
This commit is contained in:
@@ -100,7 +100,7 @@ def main(argv=None) -> int:
|
|||||||
elif args.config_command == "get":
|
elif args.config_command == "get":
|
||||||
print(cfg.get(args.key))
|
print(cfg.get(args.key))
|
||||||
else:
|
else:
|
||||||
cfg.show()
|
parser.error("Unknown config command: expected 'get' or 'set'")
|
||||||
else:
|
else:
|
||||||
parser.error("Unknown command")
|
parser.error("Unknown command")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class Config:
|
|||||||
self._config_file = self._config_dir / _CONFIG_NAME
|
self._config_file = self._config_dir / _CONFIG_NAME
|
||||||
self._data = self._load()
|
self._data = self._load()
|
||||||
|
|
||||||
|
# Private
|
||||||
def _load(self) -> dict:
|
def _load(self) -> dict:
|
||||||
if not self._config_file.exists():
|
if not self._config_file.exists():
|
||||||
self._config_dir.mkdir(parents=True, exist_ok=True)
|
self._config_dir.mkdir(parents=True, exist_ok=True)
|
||||||
@@ -28,57 +29,46 @@ class Config:
|
|||||||
with self._config_file.open("wb") as f:
|
with self._config_file.open("wb") as f:
|
||||||
tomli_w.dump(self._data, f)
|
tomli_w.dump(self._data, f)
|
||||||
|
|
||||||
@property
|
# Public
|
||||||
def obs_host(self) -> str:
|
def get_obs_host(self) -> str:
|
||||||
return self._data["obs"]["host"]
|
return self._data["obs"]["host"]
|
||||||
|
|
||||||
@obs_host.setter
|
def set_obs_host(self, value: str) -> None:
|
||||||
def obs_host(self, value: str) -> None:
|
|
||||||
self._data["obs"]["host"] = value
|
self._data["obs"]["host"] = value
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
@property
|
def get_obs_port(self) -> int:
|
||||||
def obs_port(self) -> int:
|
|
||||||
return self._data["obs"]["port"]
|
return self._data["obs"]["port"]
|
||||||
|
|
||||||
@obs_port.setter
|
def set_obs_port(self, value: int) -> None:
|
||||||
def obs_port(self, value: int) -> None:
|
|
||||||
self._data["obs"]["port"] = value
|
self._data["obs"]["port"] = value
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
@property
|
def get_obs_password(self) -> str:
|
||||||
def obs_password(self) -> str:
|
|
||||||
return self._data["obs"]["password"]
|
return self._data["obs"]["password"]
|
||||||
|
|
||||||
@obs_password.setter
|
def set_obs_password(self, value: str) -> None:
|
||||||
def obs_password(self, value: str) -> None:
|
|
||||||
self._data["obs"]["password"] = value
|
self._data["obs"]["password"] = value
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
@property
|
def get_max_record_time(self) -> int:
|
||||||
def max_record_time(self) -> int:
|
|
||||||
return self._data["record"]["max_record_time"]
|
return self._data["record"]["max_record_time"]
|
||||||
|
|
||||||
@max_record_time.setter
|
def set_max_record_time(self, value: int) -> None:
|
||||||
def max_record_time(self, value: int) -> None:
|
|
||||||
self._data["record"]["max_record_time"] = value
|
self._data["record"]["max_record_time"] = value
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
@property
|
def get_clip_output(self) -> str:
|
||||||
def clip_output(self) -> str:
|
|
||||||
return os.path.expanduser(self._data["record"]["clip_output"])
|
return os.path.expanduser(self._data["record"]["clip_output"])
|
||||||
|
|
||||||
@clip_output.setter
|
def set_clip_output(self, value: str) -> None:
|
||||||
def clip_output(self, value: str) -> None:
|
|
||||||
self._data["record"]["clip_output"] = value
|
self._data["record"]["clip_output"] = value
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
@property
|
def get_vod_output(self) -> str:
|
||||||
def vod_output(self) -> str:
|
|
||||||
return os.path.expanduser(self._data["record"]["vod_output"])
|
return os.path.expanduser(self._data["record"]["vod_output"])
|
||||||
|
|
||||||
@vod_output.setter
|
def set_vod_output(self, value: str) -> None:
|
||||||
def vod_output(self, value: str) -> None:
|
|
||||||
self._data["record"]["vod_output"] = value
|
self._data["record"]["vod_output"] = value
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
@@ -97,8 +87,3 @@ class Config:
|
|||||||
raise KeyError(f"Unknown config key: {key!r}")
|
raise KeyError(f"Unknown config key: {key!r}")
|
||||||
self._data[section][field] = type(current)(value)
|
self._data[section][field] = type(current)(value)
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
def show(self) -> None:
|
|
||||||
for section, values in self._data.items():
|
|
||||||
for field, value in values.items():
|
|
||||||
print(f"{section}.{field} = {value!r}")
|
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ from rewind.state import load_state, add_marker_to_state, remove_marker_from_sta
|
|||||||
from rewind.config import Config
|
from rewind.config import Config
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
config = Config()
|
||||||
|
|
||||||
def clip(seconds_from_end: float) -> None:
|
def clip(seconds_from_end: float) -> None:
|
||||||
if seconds_from_end <= 0 or seconds_from_end > 600:
|
if seconds_from_end <= 0 or seconds_from_end > 600:
|
||||||
raise ValueError("Clip length must be positive and less than or equal to 10 minutes")
|
raise ValueError("Clip length must be positive and less than or equal to 10 minutes")
|
||||||
|
|
||||||
clip_output = Config().clip_output
|
clip_output = Config().get_clip_output()
|
||||||
os.makedirs(clip_output, exist_ok=True)
|
os.makedirs(clip_output, exist_ok=True)
|
||||||
|
|
||||||
output_file_name = f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}.mp4"
|
output_file_name = f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}.mp4"
|
||||||
@@ -31,7 +33,7 @@ def clip(seconds_from_end: float) -> None:
|
|||||||
print(f"Created clip: {output_path}")
|
print(f"Created clip: {output_path}")
|
||||||
|
|
||||||
def save(first_marker: str, second_marker: str):
|
def save(first_marker: str, second_marker: str):
|
||||||
vod_dir = Config().vod_output
|
vod_dir = config.get_vod_output()
|
||||||
os.makedirs(vod_dir, exist_ok=True)
|
os.makedirs(vod_dir, exist_ok=True)
|
||||||
|
|
||||||
first_timestamp = get_marker_timestamp(first_marker)
|
first_timestamp = get_marker_timestamp(first_marker)
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ class Handler(FileSystemEventHandler):
|
|||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
config = Config()
|
config = Config()
|
||||||
con = open_obs_connection(config.obs_host, config.obs_port, config.obs_password, OBS_MAX_RETRIES)
|
con = open_obs_connection(config.get_obs_host(), config.get_obs_port(), config.get_obs_password(), OBS_MAX_RETRIES)
|
||||||
|
|
||||||
recording_dir = con.get_record_directory().record_directory
|
recording_dir = con.get_record_directory().record_directory
|
||||||
start_recording(con)
|
start_recording(con)
|
||||||
@@ -103,8 +103,8 @@ def main() -> None:
|
|||||||
observer.start()
|
observer.start()
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
cleanup_physical_files(recording_dir, config.max_record_time)
|
cleanup_physical_files(recording_dir, config.get_max_record_time())
|
||||||
cleanup_state(config.max_record_time)
|
cleanup_state(config.get_max_record_time())
|
||||||
time.sleep(INTERVAL)
|
time.sleep(INTERVAL)
|
||||||
finally:
|
finally:
|
||||||
if observer:
|
if observer:
|
||||||
|
|||||||
Reference in New Issue
Block a user