[14] add metadata template_variables

pull/847/head
meisnate12 3 years ago
parent ae3b150b68
commit 2adab8d700

@ -1 +1 @@
1.16.3-develop13 1.16.3-develop14

@ -492,6 +492,9 @@ class ConfigFile:
paths_to_check = [self.data["playlist_files"]] paths_to_check = [self.data["playlist_files"]]
for path in paths_to_check: for path in paths_to_check:
if isinstance(path, dict): if isinstance(path, dict):
temp_vars = {}
if "template_variables" in path and path["template_variables"] and isinstance(path["template_variables"], dict):
temp_vars = path["template_variables"]
def check_dict(attr): def check_dict(attr):
if attr in path: if attr in path:
if path[attr] is None: if path[attr] is None:
@ -503,36 +506,36 @@ class ConfigFile:
url = check_dict("url") url = check_dict("url")
if url: if url:
playlists_pairs.append(("URL", url)) playlists_pairs.append(("URL", url, temp_vars))
git = check_dict("git") git = check_dict("git")
if git: if git:
playlists_pairs.append(("Git", git)) playlists_pairs.append(("Git", git, temp_vars))
repo = check_dict("repo") repo = check_dict("repo")
if repo: if repo:
playlists_pairs.append(("Repo", repo)) playlists_pairs.append(("Repo", repo, temp_vars))
file = check_dict("file") file = check_dict("file")
if file: if file:
playlists_pairs.append(("File", file)) playlists_pairs.append(("File", file, temp_vars))
folder = check_dict("folder") folder = check_dict("folder")
if folder: if folder:
if os.path.isdir(folder): if os.path.isdir(folder):
yml_files = util.glob_filter(os.path.join(folder, "*.yml")) yml_files = util.glob_filter(os.path.join(folder, "*.yml"))
if yml_files: if yml_files:
playlists_pairs.extend([("File", yml) for yml in yml_files]) playlists_pairs.extend([("File", yml, temp_vars) for yml in yml_files])
else: else:
logger.error(f"Config Error: No YAML (.yml) files found in {folder}") logger.error(f"Config Error: No YAML (.yml) files found in {folder}")
else: else:
logger.error(f"Config Error: Folder not found: {folder}") logger.error(f"Config Error: Folder not found: {folder}")
else: else:
if os.path.exists(path): if os.path.exists(path):
playlists_pairs.append(("File", path)) playlists_pairs.append(("File", path, {}))
else: else:
logger.warning(f"Config Warning: Path not found: {path}") logger.warning(f"Config Warning: Path not found: {path}")
else: else:
logger.warning("playlist_files attribute not found") logger.warning("playlist_files attribute not found")
for file_type, playlist_file in playlists_pairs: for file_type, playlist_file, temp_vars in playlists_pairs:
try: try:
playlist_obj = PlaylistFile(self, file_type, playlist_file) playlist_obj = PlaylistFile(self, file_type, playlist_file, temp_vars)
self.playlist_names.extend([p for p in playlist_obj.playlists]) self.playlist_names.extend([p for p in playlist_obj.playlists])
self.playlist_files.append(playlist_obj) self.playlist_files.append(playlist_obj)
except Failed as e: except Failed as e:
@ -824,6 +827,9 @@ class ConfigFile:
paths_to_check = lib["metadata_path"] if isinstance(lib["metadata_path"], list) else [lib["metadata_path"]] paths_to_check = lib["metadata_path"] if isinstance(lib["metadata_path"], list) else [lib["metadata_path"]]
for path in paths_to_check: for path in paths_to_check:
if isinstance(path, dict): if isinstance(path, dict):
temp_vars = {}
if "template_variables" in path and path["template_variables"] and isinstance(path["template_variables"], dict):
temp_vars = path["template_variables"]
def check_dict(attr, name): def check_dict(attr, name):
if attr in path: if attr in path:
if path[attr] is None: if path[attr] is None:
@ -831,16 +837,16 @@ class ConfigFile:
self.errors.append(err) self.errors.append(err)
logger.error(err) logger.error(err)
else: else:
params["metadata_path"].append((name, path[attr])) params["metadata_path"].append((name, path[attr], temp_vars))
check_dict("url", "URL") check_dict("url", "URL")
check_dict("git", "Git") check_dict("git", "Git")
check_dict("repo", "Repo") check_dict("repo", "Repo")
check_dict("file", "File") check_dict("file", "File")
check_dict("folder", "Folder") check_dict("folder", "Folder")
else: else:
params["metadata_path"].append(("File", path)) params["metadata_path"].append(("File", path, {}))
else: else:
params["metadata_path"] = [("File", os.path.join(default_dir, f"{library_name}.yml"))] params["metadata_path"] = [("File", os.path.join(default_dir, f"{library_name}.yml"), {})]
params["default_dir"] = default_dir params["default_dir"] = default_dir
params["skip_library"] = False params["skip_library"] = False

@ -114,21 +114,21 @@ class Library(ABC):
def scan_metadata_files(self): def scan_metadata_files(self):
metadata = [] metadata = []
for file_type, metadata_file in self.metadata_path: for file_type, metadata_file, temp_vars in self.metadata_path:
if file_type == "Folder": if file_type == "Folder":
if os.path.isdir(metadata_file): if os.path.isdir(metadata_file):
yml_files = util.glob_filter(os.path.join(metadata_file, "*.yml")) yml_files = util.glob_filter(os.path.join(metadata_file, "*.yml"))
if yml_files: if yml_files:
metadata.extend([("File", yml) for yml in yml_files]) metadata.extend([("File", yml, temp_vars) for yml in yml_files])
else: else:
logger.error(f"Config Error: No YAML (.yml) files found in {metadata_file}") logger.error(f"Config Error: No YAML (.yml) files found in {metadata_file}")
else: else:
logger.error(f"Config Error: Folder not found: {metadata_file}") logger.error(f"Config Error: Folder not found: {metadata_file}")
else: else:
metadata.append((file_type, metadata_file)) metadata.append((file_type, metadata_file, temp_vars))
for file_type, metadata_file in metadata: for file_type, metadata_file, temp_vars in metadata:
try: try:
meta_obj = MetadataFile(self.config, self, file_type, metadata_file) meta_obj = MetadataFile(self.config, self, file_type, metadata_file, temp_vars)
if meta_obj.collections: if meta_obj.collections:
self.collections.extend([c for c in meta_obj.collections]) self.collections.extend([c for c in meta_obj.collections])
if meta_obj.metadata: if meta_obj.metadata:

@ -56,11 +56,12 @@ def get_dict(attribute, attr_data, check_list=None):
class DataFile: class DataFile:
def __init__(self, config, file_type, path): def __init__(self, config, file_type, path, temp_vars):
self.config = config self.config = config
self.library = None self.library = None
self.type = file_type self.type = file_type
self.path = path self.path = path
self.temp_vars = temp_vars
self.data_type = "" self.data_type = ""
self.templates = {} self.templates = {}
@ -130,6 +131,9 @@ class DataFile:
variables["playlist_name"] = str(name) variables["playlist_name"] = str(name)
variables["library_type"] = self.library.type.lower() variables["library_type"] = self.library.type.lower()
for temp_key, temp_value in self.temp_vars.items():
variables[temp_key] = temp_value
template_name = variables["name"] template_name = variables["name"]
template = self.templates[template_name] template = self.templates[template_name]
@ -233,8 +237,8 @@ class DataFile:
class MetadataFile(DataFile): class MetadataFile(DataFile):
def __init__(self, config, library, file_type, path): def __init__(self, config, library, file_type, path, temp_vars):
super().__init__(config, file_type, path) super().__init__(config, file_type, path, temp_vars)
self.data_type = "Collection" self.data_type = "Collection"
self.library = library self.library = library
if file_type == "Data": if file_type == "Data":
@ -1047,8 +1051,8 @@ class MetadataFile(DataFile):
class PlaylistFile(DataFile): class PlaylistFile(DataFile):
def __init__(self, config, file_type, path): def __init__(self, config, file_type, path, temp_vars):
super().__init__(config, file_type, path) super().__init__(config, file_type, path, temp_vars)
self.data_type = "Playlist" self.data_type = "Playlist"
self.playlists = {} self.playlists = {}
logger.info("") logger.info("")

@ -855,7 +855,7 @@ def library_operations(config, library):
def loop_dict(looping, dest_dict): def loop_dict(looping, dest_dict):
if not looping: if not looping:
return None return None
for lk, lv in looping: for lk, lv in looping.items():
dest_dict[lk] = loop_dict(lv, dest_dict[lk] if lk in dest_dict and dest_dict[lk] else {}) if isinstance(lv, dict) else lv dest_dict[lk] = loop_dict(lv, dest_dict[lk] if lk in dest_dict and dest_dict[lk] else {}) if isinstance(lv, dict) else lv
return dest_dict return dest_dict
meta["metadata"][map_key] = loop_dict(get_dict(attrs), og_dict) meta["metadata"][map_key] = loop_dict(get_dict(attrs), og_dict)

Loading…
Cancel
Save