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 """Validate extended attributes.""" |
| 30 |
| 31 |
| 32 import re |
| 33 |
| 34 |
| 35 class IDLExtendedAttributeFileFormatError(Exception): |
| 36 pass |
| 37 |
| 38 |
| 39 class IDLInvalidExtendedAttributeError(Exception): |
| 40 pass |
| 41 |
| 42 |
| 43 def validate_extended_attributes(definitions, basename, idl_attributes_filename)
: |
| 44 extended_attribute_validator = IDLExtendedAttributeValidator(idl_attributes_
filename) |
| 45 extended_attribute_validator.validate_extended_attributes(definitions, basen
ame) |
| 46 |
| 47 |
| 48 def read_extended_attributes_file(extended_attributes_filename): |
| 49 valid_extended_attributes = {} |
| 50 with open(extended_attributes_filename) as extended_attributes_file: |
| 51 for line in extended_attributes_file: |
| 52 line = line.strip() |
| 53 if not line or line.startswith('#'): |
| 54 continue |
| 55 name, _, values_string = map(str.strip, line.partition('=')) |
| 56 if not name: |
| 57 raise IDLExtendedAttributeFileFormatError('The format of %s is w
rong, in line "%s"' % (extended_attributes_filename, line)) |
| 58 valid_extended_attributes[name] = set() |
| 59 value_list = values_string.split('|') |
| 60 if not value_list: |
| 61 valid_extended_attributes[name].add(None) |
| 62 continue |
| 63 for value in value_list: |
| 64 value = value.strip() |
| 65 if not value: |
| 66 value = None |
| 67 valid_extended_attributes[name].add(value) |
| 68 return valid_extended_attributes |
| 69 |
| 70 |
| 71 class IDLExtendedAttributeValidator: |
| 72 def __init__(self, extended_attributes_filename): |
| 73 self.valid_extended_attributes = read_extended_attributes_file(extended_
attributes_filename) |
| 74 |
| 75 def validate_extended_attributes(self, definitions, idl_filename): |
| 76 # FIXME: this should be done when parsing the file, rather than after. |
| 77 try: |
| 78 for interface in definitions.interfaces.itervalues(): |
| 79 self.validate_extended_attributes_node(interface) |
| 80 for attribute in interface.attributes: |
| 81 self.validate_extended_attributes_node(attribute) |
| 82 for operation in interface.operations: |
| 83 self.validate_extended_attributes_node(operation) |
| 84 for argument in operation.arguments: |
| 85 self.validate_extended_attributes_node(argument) |
| 86 except IDLInvalidExtendedAttributeError, error: |
| 87 raise IDLInvalidExtendedAttributeError("""IDL ATTRIBUTE ERROR in fil
e %s: |
| 88 %s |
| 89 If you want to add a new IDL extended attribute, please add it to bindings/scrip
ts/IDLAttributes.txt and add an explanation to the Blink IDL document at http://
chromium.org/blink/webidl |
| 90 """ % (idl_filename, str(error))) |
| 91 |
| 92 def validate_extended_attributes_node(self, node): |
| 93 for name, values_string in node.extended_attributes.iteritems(): |
| 94 self.validate_name_values_string(name, values_string) |
| 95 |
| 96 def validate_name_values_string(self, name, values_string): |
| 97 if name == 'ImplementedBy': # attribute added when merging interfaces |
| 98 return |
| 99 if name not in self.valid_extended_attributes: |
| 100 raise IDLInvalidExtendedAttributeError('Unknown extended attribute [
%s]' % name) |
| 101 valid_values = self.valid_extended_attributes[name] |
| 102 if '*' in valid_values: # wildcard, any value ok |
| 103 return |
| 104 if values_string is None: |
| 105 value_list = [None] |
| 106 else: |
| 107 value_list = re.split('[|&]', values_string) |
| 108 for value in value_list: |
| 109 if value not in valid_values: |
| 110 raise IDLInvalidExtendedAttributeError('Invalid value "%s" found
in extended attribute [%s=%s]' % (value, name, values_string)) |
OLD | NEW |