from __future__ import with_statement
import sys
import wx
import os
import re

#
# Reads the input src and writes to the output dst
#
def PLSToASXConverter( src, dst ):
	# source and destination is the same, just ignore
	if src == dst:
		return

	# read pls
	lines = []
	with open( src, "r" ) as f:
		lines = f.readlines()

	# parse lines	
	if lines != None:
		# open the destination for writing
		with open( dst, "w" ) as f:
			for line in lines:
				# search for the URI
				match = re.match( "^[ \\t]*file[0-9]*[ \\t]*=[ \\t]*(.*$)", line, flags=re.IGNORECASE )
				if match != None:
					if match.group( 1 ) != None:
						# URI found, it's saved in the second match group
						# output the URI to the destination file
						print "Found '%s'" % match.group( 1 )
						f.write( match.group( 1 ) )
						f.write( "\n" )
		print "Done writing to '%s'" % dst
	else:
		print "Source file '%s' is empty or does not exist, nothing to do..." % src

#
# wxWidgets application frame
# implements the application window frame
#
class AppFrame(wx.Frame):
	def __init__(self, parent, ID, title, pos=wx.DefaultPosition,
		size=(250, 75), style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU |wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN ):
		wx.Frame.__init__(self, parent, ID, title, pos, size, style)

		# bind the windows close event
		wx.EVT_CLOSE(self, self.OnCloseWindow)

		# create a button, it's the only
		# item in the frame so it will
		# automatically stretch
		button = wx.Button(self, 1004, "Load Playlist File (*.pls)")
		# bind to the button press event
		wx.EVT_BUTTON(self, 1004, self.OnPressMe)

	# on button press
	def OnPressMe(self, event):
		# open a file dialog so user can locate the pls file
		srcDlg = wx.FileDialog( self, "Locate pls file...", "", "", "pls files (*.pls)|*.pls|All files|*", wx.FD_OPEN )

		# user located a valid file
		if srcDlg.ShowModal() == wx.ID_OK:
			# get path to the source file
			srcPath = os.path.join( srcDlg.GetDirectory(), srcDlg.GetFilename() )
			print "src: '%s'" % srcPath
		
			# specify where and what name to use for output
			dstDlg = wx.FileDialog( self, "Save asx file...", "", "", "asx files (*.asx)|*.asx|All files|*", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT )

			# user specified an output file
			if dstDlg.ShowModal() == wx.ID_OK:
				# get path to the dest file
				dstPath = os.path.join( dstDlg.GetDirectory(), dstDlg.GetFilename() )
				print "dst: '%s'" % dstPath
				# actually write to output file given the input file
				PLSToASXConverter( srcPath, dstPath )

	# closing window
	def OnCloseWindow(self, event):
		# destroy window
		self.Destroy()

#
# The wxWidgets application
#
class MyApp(wx.App):
	def OnInit(self):
		# create a frame for this application
		frame = AppFrame(None, -1, "PLS to ASX Converter")
		# tell the frame to show
		frame.Show(True)
		# bring application window to front
		self.SetTopWindow(frame)
		return True

if __name__ == "__main__":
	# create the application
	app = MyApp(0)
	# let wxWidgets take over the main loop
	app.MainLoop()

