#!/usr/bin/env python
#
# Convert from CMML (as produced by CMMLbot) to a plaintext irc log.
#
# Copyright (C) 2005 CSIRO Australia
#
# Based on sample code from the Python/XML HOWTO:
# http://pyxml.sourceforge.net/topics/howto/xml-howto.html
#
# You may find this script to be a useful example of processing CMML in
# python. Improvements and bugfixes welcome :-)
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import sys
from time import strptime, strftime

from xml.sax import saxutils
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces

def clock2timestamp(str):
  t = strptime(str, "clock:%Y%m%dT%H%M%SZ")
  return strftime ("%H:%M:%S", t)

class cmmlHandler(saxutils.DefaultHandler):
  def __init__(self):
    self.inDesc = False
    self.text = ""

  def startElement(self, name, attrs):
    if name == 'clip':
      self.speaker = ""
      self.inDesc = False
      self.text = ""
      self.start_time = clock2timestamp(attrs.get('start', None))

    if name == 'meta':
      if (attrs.get('name') == 'DC.Contributor'):
        self.speaker = attrs.get('content')

    if name == 'desc':
      self.inDesc = True

  def characters(self, ch):
    if self.inDesc:
      self.text = self.text + ch

  def endElement(self, name):
    if name == 'clip':
      print '[%s] %s: %s' % (self.start_time, self.speaker, self.text)

    if name == 'desc':
      self.inDesc = False

if __name__=='__main__':
  # Create a parser
  parser = make_parser()

  # Tell the parser we are not interested in XML namespaces
  parser.setFeature(feature_namespaces, 0)

  # Create the handler
  dh = cmmlHandler()

  # Tell the parser to use the handler
  parser.setContentHandler(dh)

  # Parse the input
  if (len(sys.argv) < 2):
    parser.parse(sys.stdin)
  else:
    parser.parse("file://" + sys.argv[1])
