#! /usr/bin/python
#Copyright (C) 2006 Scott Shawcroft
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#Contact Scott at scotts4@u.washington.edu
#
#basic2cmml Converts a basic time format to cmml, prompting for more info.
#<start-time>(-<end-time>) <description>
#Times are 0:00 (m:ss) or 0:00:00 (h:mm:ss)

import sys
import datetime
today = datetime.date.today()

args = sys.argv
if args.count("--help"):
  print "basic2cmml, Copyright (C) 2006 Scott Shawcroft"
  print "Usage: basic2cmml.py input_file"
  print "Translates \"basic\" time data to CMML using user input."
  print "The \"basic\" format:"
  print "<start-time>(-<end-time>) <description>"
  print "Times are 00:00 (mm:ss) or 00:00:00 (hh:mm:ss)"
  print "\n"
  print "General options"
  print "  --help       Shows this help and exits."
  print ""
  print "Please report bugs to <scotts4@u.washington.edu>."
else:
  file_str = sys.argv[1]
  cmml_file = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n\n<cmml>\n\n'
  
  def ask(name, default=''):
    if default:
      var = raw_input(name + " (" + default+ "):")
      if var == '':
        var = default
      return var
    else:
      var = raw_input(name + ":")
      return var
      
  def close(cmml,output=file_str):
    action = ask("Action", "h")
    output = output.rsplit(".")[0] + ".cmml"
    if action == "h":
      print "Action options:"
      print "h - brings up this help"
      print "p - print cmml file"
      print "q - quit and do not save"
      print "s - save as and then quit"
      print "w - write file in place of input and quit"
      close(cmml, output)
    elif action == "p":
      print cmml
      close(cmml,output)
    elif action == "q":
      return
    elif action == "s":
      save_as = ask("Save as", output)
      output_file = open(save_as, "w")
      output_file.write(cmml)
      output_file.close()
      return
    elif action == "w":
      output_file = open(output, "w")
      output_file.write(cmml)
      output_file.close()
      return
    else:
      print "Unrecognized input: " + action
      close(cmml,output)
      
  #Construct Stream
  print "Stream info needed:"
  if (file_str.find('/') != -1):
    stream_id = ask("ID", file_str.rsplit("/",1)[1].rsplit(".",1)[0])
  else:
    stream_id = ask("ID", file_str.rsplit(".",1)[0])
  stream_type = ask("MimeType", "audio/x-vorbis")
  if (file_str.find('/') != -1):
    stream_src = ask("Source", file_str.rsplit("/",1)[1].rsplit(".",1)[0] + ".ogg")
  else:
    stream_src = ask("Source", file_str.rsplit(".",1)[0] + ".ogg")
  cmml_file += '<stream id="%s" timebase="npt:0">\n\t<import contenttype="%s" src="%s" start="npt:0">\n\t</import>\n</stream>\n\n' % (stream_id,stream_type,stream_src)
  
  #Construct Header
  print "\n\nHeader info needed:"
  cmml_file += '<head>\n'

  title = ask("Title")
  if title:
    cmml_file += '\t<title>%s</title>\n' % (title)
    cmml_file += '\t<meta name="DC.title" content="%s"/>\n' % (title)
   
  aud = ask("Audience")
  if aud:
    cmml_file += '\t<meta name="DC.audience" content="%s"/>\n' % (aud)

  author = ask("Author")
  if author:
    cmml_file += '\t<meta name="DC.author" content="%s"/>\n' % (author)
    
  desc = ask("Description")
  if desc:
    cmml_file += '\t<meta name="DESCRIPTION" content="%s"/>\n' % (desc)
   
  pub = ask("Published", str(today))
  cmml_file += '\t<meta name="DATE.PUBLISHED" content="%s"/>\n\t<meta name="DC.date" content="%s"/>\n' % (pub,pub)
  
  license = ask("License", "Creative Commons by v2.5")
  cmml_file += '\t<meta name="DC.license" content="%s"/>\n' % (license)
  
  license_url = ask("License URL", "http://creativecommons.org/licenses/by/2.5/")
  cmml_file += '\t<meta name="DC.rights" content="%s"/>\n' % (license_url)
  
  cont = ask("Contributor", author)
  cmml_file += '\t<meta name="DC.contributor" content="%s"/>\n' % (cont)
  
  creator = ask("Creator", author)
  cmml_file += '\t<meta name="DC.creator" content="%s"/>\n' % (creator)
  
  language = ask("Language", "en-US")
  cmml_file += '\t<meta name="DC.Language" content="%s"/>\n' % (language)
  
  cmml_file += '\t<meta name="DC.Format" content="text/x-cmml"/>\n'
  
  cmml_file += '</head>\n\n'
  
  standard_file = open(file_str) #File object.
  standard_file = standard_file.readlines() #File as an array of lines.
  count = str(len(standard_file))
  x=1
  for line in standard_file:
    #Strip newline characters.
    line.strip()
    #Initial split.
    time = line.split(" ",1)[0]
    desc = line.split(" ",1)[1]
    if time.find("-")!=-1:
      start_time = time.split("-")[0]
      end_time = time.split("-")[1]
    else:
      start_time = time
      end_time = None
    if end_time:
      print "\nClip %s of %s needs info:\nStart: %s\nEnd: %s\nDescription: %s" % (str(x),count,start_time,end_time,desc.strip())
    else:
      print "\nClip %s of %s needs info:\nStart: %s\nEnd: None\nDescription: %s" % (str(x),count,start_time,desc.strip())
    clip_id = ask("ID", "basic2cmml" + str(x))
    clip_title = ask("Title")
    if end_time:
      cmml_file += '<clip id="%s" title="%s" start="%s" end="%s">\n' % (clip_id,clip_title,start_time,end_time)
    else:
      cmml_file += '<clip id="%s" title="%s" start="%s">\n' % (clip_id,clip_title,start_time)
    clip_img = ask("IMG")
    if clip_img:
      cmml_file += '\t<img src="%s">\n' % (clip_img)
    clip_link_url = ask("Link URL")
    if clip_link_url:
      clip_link_text = ask("Link Text", clip_link_url)
      cmml_file += '\t<a href="%s">%s</a>\n' % (clip_link_url,clip_link_text)
    cmml_file += '\t<desc>%s</desc>\n' % (desc.strip())
    cmml_file += '</clip>\n\n'
    x += 1
  cmml_file += '</cmml>'
  print "Generation finished.  Please select an option below.  Use \"h\" for help."
  close(cmml_file)
