#!/usr/bin/env python
#
# $Source: /home/blais/repos/cvsroot/arubomu/bin/arubomu-convert-list,v $
# $Id: arubomu-convert-list,v 1.2 2004/02/16 01:02:57 blais Exp $
#

"""arubomu-convert-list [<options>] <file> [<file> ...]

Album-list reader.
Read a list of album information from a single file.

"""

__author__ = "Martin Blais <blais@furius.ca>"
__version__ = "$Id: arubomu-convert-list,v 1.2 2004/02/16 01:02:57 blais Exp $"


import sys, os

from elementtree.ElementTree import Element, ElementTree
from elementtree_pretty import write_pretty

from arubomu import album



xmlindent = os.environ.get('XMLLINT_INDENT', '   ')


def main():
    import optparse
    parser = optparse.OptionParser(__doc__)
    parser.add_option('-o', '--output', action="store",
                      help='Store all outputs in the given filename')
    opts, args = parser.parse_args()

    if not args:
        raise parser.error('you need to specify album-list files to read.')

    if opts.output:
        outf = open(opts.output, 'w')
    else:
        outf = sys.stdout

    # for each entry requested, get appropriate fetcher and parse url contents.
    albumlist = []
    for fn in args:
        l = album.load_list(open(fn, 'r'))
        if l:
            albumlist.extend(l)

    pel = Element('album-list')
    for a in albumlist:
        pel.append(a.toxml(loose=True))

    tree = ElementTree(pel)
    write_pretty(tree, outf, encoding='ISO-8859-1', indent=xmlindent)
    print

# Run main if loaded as a script
if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print >> sys.stderr, '\nInterrupted.'
