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

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

Converts a CD-index entry to an XML file.  Reads input from files or stdin.

"""

__version__ = "$Revision: 1.12 $"
__author__ = "Martin Blais <blais@furius.ca>"


import sys

from arubomu import cdindex, common

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


def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip(), version=__version__)
    parser.add_option('-I', '--input-encoding', action='store',
                      default=common.xml_encoding,
                      help="Specifies the file encoding of the input file.")
    parser.add_option('-O', '--output-encoding', action='store',
                      default=common.xml_encoding,
                      help="Specifies the file encoding of the output file.")
    opts, args = parser.parse_args()

    if not args:
        args = [None] # stdin

    # read input cdindex files
    allalbs = []
    for fn in args:
        if fn == None:
            f = sys.stdin
        else:
            try:
                f = open(fn, 'r')
            except IOError, e:
                raise SystemExit("Error: opening file (%s)" % str(e))

        albs = cdindex.load(f)
        allalbs.extend(albs)

    # output to XML
    pel = Element('album-list')
    for a in allalbs:
        pel.append(a.toxml(loose=True))

    tree = ElementTree(pel)
    write_pretty(tree, sys.stdout, encoding=opts.output_encoding)
    print

if __name__ == '__main__':
    main()
