Index: filters/parse_interface.py |
diff --git a/filters/parse_interface.py b/filters/parse_interface.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7e2daf7ad619152c16410792ce25ca4c714cef9c |
--- /dev/null |
+++ b/filters/parse_interface.py |
@@ -0,0 +1,67 @@ |
+# This file is part of the Adblock Plus website, |
+# Copyright (C) 2006-2015 Eyeo GmbH |
+# |
+# Adblock Plus is free software: you can redistribute it and/or modify |
+# it under the terms of the GNU General Public License version 3 as |
+# published by the Free Software Foundation. |
+# |
+# Adblock Plus is distributed in the hope that it will be useful, |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+# GNU General Public License for more details. |
+# |
+# You should have received a copy of the GNU General Public License |
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ |
+import re, warnings |
+ |
+TYPE_REGEXP = r"(?:arrayof\s+)?\w+" |
+ |
+def parse_interface(interface_items): |
+ parsed = [] |
+ for key, value in interface_items.iteritems(): |
+ if "(" in key: |
+ # Item is a method |
+ match = re.match(r"^\s*(%s)\s+(\S+)\s*\(\s*([^\)]*)\s*\)\s*$" % TYPE_REGEXP, key) |
+ if not match: |
+ warnings.warn("Skipped malformed method: '%s'" % key) |
+ continue |
+ return_type, property_name, argument_string = match.groups() |
+ arguments = [] |
+ if "version" in value: |
+ property_version = value["version"] |
+ else: |
+ property_version = "" |
Wladimir Palant
2015/02/24 20:19:40
Nit: how about:
property_version = value.get("v
kzar
2015/02/26 19:50:17
Done.
|
+ if argument_string: |
+ for argument in argument_string.split(","): |
+ if argument.strip(): |
+ match = re.match(r"^\s*(%s)\s+(\S+)\s*$" % TYPE_REGEXP, argument) |
+ if not match: |
+ warnings.warn("Skipped malformed argument: '%s'" % argument) |
+ continue |
+ argument_type, argument_name = match.groups() |
+ arguments.append({ |
+ "name": argument_name, |
+ "type": argument_type |
+ }) |
+ parsed.append({ |
+ "type": "method", |
+ "name": property_name, |
+ "version": property_version, |
+ "return_type": return_type, |
+ "arguments": arguments |
+ }) |
+ else: |
+ # Item is a property |
+ match = re.match(r"^\s*(readonly\s+)?(%s)\s+(\S+)\s*$" % TYPE_REGEXP, key) |
+ if not match: |
+ warnings.warn("Skipped malformed property: '%s'" % key) |
+ continue |
+ property_modifier, property_type, property_name = match.groups() |
+ parsed.append({ |
+ "type": property_type, |
+ "name": property_name, |
+ "modifier": property_modifier or "" |
+ }) |
+ parsed.sort(key=lambda x: x["name"]) |
+ return parsed |