Skip to content

Commit

Permalink
implement --saveconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjoyce committed Apr 5, 2024
1 parent 962250e commit 6c3a2f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
23 changes: 19 additions & 4 deletions melspec-to-video.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def create_vertical_axis(

# localise some variables
width, height = image_size
axis = params.get("overlays", {}).get("frequency_axis")
axis = params.overlays.get("frequency_axis")
x_pos = width * axis["axis_position"]
ink_color = tuple(axis["axis_rgba"])
melspec = params.mel_spectrogram
Expand Down Expand Up @@ -729,6 +729,15 @@ def create_vertical_axis(
return axis_image


def save_config(params: Params) -> bool:
# setup some keys we want to exclude
params.set_exclusions(["config", "saveconfig"])
print(params.exclusions)
newfile = Path(params.saveconfig).resolve()
params.save_to_yaml(str(newfile))
return True


def main():
"""process command line argument and config"""
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -794,6 +803,12 @@ def main():
default=None,
)

parser.add_argument(
"--saveconfig",
help="Combine args with config and save.",
default=None,
)

args = parser.parse_args()
params = Params(args.config, args=args, file_type="yaml")

Expand All @@ -812,11 +827,11 @@ def main():
else:
print(f"Source Audio Path : {source_audio_path}")

# the mp4 file - will be tested later and overwriten if needed
output_path = Path(args.output).resolve()

if args.saveconfig:
save_config(params)
print(params)

## Project Data
if args.path:
project_path = Path(args.path).resolve()
if not project_path.exists():
Expand Down
21 changes: 12 additions & 9 deletions params.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
if args:
self.overlay_args(args)
self._set_dynamic_attributes()
self.exclusions: List[str] = [] # List to hold keys to exclude during save
self._exclusions: List[str] = [] # List to hold keys to exclude during save

def _load_config(self, file_path: Optional[str], file_type: str):
if file_path:
Expand All @@ -62,15 +62,18 @@ def __getattr__(self, name: str) -> Any:
raise AttributeError(f"'Params' object has no attribute '{name}'")

def __setattr__(self, name: str, value: Any):
"""Allows setting configuration values as attributes."""
# This check prevents infinite recursion by allowing direct modification of '_config'
if name == "_config":
"""Allows setting configuration values as attributes, distinguishing between
special attributes and configuration keys."""
if name in ["_exclusions"]:
# Directly handle special attributes; they should not be added to _config
object.__setattr__(self, name, value)
elif name == "_config" or name.startswith("_"):
# Handle private attributes, including _config itself, normally
super().__setattr__(name, value)
else:
# All other attributes are treated as configuration keys
self._config[name] = value
super().__setattr__(
name, value
) # Optionally update the attribute directly as well
super().__setattr__(name, value)

def __repr__(self):
# Convert the internal configuration dictionary to a pretty-printed string
Expand All @@ -82,7 +85,7 @@ def set_exclusions(self, keys: List[str]):
Args:
keys (List[str]): A list of keys to exclude.
"""
self.exclusions = keys
self._exclusions = keys

def load_yaml_config(self, config_path: str) -> Dict[str, Any]:
"""Loads a YAML configuration file using pathlib for enhanced path handling.
Expand Down Expand Up @@ -153,7 +156,7 @@ def save_to_yaml(self, file_path: str):
config_to_save = {
key: value
for key, value in self._config.items()
if key not in self.exclusions
if key not in self._exclusions
}
path = Path(file_path)
content = yaml.dump(config_to_save, default_flow_style=False)
Expand Down

0 comments on commit 6c3a2f6

Please sign in to comment.