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 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler#TOC
-Extended-attribute-validation |
| 32 """ |
| 33 |
| 34 |
| 35 import os.path |
| 36 import re |
| 37 |
| 38 module_path = os.path.dirname(__file__) |
| 39 source_path = os.path.join(module_path, os.pardir, os.pardir) |
| 40 EXTENDED_ATTRIBUTES_RELATIVE_PATH = os.path.join('bindings', |
| 41 'IDLExtendedAttributes.txt') |
| 42 EXTENDED_ATTRIBUTES_FILENAME = os.path.join(source_path, |
| 43 EXTENDED_ATTRIBUTES_RELATIVE_PATH) |
| 44 |
| 45 class IDLInvalidExtendedAttributeError(Exception): |
| 46 pass |
| 47 |
| 48 |
| 49 class IDLExtendedAttributeValidator(object): |
| 50 def __init__(self): |
| 51 self.valid_extended_attributes = read_extended_attributes_file() |
| 52 |
| 53 def validate_extended_attributes(self, definitions): |
| 54 # FIXME: this should be done when parsing the file, rather than after. |
| 55 for interface in definitions.interfaces.itervalues(): |
| 56 self.validate_extended_attributes_node(interface) |
| 57 for attribute in interface.attributes: |
| 58 self.validate_extended_attributes_node(attribute) |
| 59 for operation in interface.operations: |
| 60 self.validate_extended_attributes_node(operation) |
| 61 for argument in operation.arguments: |
| 62 self.validate_extended_attributes_node(argument) |
| 63 |
| 64 def validate_extended_attributes_node(self, node): |
| 65 for name, values_string in node.extended_attributes.iteritems(): |
| 66 self.validate_name_values_string(name, values_string) |
| 67 |
| 68 def validate_name_values_string(self, name, values_string): |
| 69 if name not in self.valid_extended_attributes: |
| 70 raise IDLInvalidExtendedAttributeError( |
| 71 'Unknown extended attribute [%s]' % name) |
| 72 valid_values = self.valid_extended_attributes[name] |
| 73 if values_string is None and None not in valid_values: |
| 74 raise IDLInvalidExtendedAttributeError( |
| 75 'Missing required argument for extended attribute [%s]' % name) |
| 76 if '*' in valid_values: # wildcard, any (non-empty) value ok |
| 77 return |
| 78 if values_string is None: |
| 79 values = set([None]) |
| 80 else: |
| 81 values = set(re.split('[|&]', values_string)) |
| 82 invalid_values = values - valid_values |
| 83 if invalid_values: |
| 84 invalid_value = invalid_values.pop() |
| 85 raise IDLInvalidExtendedAttributeError( |
| 86 'Invalid value "%s" found in extended attribute [%s=%s]' % |
| 87 (invalid_value, name, values_string)) |
| 88 |
| 89 |
| 90 def read_extended_attributes_file(): |
| 91 def extended_attribute_name_values(): |
| 92 with open(EXTENDED_ATTRIBUTES_FILENAME) as extended_attributes_file: |
| 93 for line in extended_attributes_file: |
| 94 line = line.strip() |
| 95 if not line or line.startswith('#'): |
| 96 continue |
| 97 name, _, values_string = map(str.strip, line.partition('=')) |
| 98 value_list = [value.strip() for value in values_string.split('|'
)] |
| 99 yield name, value_list |
| 100 |
| 101 valid_extended_attributes = {} |
| 102 for name, value_list in extended_attribute_name_values(): |
| 103 if not value_list: |
| 104 valid_extended_attributes[name] = set([None]) |
| 105 continue |
| 106 valid_extended_attributes[name] = set([value if value else None |
| 107 for value in value_list]) |
| 108 return valid_extended_attributes |
OLD | NEW |