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 Design doc:
     32 http://www.chromium.org/developers/design-documents/idl-compiler#TOC-Front-end
     33 """
     34 
     35 import os
     36 
     37 import blink_idl_parser
     38 from blink_idl_parser import BlinkIDLParser
     39 from idl_definitions import IdlDefinitions
     40 from idl_validator import EXTENDED_ATTRIBUTES_RELATIVE_PATH, IDLInvalidExtendedAttributeError, IDLExtendedAttributeValidator
     41 from interface_dependency_resolver import InterfaceDependencyResolver
     42 from utilities import idl_filename_to_component
     43 
     44 
     45 class IdlReader(object):
     46     def __init__(self, interfaces_info=None, outputdir=''):
     47         self.extended_attribute_validator = IDLExtendedAttributeValidator()
     48         self.interfaces_info = interfaces_info
     49 
     50         if interfaces_info:
     51             self.interface_dependency_resolver = InterfaceDependencyResolver(interfaces_info, self)
     52         else:
     53             self.interface_dependency_resolver = None
     54 
     55         self.parser = BlinkIDLParser(outputdir=outputdir)
     56 
     57     def read_idl_definitions(self, idl_filename):
     58         """Returns a dictionary whose key is component and value is an IdlDefinitions object for an IDL file, including all dependencies."""
     59         definitions = self.read_idl_file(idl_filename)
     60         component = idl_filename_to_component(idl_filename)
     61 
     62         if not self.interface_dependency_resolver:
     63             return {component: definitions}
     64 
     65         # This definitions should have a dictionary. No need to resolve any
     66         # dependencies.
     67         if not definitions.interfaces:
     68             return {component: definitions}
     69 
     70         return self.interface_dependency_resolver.resolve_dependencies(definitions, component)
     71 
     72     def read_idl_file(self, idl_filename):
     73         """Returns an IdlDefinitions object for an IDL file, without any dependencies.
     74 
     75         The IdlDefinitions object is guaranteed to contain a single
     76         IdlInterface; it may also contain other definitions, such as
     77         callback functions and enumerations."""
     78         ast = blink_idl_parser.parse_file(self.parser, idl_filename)
     79         if not ast:
     80             raise Exception('Failed to parse %s' % idl_filename)
     81         idl_file_basename, _ = os.path.splitext(os.path.basename(idl_filename))
     82         definitions = IdlDefinitions(idl_file_basename, ast)
     83 
     84         # Validate file contents with filename convention
     85         # The Blink IDL filenaming convention is that the file
     86         # <definition_name>.idl MUST contain exactly 1 definition
     87         # (interface, dictionary or exception), and the definition name must
     88         # agree with the file's basename, unless it is a partial definition.
     89         # (e.g., 'partial interface Foo' can be in FooBar.idl).
     90         targets = (definitions.interfaces.values() +
     91                    definitions.dictionaries.values())
     92         number_of_targets = len(targets)
     93         if number_of_targets != 1:
     94             raise Exception(
     95                 'Expected exactly 1 definition in file {0}, but found {1}'
     96                 .format(idl_filename, number_of_targets))
     97         target = targets[0]
     98         if not target.is_partial and target.name != idl_file_basename:
     99             raise Exception(
    100                 'Definition name "{0}" disagrees with IDL file basename "{1}".'
    101                 .format(target.name, idl_file_basename))
    102 
    103         # Validate extended attributes
    104         if not self.extended_attribute_validator:
    105             return definitions
    106 
    107         try:
    108             self.extended_attribute_validator.validate_extended_attributes(definitions)
    109         except IDLInvalidExtendedAttributeError as error:
    110             raise IDLInvalidExtendedAttributeError("""
    111 IDL ATTRIBUTE ERROR in file:
    112 %s:
    113     %s
    114 If you want to add a new IDL extended attribute, please add it to:
    115     %s
    116 and add an explanation to the Blink IDL documentation at:
    117     http://www.chromium.org/blink/webidl/blink-idl-extended-attributes
    118     """ % (idl_filename, str(error), EXTENDED_ATTRIBUTES_RELATIVE_PATH))
    119 
    120         return definitions
    121