dom/bindings/ExampleGen.py
author Kartikaya Gupta <kgupta@mozilla.com>
Fri, 04 Jan 2013 16:32:58 -0500
changeset 117678 5a7b468178a28e1bd2b761f8781414f9b7c0c940
parent 114715 4786984b7d461fdbe936fa65a2bb9416983ec938
child 131407 59c279d93f8e268449e5153b1330e9d254ea4f8a
permissions -rw-r--r--
Bug 826381 - Update more tests to go with cset 11f420dd6b47. r=fix-test

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import cPickle
from Configuration import Configuration
from Codegen import CGExampleRoot, replaceFileIfChanged

def generate_interface_example(config, interfaceName):
    """
    |config| Is the configuration object.
    |interfaceName| is the name of the interface we're generating an example for.
    """

    root = CGExampleRoot(config, interfaceName)
    exampleHeader = interfaceName + "-example.h"
    exampleImpl = interfaceName + "-example.cpp"
    replaceFileIfChanged(exampleHeader, root.declare())
    replaceFileIfChanged(exampleImpl, root.define())

def main():

    # Parse arguments.
    from optparse import OptionParser
    usagestring = "usage: %prog configFile interfaceName"
    o = OptionParser(usage=usagestring)
    o.add_option("--verbose-errors", action='store_true', default=False,
                 help="When an error happens, display the Python traceback.")
    (options, args) = o.parse_args()

    if len(args) != 2:
        o.error(usagestring)
    configFile = os.path.normpath(args[0])
    interfaceName = args[1]

    # Load the parsing results
    f = open('ParserResults.pkl', 'rb')
    parserData = cPickle.load(f)
    f.close()

    # Create the configuration data.
    config = Configuration(configFile, parserData)

    # Generate the example class.
    generate_interface_example(config, interfaceName)

if __name__ == '__main__':
    main()