#
# Converts a Playlist file (.pls) to a Brasero Data CD Project created
# for ease of burning songbird playlists.
#
# author: netpro25@gmail.com
# v 1.0

import sys
import fileinput
import re

print len(sys.argv)

if len(sys.argv) == 3:
  file = sys.argv[1]
  output = sys.argv[2]
  print "Brasero project file saved as:", output
else:
  print "Error, please specify input and output files"
  sys.exit(0)

xmlStart = '<?xml version="1.0" encoding="UTF8"?>\n<braseroproject>\n<version>0.2</version>\n<track>\n<data>\n'
xmlEnd = '</data>\n</track>\n</braseroproject>'

# Open XML file for writing
fout = open(output, "w")
fout.write(xmlStart)

for line in fileinput.input(file):
  # Find the pattern for the file location  
  pattern = re.compile('File[0-9]+')    
  if pattern.match(line):
    # Get the file name by itself
    uri = line.split("/")
    fname = uri[len(uri)-1].rstrip()
    
    # Replace File[0-9] with file
    patttern = re.compile('File[0-9]+=/')
    line = patttern.sub('file:///', line.rstrip())
    fout.write("<graft>\n")
    fout.write("<path>/" + fname + "</path>\n")
    fout.write("<uri>" + line + "</uri>\n")
    fout.write("</graft>\n")

fout.write(xmlEnd)
fout.close()
