#!python
# Copyright (C) 2010 Samuel Abels.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
import sys
import os
import datetime
sys.path.insert(0, 'src')
from optparse import OptionParser
from Gelatin import __version__, generator
from Gelatin.generator import Builder
from Gelatin.util import compile

usage  = '''%prog [options] filename [filename ...]'''
parser = OptionParser(usage = usage, version = __version__)
parser.add_option('--syntax', '-s',
                  dest    = 'syntax',
                  metavar = 'FILE',
                  default = None,
                  help    = '''
The file containing the syntax for parsing the input
'''.strip())
parser.add_option('--format', '-f',
                  dest    = 'format',
                  default = 'xml',
                  metavar = 'FORMAT',
                  help    = '''
The output format. Valid values are: xml json yaml none. Default is xml.
'''.strip())
parser.add_option('--debug',
                  dest    = 'debug',
                  type    = 'int',
                  metavar = 'NUM',
                  default = 0,
                  help    = '''
Print debug info.
'''.strip())

if __name__ == '__main__':
    # Parse options.
    options, args = parser.parse_args(sys.argv)
    args.pop(0)

    if not options.syntax:
        parser.error('missing switch -s')
    if not os.path.exists(options.syntax):
        parser.error('no such file or directory: ' + options.syntax)
    if not os.path.isfile(options.syntax):
        parser.error('not a valid input file: ' + options.syntax)

    input_files = args
    if not input_files:
        parser.error('missing input file')

    serializer = generator.new(options.format)
    if serializer is None:
        parser.error('invalid output format: ' + options.format)

    def dbg(*msg):
        if options.debug:
            now = str(datetime.datetime.now())
            sys.stderr.write(now + ' ' + ' '.join(msg) + '\n')

    start = datetime.datetime.now()
    dbg("Compiling", options.syntax + '...')
    converter = compile(options.syntax)
    for input_file in input_files:
        dbg("Parsing", input_file + '...')
        builder = Builder()
        converter.parse(input_file, builder, debug=options.debug)
        if options.format != 'none':
            dbg("Generating output...")
            print(builder.serialize(serializer))
    dbg('Total: ' + str(datetime.datetime.now() - start))
