Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import os
- import xml.etree.cElementTree as ET
- from fnmatch import fnmatch as match_filename
- from itertools import takewhile
- output_dir = os.path.abspath("outputs")
- first_attribute_id = "WaveFile"
- second_attribute_id = "WaveFile2"
- category_attribute_id = "Category"
- tags_attribute_id = "Tags"
- def files(path):
- for file in os.listdir(path):
- if os.path.isfile(os.path.join(path, file)):
- yield file
- def is_musicfile(file):
- return match_filename(file, "*.wav")
- def get_filename(file):
- (filename, _extension) = os.path.splitext(file)
- return filename
- def replace_after_last_seperator(seperator, old_string, new_part):
- head, sep, _tail = old_string.rpartition(seperator)
- return head + sep + new_part
- def parse_category(file):
- is_alphanumeric = lambda c: c.isalnum()
- category = str.join("", takewhile(is_alphanumeric, file))
- return category
- def parse_tags(file):
- # Throw away extension, since it is not included in tag
- filename = get_filename(file)
- # Remove category from file and 'index' (A or B) from file, as they are not included in tag.
- category = parse_category(filename)
- invalid = lambda tag: (tag == category) or (tag == "A") or (tag == "B")
- tags = [tag.strip() for tag in filename.split("-") if not invalid(tag.strip())]
- return tags
- def create_preset_file(xml_file_root, file_a, file_b):
- # Modify STATE tag
- for state in xml_file_root.iter("STATE"):
- # Get current text from sought after fields in STATE tag
- wavefile1 = state.get(first_attribute_id)
- wavefile2 = state.get(second_attribute_id)
- # Create new strings to replace template strings in STATE tag
- # os.sep is '\' on Windows, '/' on Unix
- new_first_attribute = replace_after_last_seperator(os.sep, wavefile1, file_a)
- new_second_attribute = replace_after_last_seperator(os.sep, wavefile2, file_b)
- # Write changes to XML tree
- state.set(first_attribute_id, new_first_attribute)
- state.set(second_attribute_id, new_second_attribute)
- # Modify Category, Tags inside ATTRIBUTES tag
- for attribute in xml_file_root.iter("ATTRIBUTES"):
- # Parse new Category and Tags from input file
- new_category = parse_category(file_a)
- new_tags = [tag.rstrip("s") for tag in parse_tags(file_a)]
- # Write changes to XML tree
- attribute.set(category_attribute_id, new_category)
- attribute.set(tags_attribute_id, " ".join(new_tags))
- # Main logic below ->
- # Create output folder if it does not exist
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
- # Parse alternative template file
- parser = argparse.ArgumentParser()
- parser.add_argument(
- dest="template", help="path to template xml", default="template.xml", nargs="?"
- )
- args = parser.parse_args()
- template_file = args.template
- # Read and partition music files to work on
- musicfiles = [file for file in files(".") if is_musicfile(file)]
- musicfile_pairs = [
- (file_a, file_b)
- for file_a in musicfiles
- if "A" in file_a
- for file_b in musicfiles
- if "B" in file_b
- if file_a.replace("A", "B") == file_b
- ]
- # Create xml files for every pair of music files
- for (file_a, file_b) in musicfile_pairs:
- # Parse template file and create editable root element
- template_document = ET.parse(template_file)
- template_document_root = template_document.getroot()
- # Create new preset file
- new_preset_file = create_preset_file(template_document_root, file_a, file_b)
- # Write changes
- suggested_filename = str.join(" ", parse_tags(file_a))
- new_preset_filename = f"{suggested_filename}.xml"
- write_path = os.path.join(output_dir, new_preset_filename)
- template_document.write(write_path, encoding="utf-8", xml_declaration=True)
- parse_tags(file_a)
- print(f"Done! Result can be found at {output_dir}")
Advertisement
Add Comment
Please, Sign In to add comment