From 94d7b74ed13d0b574f0b3b078e1b173c5c95ecf3 Mon Sep 17 00:00:00 2001 From: Collin Heist Date: Mon, 11 Apr 2022 16:40:01 -0600 Subject: [PATCH 1/2] Simplify repeated custom font specification In series YAML files, new `fonts` section can be specified which lists any number of custom fonts under a given _key_. That _key_ can then be specified as a `font:` value for a given series, using all of those attributes Implements #71 --- modules/Font.py | 13 ++++++++++++- modules/PreferenceParser.py | 10 +++++++--- modules/Show.py | 7 +++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/modules/Font.py b/modules/Font.py index 5b498d896..f2c7da347 100755 --- a/modules/Font.py +++ b/modules/Font.py @@ -12,17 +12,28 @@ class Font: interline spacing. """ - def __init__(self, yaml: dict, card_class: 'CardType', + def __init__(self, yaml, font_map: dict, card_class: 'CardType', series_info: 'SeriesInfo') -> None: """ Constructs a new instance of a Font for the given YAML, CardType, and series. :param yaml: 'font' dictionary from a series YAML file. + :type yaml: Dict or str. If str, must be a key of + font_map. + :param font_map: Map of font labels to custom font + descriptions. :param card_class: CardType class to use values from. :param series_info: Associated SeriesInfo (for logging). """ + # If the given value is a key of the font map, use those values instead + if isinstance(yaml, str) and yaml in font_map: + yaml = font_map[yaml] + elif not isinstance(yaml, dict): + log.error(f'Invalid font for series "{series_info}"') + yaml = {} + # Store arguments self.__yaml = yaml self.__card_class = card_class diff --git a/modules/PreferenceParser.py b/modules/PreferenceParser.py index e314813ff..58335b4b6 100755 --- a/modules/PreferenceParser.py +++ b/modules/PreferenceParser.py @@ -273,12 +273,15 @@ def iterate_series_files(self) -> [Show]: continue # Get library map for this file; error+skip missing library paths - libraries = file_yaml.get('libraries', {}) - if not all('path' in libraries[library] for library in libraries): + library_map = file_yaml.get('libraries', {}) + if not all('path' in library_map[lib] for lib in library_map): log.error(f'Libraries in series file "{file_object.resolve()}" ' f'are missing their "path" attributes.') continue + # Get font map for this file + font_map = file_yaml.get('fonts', {}) + # Go through each series in this file for show_name in tqdm(file_yaml['series'], leave=False, desc='Creating Shows'): @@ -286,7 +289,8 @@ def iterate_series_files(self) -> [Show]: yield Show( show_name, file_yaml['series'][show_name], - libraries, + library_map, + font_map, self.source_directory, ) diff --git a/modules/Show.py b/modules/Show.py index 70288243f..07231b649 100755 --- a/modules/Show.py +++ b/modules/Show.py @@ -24,8 +24,8 @@ class Show: """ - def __init__(self, name: str, yaml_dict: dict, library_map: dict, - source_directory: Path) -> None: + def __init__(self, name: str, yaml_dict: dict, library_map: dict, + font_map: dict, source_directory: Path) -> None: """ Constructs a new instance of a Show object from the given YAML dictionary, library map, and referencing the base source directory. If @@ -37,6 +37,8 @@ def __init__(self, name: str, yaml_dict: dict, library_map: dict, as found in a card YAML file. :param library_map: Map of library titles to media directories. + :param font_map: Map of font labels to custom font + descriptions. :param source_directory: Base source directory this show should search for and place source images. """ @@ -82,6 +84,7 @@ def __init__(self, name: str, yaml_dict: dict, library_map: dict, self.__parse_yaml() self.font = Font( self.__yaml.get('font', {}), + font_map, self.card_class, self.series_info ) From 896803fda3dab115625a84b2bed34544400692af Mon Sep 17 00:00:00 2001 From: Collin Heist Date: Mon, 11 Apr 2022 16:49:41 -0600 Subject: [PATCH 2/2] Handle bad custom font specification in YAML Now sets the Font validity attribute to False if the `font:` YAML is not a dictionary, instead of raising IndexError --- modules/Font.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/Font.py b/modules/Font.py index f2c7da347..7811ecfd8 100755 --- a/modules/Font.py +++ b/modules/Font.py @@ -22,16 +22,22 @@ def __init__(self, yaml, font_map: dict, card_class: 'CardType', :type yaml: Dict or str. If str, must be a key of font_map. :param font_map: Map of font labels to custom font - descriptions. + dictionaries. :param card_class: CardType class to use values from. :param series_info: Associated SeriesInfo (for logging). """ + # Assume object is valid to start with + self.valid = True + # If the given value is a key of the font map, use those values instead if isinstance(yaml, str) and yaml in font_map: yaml = font_map[yaml] - elif not isinstance(yaml, dict): + + # If font YAML (either from map or directly) is not a dictionary, bad! + if not isinstance(yaml, dict): log.error(f'Invalid font for series "{series_info}"') + self.valid = False yaml = {} # Store arguments @@ -39,14 +45,13 @@ def __init__(self, yaml, font_map: dict, card_class: 'CardType', self.__card_class = card_class self.__series_info = series_info - # This font's FontValidator object + # Use the global FontValidator object self.__validator = global_preferences.fv # Generic font attributes self.set_default() - # Parse YAML - self.valid = True + # Parse YAML, update validity self.__parse_attributes()