Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import argparse
- from itertools import takewhile
- import xml.etree.cElementTree as ET
- 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):
- (_, extension) = os.path.splitext(file)
- return extension == '.wav'
- def get_filename(file):
- (filename, _) = 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()
- return ''.join(takewhile(is_alphanumeric, file))
- 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().rstrip('s') 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
- new_first_attribute = replace_after_last_seperator('\\', wavefile1, file_a)
- new_second_attribute = replace_after_last_seperator('\\', 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 = 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', nargs='?', default='template.xml')
- args = parser.parse_args()
- template_file = args.template
- # Read and partition music files to work on
- musicfiles_in_dir = [file for file in files('.') if is_musicfile(file)]
- a_files = [file for file in musicfiles_in_dir if 'A' in file]
- b_files = [file for file in musicfiles_in_dir if 'B' in file]
- 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]
- # 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
- output_file = f'{get_filename(file_a)}-and-{get_filename(file_b)}.xml'
- write_path = os.path.join(output_dir, output_file)
- template_document.write(write_path)
- print(f'Done! Result can be found at {output_dir}')
Advertisement
Add Comment
Please, Sign In to add comment