OLD | NEW |
(Empty) | |
| 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 |
| 30 """ |
| 31 FIXME: This module should be removed. All IDL file reading should be done in the |
| 32 Parser; the Code Generator should not have to do this. |
| 33 """ |
| 34 |
| 35 import fnmatch |
| 36 import os |
| 37 import posixpath |
| 38 |
| 39 import idl_reader |
| 40 |
| 41 reader = idl_reader.IdlReader() |
| 42 idl_filenames = {} |
| 43 |
| 44 |
| 45 def find_idl_files(idl_directories): |
| 46 directories = [os.path.abspath(directory) for directory in idl_directories] |
| 47 directories.append('.') |
| 48 for directory in directories: |
| 49 for dirpath, _, files in os.walk(directory): |
| 50 for filename in fnmatch.filter(files, '*.idl'): |
| 51 root, _ = os.path.splitext(filename) |
| 52 fullpath = os.path.join(dirpath, filename) |
| 53 idl_filenames[root] = fullpath |
| 54 |
| 55 |
| 56 def idl_filename_for_interface(root_name): |
| 57 try: |
| 58 return idl_filenames[root_name] |
| 59 except KeyError: |
| 60 # FIXME: This error message should be improved. |
| 61 # This error occurs during symbol resolution: an unidentified type |
| 62 # is assumed to be an interface type. |
| 63 raise Exception('Could NOT find IDL file for interface \'%s\'' % root_na
me) |
| 64 |
| 65 |
| 66 def memoize(f): |
| 67 """Return a memoized version of a single-variable function. |
| 68 |
| 69 Useful as a decorator. |
| 70 """ |
| 71 cache = {} |
| 72 |
| 73 def memoized_function(x): |
| 74 if x not in cache: |
| 75 cache[x] = f(x) |
| 76 return cache[x] |
| 77 return memoized_function |
| 78 |
| 79 |
| 80 @memoize |
| 81 def _get_interface(root_name): |
| 82 """Return an interface or partial interface, given a root name. |
| 83 |
| 84 Memoized so only parses interfaces once. |
| 85 |
| 86 Args: |
| 87 root_name: the IDL filename minus the extension: Foo for Foo.idl. |
| 88 This is either the interface name or the root name of a |
| 89 partial interface file. |
| 90 """ |
| 91 idl_filename = idl_filename_for_interface(root_name) |
| 92 definitions = reader.read_idl_definitions(idl_filename) |
| 93 interfaces = definitions.interfaces |
| 94 if root_name in interfaces: |
| 95 return interfaces[root_name] |
| 96 if len(interfaces) == 1: |
| 97 interface = interfaces.values()[0] |
| 98 if interface.is_partial: |
| 99 return interface |
| 100 |
| 101 raise Exception('Could NOT find interface definition for %s in %s' % (root_n
ame, idl_filename)) |
| 102 |
| 103 |
| 104 def ancestors(interface): |
| 105 yield interface |
| 106 while interface.parent: |
| 107 interface = _get_interface(interface.parent) |
| 108 yield interface |
| 109 |
| 110 |
| 111 def base_interface(interface): |
| 112 return list(ancestors(interface))[-1] |
| 113 |
| 114 |
| 115 def base_interface_name(interface): |
| 116 return base_interface(interface).name |
| 117 |
| 118 |
| 119 def inherits_interface(interface, interface_name): |
| 120 ancestor_names = [ancestor_interface.name for ancestor_interface in ancestor
s(interface)] |
| 121 return interface_name in ancestor_names |
| 122 |
| 123 |
| 124 def interface_inherits_extended_attribute(interface, extended_attribute): |
| 125 ancestor_extended_attributes = [ancestor_interface.extended_attributes for a
ncestor_interface in ancestors(interface)] |
| 126 return any([extended_attribute in extended_attributes for extended_attribute
s in ancestor_extended_attributes]) |
| 127 |
| 128 |
| 129 def is_callback_interface_base(interface_name): |
| 130 interface = _get_interface(interface_name) |
| 131 return interface.is_callback |
| 132 |
| 133 |
| 134 def implemented_as_from_implemented_by(implemented_by): |
| 135 interface = _get_interface(implemented_by) |
| 136 return interface.extended_attributes.get('ImplementedAs', implemented_by) |
| 137 |
| 138 |
| 139 def _implemented_as_cpp_name(definition_or_member): |
| 140 """ |
| 141 For interfaces, this is the class name. |
| 142 For interface members (attributes and operations), this is the method name. |
| 143 For attributes, this is also used in the getter and setter. |
| 144 """ |
| 145 # FIXME: duplicated from types |
| 146 return definition_or_member.extended_attributes.get('ImplementedAs', definit
ion_or_member.name) |
| 147 |
| 148 |
| 149 def implemented_as_cpp_name_from_idl_type(idl_type): |
| 150 interface = _get_interface(idl_type) |
| 151 return _implemented_as_cpp_name(interface) |
| 152 |
| 153 |
| 154 def _get_relative_dir_posix(filename): |
| 155 """Returns relative directory to a local file, in POSIX format.""" |
| 156 relative_path_local = os.path.relpath(filename) |
| 157 relative_dir_local = os.path.dirname(relative_path_local) |
| 158 return relative_dir_local.replace(os.path.sep, posixpath.sep) |
| 159 |
| 160 |
| 161 def idl_file_rel_dir_posix(interface_name): |
| 162 idl_filename = idl_filename_for_interface(interface_name) |
| 163 return _get_relative_dir_posix(idl_filename) |
OLD | NEW |