dom/bindings/ExampleGen.py
author Boris Zbarsky <bzbarsky@mit.edu>
Wed, 17 Oct 2012 17:01:55 -0400
changeset 110591 50d6355b80c44f79bd42b12af7e00b8614d0cb59
parent 101796 dom/bindings/BindingGen.py@532d31e06fb87b6a62239bb77155dba36d75adab
child 114715 4786984b7d461fdbe936fa65a2bb9416983ec938
permissions -rw-r--r--
Bug 796983 part 2. Add a way to generate an example class declaration for a given WebIDL interface. r=jst We mark constructors as static in the parser because they are. This allows us to just use the isStatic() for the IDLMember to mark our declarations static. To generate an example interface implementation, just "make interfacename-example" in $objdir/dom/bindings. This will place files called interfacename-example.h and interfacename-example.cpp in that directory. For example, "make XMLHttpRequest-example" will get you $objdir/dom/bindings/XMLHttpRequest-example.h and $objdir/dom/bindings/XMLHttpRequest-example.cpp. Attribute getters currently default to const methods, while setters and operations default to non-const methods.

# 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
import WebIDL
from Configuration import *
from Codegen import CGExampleRoot, replaceFileIfChanged
# import Codegen in general, so we can set a variable on it
import Codegen

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()