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 if idl_attributes_filename is None: | |
45 return | |
haraken
2013/07/17 02:44:49
You might want to output an error for this case.
Nils Barth (inactive)
2013/07/17 12:05:09
Fixed by refactoring.
This “return” is because the
| |
46 extended_attribute_validator = IDLExtendedAttributeValidator(idl_attributes_ filename) | |
47 extended_attribute_validator.validate_extended_attributes(definitions, basen ame) | |
48 | |
49 | |
50 def read_extended_attributes_file(extended_attributes_filename): | |
51 valid_extended_attributes = {} | |
52 with open(extended_attributes_filename) as extended_attributes_file: | |
53 for line in extended_attributes_file: | |
54 line = line.strip() | |
55 if not line or line.startswith('#'): | |
56 continue | |
57 name, _, values_string = map(str.strip, line.partition('=')) | |
58 if not name: | |
59 raise IDLExtendedAttributeFileFormatError('The format of %s is w rong, in line "%s"' % (extended_attributes_filename, line)) | |
60 valid_extended_attributes[name] = set() | |
61 value_list = values_string.split('|') | |
62 if not value_list: | |
63 valid_extended_attributes[name].add(None) | |
64 continue | |
65 for value in value_list: | |
66 value = value.strip() | |
67 if not value: | |
68 value = None | |
69 valid_extended_attributes[name].add(value) | |
70 return valid_extended_attributes | |
71 | |
72 | |
73 class IDLExtendedAttributeValidator: | |
74 def __init__(self, extended_attributes_filename): | |
75 self.valid_extended_attributes = read_extended_attributes_file(extended_ attributes_filename) | |
76 | |
77 def validate_extended_attributes(self, definitions, idl_filename): | |
78 # FIXME: this should be done when parsing the file, rather than after. | |
haraken
2013/07/17 02:44:49
I think postmortem processing sounds reasonable.
Nils Barth (inactive)
2013/07/17 12:05:09
(See longer reply.)
Nils Barth (inactive)
2013/07/17 12:11:02
It’s not a big deal either way, but validating whi
haraken
2013/07/21 14:31:50
Sounds like a plan. I don't have a strong opinion
Nils Barth (inactive)
2013/07/22 06:32:01
Got it; we’ll see what makes sense when actually t
| |
79 try: | |
80 for interface in definitions.interfaces.itervalues(): | |
81 self.validate_extended_attributes_node(interface) | |
82 for attribute in interface.attributes: | |
83 self.validate_extended_attributes_node(attribute) | |
84 for function in interface.functions: | |
85 self.validate_extended_attributes_node(function) | |
86 for argument in function.arguments: | |
87 self.validate_extended_attributes_node(argument) | |
88 except IDLInvalidExtendedAttributeError, error: | |
89 raise IDLInvalidExtendedAttributeError("""IDL ATTRIBUTE ERROR in fil e %s: | |
90 %s | |
91 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 | |
92 """ % (idl_filename, str(error))) | |
93 | |
94 def validate_extended_attributes_node(self, node): | |
95 for name, value_string in node.extended_attributes.iteritems(): | |
96 self.validate_name_value_string(name, value_string) | |
97 | |
98 def validate_name_value_string(self, name, values_string): | |
99 if name == 'ImplementedBy': # attribute added when merging interfaces | |
100 return | |
101 if name not in self.valid_extended_attributes: | |
102 raise IDLInvalidExtendedAttributeError('Unknown extended attribute [ %s]' % name) | |
103 valid_values = self.valid_extended_attributes[name] | |
104 if '*' in valid_values: # wildcard, any value ok | |
105 return | |
106 if values_string is None: | |
107 value_list = [None] | |
108 else: | |
109 value_list = re.split('[|&]', values_string.strip()) | |
110 for value in value_list: | |
111 if value not in valid_values: | |
haraken
2013/07/17 02:44:49
Don't you need to do value.strip() ?
Nils Barth (inactive)
2013/07/17 12:05:09
Nope, since the parser has already handled whitesp
| |
112 raise IDLInvalidExtendedAttributeError('Invalid value "{value}" found in extended attribute [{name}={value_string}]'.format(**locals())) | |
OLD | NEW |