Added functional config file -c\--config

This commit is contained in:
Zen 2020-12-08 02:21:36 +02:00
parent d1bd817317
commit 87192859d2
2 changed files with 62 additions and 11 deletions

View file

@ -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')

View file

@ -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)