Guest User

preset_merger

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