From 87192859d288d759f098899e30d7934007c63578 Mon Sep 17 00:00:00 2001 From: Zen Date: Tue, 8 Dec 2020 02:21:36 +0200 Subject: [PATCH] Added functional config file `-c`\`--config` --- Av1an/arg_parse.py | 33 +++++++++++++++++++++++---------- Projects/Project.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/Av1an/arg_parse.py b/Av1an/arg_parse.py index 7382e56..5d20f71 100755 --- a/Av1an/arg_parse.py +++ b/Av1an/arg_parse.py @@ -25,25 +25,22 @@ class Args: self.project = Project(self.parsed) + if self.project.config: + self.save_load_project_file() + return self.project - def get_defaults(self): + def get_defaults(self) -> dict: """ - Get default values specified in + Get dictionary of default values specified in arg_parsing() """ return vars(self.parser.parse_args([])) - def get_unique(self): - """ - Return difference in default and new dictionaries - """ - return - - def get_difference(self): + def get_difference(self) -> dict: """ Return difference of defaults and new """ - return [x for x in self.parsed.items() if x not in self.defaults.items()] + return dict([x for x in self.parsed.items() if x not in self.defaults.items()]) def parse(self): """ @@ -54,6 +51,21 @@ class Args: self.parser.print_help() sys.exit() + def save_load_project_file(self): + """ + Saves current/Loads given project file, loads saved project first and when overwrites only unique values from current parse + """ + cfg_path = Path(self.project.config) + + if cfg_path.exists(): + + new = self.get_difference() + self.project.load_project_from_file(self.project.config) + self.project.load_project(new) + + else: + self.project.save_project_to_file(self.project.config) + def arg_parsing(self): """Command line parsing and setting default variables""" parser = argparse.ArgumentParser() @@ -68,6 +80,7 @@ class Args: io_group.add_argument('--logging', '-log', type=str, default=None, help='Enable logging') io_group.add_argument('--resume', '-r', help='Resuming previous session', action='store_true') io_group.add_argument('--keep', help='Keep temporally folder after encode', action='store_true') + io_group.add_argument('--config', '-c', type=str, default=None, help="Path to config file, create if doesn't exists") # Splitting split_group = parser.add_argument_group('Splitting') diff --git a/Projects/Project.py b/Projects/Project.py index 0ecaa7c..34d560b 100644 --- a/Projects/Project.py +++ b/Projects/Project.py @@ -1,4 +1,4 @@ - +import json from pathlib import Path from Av1an.commandtypes import Command from Av1an.utils import frame_probe_fast @@ -17,6 +17,7 @@ class Project(object): self.temp: Path = None self.output_file: Path = None self.mkvmerge: bool = None + self.config = None # Splitting self.chunk_method: str = None @@ -70,6 +71,13 @@ class Project(object): self.video_dimensions = (None, None) self.video_framerate = None + # Set all initial values + self.load_project(initial_data) + + def load_project(self, initial_data): + """ + Loads project attributes to this class + """ # Set all initial values for key in initial_data: setattr(self, key, initial_data[key]) @@ -100,3 +108,33 @@ class Project(object): suffix = '.mkv' self.output_file = Path(self.output_file).with_suffix(suffix) if self.output_file \ else Path(f'{self.input.stem}_{self.encoder}{suffix}') + + def load_project_from_file(self, path_string): + """ + Loads projedt attributes from json to this class + """ + pth = Path(path_string) + with open(pth) as json_data: + data = json.load(json_data) + self.load_project(data) + + def save_project_to_file(self, path_string): + """ + Save project attributes from json to this class + """ + pth = Path(path_string) + with open(pth, 'w') as json_data: + json_data.write(self.save_project()) + + def save_project(self): + """ + Returns json of this class, which later can be loaded + """ + dt = dict(self.__dict__) + del dt['input'] + del dt['output_file'] + del dt['temp'] + del dt['vmaf_path'] + del dt['config'] + return json.dumps(dt, indent=4, sort_keys=True) +