Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 import re | 1 # This file is part of the Adblock Plus website, |
2 # Copyright (C) 2006-2015 Eyeo GmbH | |
3 # | |
4 # Adblock Plus is free software: you can redistribute it and/or modify | |
5 # it under the terms of the GNU General Public License version 3 as | |
6 # published by the Free Software Foundation. | |
7 # | |
8 # Adblock Plus is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
13 # You should have received a copy of the GNU General Public License | |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
2 | 15 |
3 def parse_interface(interfaces): | 16 import re, warnings |
Wladimir Palant
2014/09/18 20:33:04
Nit: I think what it gets as parameter isn't inter
kzar
2014/09/19 11:28:06
Done.
| |
17 | |
18 TYPE_REGEXP = r"(?:arrayof\s+)?\w+" | |
19 | |
20 def parse_interface(interface_items): | |
4 parsed = [] | 21 parsed = [] |
5 for property_key, property in interfaces.iteritems(): | 22 for key, value in interface_items.iteritems(): |
Wladimir Palant
2014/09/18 20:33:04
Nit: I think that simple key, value as names would
kzar
2014/09/19 11:28:06
Done.
| |
6 if property_key.find("(") > -1: | 23 if "(" in key: |
Wladimir Palant
2014/09/18 20:33:04
Nit: this can be written as simply |"(" in propert
kzar
2014/09/19 11:28:06
Done.
| |
7 match = re.match("^(arrayof\W+\w+|\w+)\W+(\w+)[\W\(]+([^\)]*)[\W\)]+$", pr operty_key) | 24 # Item is a method |
Wladimir Palant
2014/09/18 20:33:04
You generally want to use r"foo" rather than "foo"
kzar
2014/09/19 11:28:06
Done.
| |
25 match = re.match(r"^\s*(%s)\s+(\S+)\s*\(\s*([^\)]*)\s*\)\s*$" % TYPE_REGEX P, key) | |
8 if not match: | 26 if not match: |
27 warnings.warn("Skipped malformed method: '%s'" % key) | |
9 continue | 28 continue |
Wladimir Palant
2014/09/18 20:33:04
Just dropping the line without any message is a gr
kzar
2014/09/19 11:28:06
Done.
| |
10 return_type, property_name, argument_string = match.groups() | 29 return_type, property_name, argument_string = match.groups() |
11 arguments = [] | 30 arguments = [] |
12 if "version" in property: | |
13 property_version = property["version"] | |
14 else: | |
15 property_version = "" | |
Wladimir Palant
2014/09/18 20:33:04
Nit: Use None instead of empty string here?
kzar
2014/09/19 11:28:06
I think empty strings are more useful for template
| |
16 if argument_string: | 31 if argument_string: |
17 for i, argument in enumerate(argument_string.split(",")): | 32 for argument in argument_string.split(","): |
Wladimir Palant
2014/09/18 20:33:04
Nit: Why use enumerate() here? Doesn't seem to be
kzar
2014/09/19 11:28:06
Done.
| |
18 parts = argument.split(" ") | 33 if argument.strip(): |
Wladimir Palant
2014/09/18 20:33:04
How about:
match = re.match(r"^\s*(%s)\s+(\w+)\
kzar
2014/09/19 11:28:06
Done.
| |
19 argument_name = parts[-1] | 34 match = re.match(r"^\s*(%s)\s+(\S+)\s*$" % TYPE_REGEXP, argument) |
20 argument_type = " ".join(parts[:-1]) | 35 if not match: |
21 arguments.append({ | 36 warnings.warn("Skipped malformed argument: '%s'" % argument) |
22 "name": argument_name, | 37 continue |
23 "type": argument_type | 38 argument_type, argument_name = match.groups() |
24 }) | 39 arguments.append({ |
25 parsed.append({ | 40 "name": argument_name, |
41 "type": argument_type | |
42 }) | |
43 value.update({ | |
26 "type": "method", | 44 "type": "method", |
27 "name": property_name, | 45 "name": property_name, |
28 "version": property_version, | |
29 "return_type": return_type, | 46 "return_type": return_type, |
30 "arguments": arguments, | 47 "arguments": arguments |
31 }) | 48 }) |
49 parsed.append(value) | |
32 else: | 50 else: |
33 parts = property_key.split() | 51 # Item is a property |
Wladimir Palant
2014/09/18 20:33:04
I'd prefer:
match = re.match(r"^\s*(readonly\s+
kzar
2014/09/19 11:28:06
Done.
| |
34 property = { } | 52 match = re.match(r"^\s*(readonly\s+)?(%s)\s+(\S+)\s*$" % TYPE_REGEXP, key) |
Wladimir Palant
2014/09/18 20:33:04
Nit: no space between the brackets.
kzar
2014/09/19 11:28:06
Done.
| |
35 property["type"], property["name"] = parts[-2:] | 53 if not match: |
36 if len(parts) == 3: | 54 warnings.warn("Skipped malformed property: '%s'" % key) |
37 property["modifier"] = parts[0] | 55 continue |
Wladimir Palant
2014/09/18 20:33:04
I think using temporary variables like you've done
kzar
2014/09/19 11:28:06
Done.
| |
38 parsed.append(property) | 56 property_modifier, property_type, property_name = match.groups() |
39 parsed = sorted(parsed, key=lambda x: x["name"]) | 57 value.update({ |
Wladimir Palant
2014/09/18 20:33:04
Please use parse.sort() that will sort the list in
kzar
2014/09/19 11:28:06
Done.
| |
58 "type": property_type, | |
59 "name": property_name, | |
60 "modifier": property_modifier or "" | |
61 }) | |
62 parsed.append(value) | |
63 parsed.sort(key=lambda x: x["name"]) | |
40 return parsed | 64 return parsed |
LEFT | RIGHT |