thesoxie

preset_creator

Apr 14th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. import argparse
  2. import os
  3. import xml.etree.cElementTree as ET
  4. from fnmatch import fnmatch as match_filename
  5. from itertools import takewhile
  6.  
  7. output_dir = os.path.abspath("outputs")
  8. first_attribute_id = "WaveFile"
  9. second_attribute_id = "WaveFile2"
  10. category_attribute_id = "Category"
  11. tags_attribute_id = "Tags"
  12.  
  13.  
  14. def files(path):
  15.     for file in os.listdir(path):
  16.         if os.path.isfile(os.path.join(path, file)):
  17.             yield file
  18.  
  19.  
  20. def is_musicfile(file):
  21.     return match_filename(file, "*.wav")
  22.  
  23.  
  24. def get_filename(file):
  25.     (filename, _extension) = os.path.splitext(file)
  26.     return filename
  27.  
  28.  
  29. def replace_after_last_seperator(seperator, old_string, new_part):
  30.     head, sep, _tail = old_string.rpartition(seperator)
  31.     return head + sep + new_part
  32.  
  33.  
  34. def parse_category(file):
  35.     is_alphanumeric = lambda c: c.isalnum()
  36.     category = str.join("", takewhile(is_alphanumeric, file))
  37.     return category
  38.  
  39.  
  40. def parse_tags(file):
  41.     # Throw away extension, since it is not included in tag
  42.     filename = get_filename(file)
  43.     # Remove category from file and 'index' (A or B) from file, as they are not included in tag.
  44.     category = parse_category(filename)
  45.     invalid = lambda tag: (tag == category) or (tag == "A") or (tag == "B")
  46.     tags = [tag.strip() for tag in filename.split("-") if not invalid(tag.strip())]
  47.     return tags
  48.  
  49.  
  50. def create_preset_file(xml_file_root, file_a, file_b):
  51.     # Modify STATE tag
  52.     for state in xml_file_root.iter("STATE"):
  53.         # Get current text from sought after fields in STATE tag
  54.         wavefile1 = state.get(first_attribute_id)
  55.         wavefile2 = state.get(second_attribute_id)
  56.         # Create new strings to replace template strings in STATE tag
  57.         # os.sep is '\' on Windows, '/' on Unix
  58.         new_first_attribute = replace_after_last_seperator(os.sep, wavefile1, file_a)
  59.         new_second_attribute = replace_after_last_seperator(os.sep, wavefile2, file_b)
  60.         # Write changes to XML tree
  61.         state.set(first_attribute_id, new_first_attribute)
  62.         state.set(second_attribute_id, new_second_attribute)
  63.  
  64.     # Modify Category, Tags inside ATTRIBUTES tag
  65.     for attribute in xml_file_root.iter("ATTRIBUTES"):
  66.         # Parse new Category and Tags from input file
  67.         new_category = parse_category(file_a)
  68.         new_tags = [tag.rstrip("s") for tag in parse_tags(file_a)]
  69.         # Write changes to XML tree
  70.         attribute.set(category_attribute_id, new_category)
  71.         attribute.set(tags_attribute_id, " ".join(new_tags))
  72.  
  73.  
  74. # Main logic below ->
  75. # Create output folder if it does not exist
  76. if not os.path.exists(output_dir):
  77.     os.makedirs(output_dir)
  78.  
  79. # Parse alternative template file
  80. parser = argparse.ArgumentParser()
  81. parser.add_argument(
  82.     dest="template", help="path to template xml", default="template.xml", nargs="?"
  83. )
  84.  
  85. args = parser.parse_args()
  86. template_file = args.template
  87.  
  88. # Read and partition music files to work on
  89. musicfiles = [file for file in files(".") if is_musicfile(file)]
  90. musicfile_pairs = [
  91.     (file_a, file_b)
  92.     for file_a in musicfiles
  93.     if "A" in file_a
  94.     for file_b in musicfiles
  95.     if "B" in file_b
  96.     if file_a.replace("A", "B") == file_b
  97. ]
  98.  
  99. # Create xml files for every pair of music files
  100. for (file_a, file_b) in musicfile_pairs:
  101.     # Parse template file and create editable root element
  102.     template_document = ET.parse(template_file)
  103.     template_document_root = template_document.getroot()
  104.     # Create new preset file
  105.     new_preset_file = create_preset_file(template_document_root, file_a, file_b)
  106.     # Write changes
  107.     suggested_filename = str.join(" ", parse_tags(file_a))
  108.     new_preset_filename = f"{suggested_filename}.xml"
  109.     write_path = os.path.join(output_dir, new_preset_filename)
  110.     template_document.write(write_path, encoding="utf-8", xml_declaration=True)
  111.     parse_tags(file_a)
  112.  
  113. print(f"Done! Result can be found at {output_dir}")
Advertisement
Add Comment
Please, Sign In to add comment