| OLD | NEW |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 import os | 35 import os |
| 36 | 36 |
| 37 import blink_idl_parser | 37 import blink_idl_parser |
| 38 from blink_idl_parser import BlinkIDLParser | 38 from blink_idl_parser import BlinkIDLParser |
| 39 from idl_definitions import IdlDefinitions | 39 from idl_definitions import IdlDefinitions |
| 40 from idl_validator import EXTENDED_ATTRIBUTES_RELATIVE_PATH, IDLInvalidExtendedA
ttributeError, IDLExtendedAttributeValidator | 40 from idl_validator import EXTENDED_ATTRIBUTES_RELATIVE_PATH, IDLInvalidExtendedA
ttributeError, IDLExtendedAttributeValidator |
| 41 from interface_dependency_resolver import InterfaceDependencyResolver | 41 from interface_dependency_resolver import InterfaceDependencyResolver |
| 42 | 42 |
| 43 | 43 |
| 44 class IdlReader(object): | 44 class IdlReader(object): |
| 45 def __init__(self, interfaces_info=None, outputdir=''): | 45 def __init__(self, interfaces_info=None, outputdir='', multi_interface=False
): |
| 46 self.multi_interface = multi_interface |
| 46 self.extended_attribute_validator = IDLExtendedAttributeValidator() | 47 self.extended_attribute_validator = IDLExtendedAttributeValidator() |
| 47 | 48 |
| 48 if interfaces_info: | 49 if interfaces_info: |
| 49 self.interface_dependency_resolver = InterfaceDependencyResolver(int
erfaces_info, self) | 50 self.interface_dependency_resolver = InterfaceDependencyResolver(int
erfaces_info, self) |
| 50 else: | 51 else: |
| 51 self.interface_dependency_resolver = None | 52 self.interface_dependency_resolver = None |
| 52 | 53 |
| 53 self.parser = BlinkIDLParser(outputdir=outputdir) | 54 self.parser = BlinkIDLParser(outputdir=outputdir) |
| 54 | 55 |
| 55 def read_idl_definitions(self, idl_filename): | 56 def read_idl_definitions(self, idl_filename): |
| 56 """Returns an IdlDefinitions object for an IDL file, including all depen
dencies.""" | 57 """Returns an IdlDefinitions object for an IDL file, including all depen
dencies.""" |
| 57 definitions = self.read_idl_file(idl_filename) | 58 definitions = self.read_idl_file(idl_filename) |
| 58 if not self.interface_dependency_resolver: | 59 if not self.interface_dependency_resolver: |
| 59 return definitions | 60 return definitions |
| 60 self.interface_dependency_resolver.resolve_dependencies(definitions) | 61 self.interface_dependency_resolver.resolve_dependencies(definitions) |
| 61 return definitions | 62 return definitions |
| 62 | 63 |
| 63 def read_idl_file(self, idl_filename): | 64 def read_idl_file(self, idl_filename): |
| 64 """Returns an IdlDefinitions object for an IDL file, without any depende
ncies. | 65 """Returns an IdlDefinitions object for an IDL file, without any depende
ncies. |
| 65 | 66 |
| 66 The IdlDefinitions object is guaranteed to contain a single | 67 The IdlDefinitions object is guaranteed to contain a single |
| 67 IdlInterface; it may also contain other definitions, such as | 68 IdlInterface; it may also contain other definitions, such as |
| 68 callback functions and enumerations.""" | 69 callback functions and enumerations.""" |
| 69 ast = blink_idl_parser.parse_file(self.parser, idl_filename) | 70 ast = blink_idl_parser.parse_file(self.parser, idl_filename) |
| 70 if not ast: | 71 if not ast: |
| 71 raise Exception('Failed to parse %s' % idl_filename) | 72 raise Exception('Failed to parse %s' % idl_filename) |
| 72 definitions = IdlDefinitions(ast) | 73 definitions = IdlDefinitions(ast) |
| 73 | 74 |
| 74 # Validate file contents with filename convention | 75 if not self.multi_interface: |
| 75 # The Blink IDL filenaming convention is that the file | 76 # Validate file contents with filename convention |
| 76 # <interface_name>.idl MUST contain exactly 1 interface (or exception), | 77 # The Blink IDL filenaming convention is that the file |
| 77 # and the interface name must agree with the file's basename, | 78 # <interface_name>.idl MUST contain exactly 1 interface (or exceptio
n), |
| 78 # unless it is a partial interface. | 79 # and the interface name must agree with the file's basename, |
| 79 # (e.g., 'partial interface Foo' can be in FooBar.idl). | 80 # unless it is a partial interface. |
| 80 number_of_interfaces = len(definitions.interfaces) | 81 # (e.g., 'partial interface Foo' can be in FooBar.idl). |
| 81 if number_of_interfaces != 1: | 82 number_of_interfaces = len(definitions.interfaces) |
| 82 raise Exception( | 83 if number_of_interfaces != 1: |
| 83 'Expected exactly 1 interface in file {0}, but found {1}' | 84 raise Exception( |
| 84 .format(idl_filename, number_of_interfaces)) | 85 'Expected exactly 1 interface in file {0}, but found {1}' |
| 85 interface = next(definitions.interfaces.itervalues()) | 86 .format(idl_filename, number_of_interfaces)) |
| 86 idl_file_basename, _ = os.path.splitext(os.path.basename(idl_filename)) | 87 interface = next(definitions.interfaces.itervalues()) |
| 87 if not interface.is_partial and interface.name != idl_file_basename: | 88 idl_file_basename, _ = os.path.splitext(os.path.basename(idl_filenam
e)) |
| 88 raise Exception( | 89 if not interface.is_partial and interface.name != idl_file_basename: |
| 89 'Interface name "{0}" disagrees with IDL file basename "{1}".' | 90 raise Exception( |
| 90 .format(interface.name, idl_file_basename)) | 91 'Interface name "{0}" disagrees with IDL file basename "{1}"
.' |
| 92 .format(interface.name, idl_file_basename)) |
| 93 else: |
| 94 if len(definitions.interfaces) > 1: |
| 95 print '----- Supplemental interfaces %s' % len(definitions.inter
faces) |
| 91 | 96 |
| 92 # Validate extended attributes | 97 # Validate extended attributes |
| 93 if not self.extended_attribute_validator: | 98 if not self.extended_attribute_validator: |
| 94 return definitions | 99 return definitions |
| 95 | 100 |
| 96 try: | 101 try: |
| 97 self.extended_attribute_validator.validate_extended_attributes(defin
itions) | 102 self.extended_attribute_validator.validate_extended_attributes(defin
itions) |
| 98 except IDLInvalidExtendedAttributeError as error: | 103 except IDLInvalidExtendedAttributeError as error: |
| 99 raise IDLInvalidExtendedAttributeError(""" | 104 raise IDLInvalidExtendedAttributeError(""" |
| 100 IDL ATTRIBUTE ERROR in file: | 105 IDL ATTRIBUTE ERROR in file: |
| 101 %s: | 106 %s: |
| 102 %s | 107 %s |
| 103 If you want to add a new IDL extended attribute, please add it to: | 108 If you want to add a new IDL extended attribute, please add it to: |
| 104 %s | 109 %s |
| 105 and add an explanation to the Blink IDL documentation at: | 110 and add an explanation to the Blink IDL documentation at: |
| 106 http://www.chromium.org/blink/webidl/blink-idl-extended-attributes | 111 http://www.chromium.org/blink/webidl/blink-idl-extended-attributes |
| 107 """ % (idl_filename, str(error), EXTENDED_ATTRIBUTES_RELATIVE_PATH)) | 112 """ % (idl_filename, str(error), EXTENDED_ATTRIBUTES_RELATIVE_PATH)) |
| 108 | 113 |
| 109 return definitions | 114 return definitions |
| OLD | NEW |