Home | History | Annotate | Download | only in scripts
      1 # Copyright (C) 2013 Google Inc. All rights reserved.
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions are
      5 # met:
      6 #
      7 #     * Redistributions of source code must retain the above copyright
      8 # notice, this list of conditions and the following disclaimer.
      9 #     * Redistributions in binary form must reproduce the above
     10 # copyright notice, this list of conditions and the following disclaimer
     11 # in the documentation and/or other materials provided with the
     12 # distribution.
     13 #     * Neither the name of Google Inc. nor the names of its
     14 # contributors may be used to endorse or promote products derived from
     15 # this software without specific prior written permission.
     16 #
     17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 """Read an IDL file or complete IDL interface, producing an IdlDefinitions object."""
     30 
     31 import os.path
     32 
     33 import blink_idl_parser
     34 import idl_definitions_builder
     35 import idl_validator
     36 import interface_dependency_resolver
     37 
     38 
     39 class IdlReader:
     40     def __init__(self, interface_dependencies_filename=None, additional_idl_filenames=None, idl_attributes_filename=None, outputdir='', verbose=False):
     41         if idl_attributes_filename:
     42             self.extended_attribute_validator = idl_validator.IDLExtendedAttributeValidator(idl_attributes_filename)
     43         else:
     44             self.extended_attribute_validator = None
     45 
     46         if interface_dependencies_filename:
     47             self.interface_dependency_resolver = interface_dependency_resolver.InterfaceDependencyResolver(interface_dependencies_filename, additional_idl_filenames, self)
     48         else:
     49             self.interface_dependency_resolver = None
     50 
     51         self.parser = blink_idl_parser.BlinkIDLParser(outputdir=outputdir, verbose=verbose)
     52 
     53     def read_idl_definitions(self, idl_filename):
     54         """Returns an IdlDefinitions object for an IDL file, including all dependencies."""
     55         basename = os.path.basename(idl_filename)
     56         interface_name, _ = os.path.splitext(basename)
     57         definitions = self.read_idl_file(idl_filename)
     58         if self.interface_dependency_resolver:
     59             should_generate_bindings = self.interface_dependency_resolver.resolve_dependencies(definitions, interface_name)
     60             if not should_generate_bindings:
     61                 return None
     62         return definitions
     63 
     64     def read_idl_file(self, idl_filename):
     65         """Returns an IdlDefinitions object for an IDL file, without any dependencies."""
     66         ast = blink_idl_parser.parse_file(self.parser, idl_filename)
     67         definitions = idl_definitions_builder.build_idl_definitions_from_ast(ast)
     68         if self.extended_attribute_validator:
     69             try:
     70                 self.extended_attribute_validator.validate_extended_attributes(definitions)
     71             except idl_validator.IDLInvalidExtendedAttributeError, error:
     72                 raise idl_validator.IDLInvalidExtendedAttributeError("""IDL ATTRIBUTE ERROR in file %s:
     73     %s
     74     If you want to add a new IDL extended attribute, please add it to bindings/scripts/IDLAttributes.txt and add an explanation to the Blink IDL document at http://chromium.org/blink/webidl
     75     """ % (idl_filename, str(error)))
     76 
     77         return definitions
     78