#!/usr/bin/env python2.5
#      -*-mode: python -*-    -*- coding: utf-8 -*-

"""

Command line interface to scribe tool.   Should provide more-or-less
the same functionality as the web interface....


"""
__version__="$Id: scribe.py,v 1.2 2009-03-03 06:48:49 sandro Exp $"

import urllib2
from optparse import OptionParser

import debugtools 
from debugtools import debug

import meeting
import chatevents
import html as h

def generate_page(source):
    """
    Has lots of code in common with the web-server version, but ... eh.
    """

    m = meeting.Meeting()
    m.useWikiSourceURL(source)
    count = 0

    d = h.Document()
    d.head << h.stylelink("http://www.w3.org/2008/06/scribe.css")

    try:
        for c in chatevents.fromWikiRRSAgent(source):
            count += 1
            m.addChatEvent(c)
    except urllib2.URLError:
        d = h.Document()
        d << h.h2("Error, Can't Open URL")
        d << h.p("Failed on %s" % `source`)
        print d
        return

    d << h.h1("Meeting Records Preview")
    m.toHTML(d)
    print d

def run():
    parser = OptionParser(usage="%prog [options] input-location",
                          version=__version__)
    parser.set_defaults(verbose=True)
    parser.set_defaults(output="-")
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose", 
                      help="don't print status messages to stderr")
    parser.add_option("-D", "--debug",
                      action="append", dest="debugTags", 
                      help="turn on debugging of a particular type (try 'all')")
    parser.add_option("-O", "--output", action="store", dest="output",
                      help="Save the output to this file (else stdout)")
                      
    (options, args) = parser.parse_args()

    if options.debugTags:
        debugtools.tags.update(options.debugTags)
    verbose = options.verbose

    debug('cmdline', 'args:', args)
    
    if len(args) != 1:
        parser.print_help()
        sys.exit(1)

    source_address = args[0]
    generate_page(source_address)


if __name__ == "__main__":
    import doctest, sys
    doctest.testmod(sys.modules[__name__])

    run()
