Skip to content

Instantly share code, notes, and snippets.

@Grezzo
Created November 7, 2016 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grezzo/92cf94c8e1e1f1a58d3e2c1a4098fcaf to your computer and use it in GitHub Desktop.
Save Grezzo/92cf94c8e1e1f1a58d3e2c1a4098fcaf to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import logging
def main():
import argparse, os, codecs, sys
from xml.dom.minidom import parse
#Set up argument parser
parser = argparse.ArgumentParser(description="Remove elements from an fcpxml",
epilog="Latest version available at https://gist.github.com/Grezzo/92cf94c8e1e1f1a58d3e2c1a4098fcaf")
parser.add_argument("input", metavar='SOURCE', help="the fcpxml file that you want to remove elements from")
parser.add_argument("output", metavar='DESTINATION', help="the name of the fcpxml file to create")
parser.add_argument("element", metavar='ELEMENT', help="the type of element to remove")
parser.add_argument("-v", "--verbose", help="print verbose logging information", action="store_true")
parser.add_argument("-d", "--debug", help="print debug logging information", action="store_true")
args = parser.parse_args()
#Turn on logging if specified
if args.debug:
logging.getLogger().setLevel("DEBUG")
elif args.verbose:
logging.getLogger().setLevel("INFO")
#Check that input file exists
if not os.path.isfile(args.input):
print(os.path.basename(__file__) + ": error: " + args.input + " does not exist")
sys.exit(2)
#Parse fcpxml and catch any errors
try:
xmldom = parse(args.input)
except:
print(os.path.basename(__file__) + ": error: " + args.input + " cannot be parsed")
sys.exit(2)
#Check if it is really an fcpxml
if not xmldom.getElementsByTagName("fcpxml"):
print(os.path.basename(__file__) + ": error: " + args.input + " xml is not an fcpxml")
sys.exit(2)
#Check if destination folder exists
if not os.path.exists(os.path.dirname(args.output)):
print(os.path.basename(__file__) + ": error: " + os.path.dirname(args.output) + " does not exist")
sys.exit(2)
#Loop through and remove each filter-video element
element_list = xmldom.getElementsByTagName(args.element)
logging.info("Removing " + str(len(element_list)) + " elements")
for element in element_list:
element.parentNode.removeChild(element)
#Write out new fcpxml file
with codecs.open(args.output, "w", "utf-8") as new_xml:
xmldom.writexml(new_xml)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment