#!/usr/bin/python

import proto_parser as p
from mako.template import Template
import sys
import os, os.path
from optparse import OptionParser

parser = OptionParser("%s <protodef> <template> <output>" % sys.argv[0])
parser.add_option(
    "-p", "--package",
    default = "Messages",
    dest    = "pkg"
)


opts, args = parser.parse_args()

if len(args) != 3:
    parser.error("invalid argument count")
else:
    protof   = args[0]
    module   = os.path.basename(protof).split(".")[0]
    data     = file(protof).read()
    template = Template(filename = args[1])
    outfile  = file(args[2], "w+")
    
    s = template.render(
        messages = p.parse(data), 
        pkgName = opts.pkg,
        module = module,
    )
    outfile.write(s)
    outfile.close()
