Guest User

preset_creator

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