| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 Google Inc. All rights reserved. | 2 # Copyright (c) 2014 Google Inc. All rights reserved. |
| 3 # | 3 # |
| 4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
| 6 # met: | 6 # met: |
| 7 # | 7 # |
| 8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 try: | 30 try: |
| 31 import simplejson as json | 31 import simplejson as json |
| 32 except ImportError: | 32 except ImportError: |
| 33 import json | 33 import json |
| 34 | 34 |
| 35 import ast |
| 36 import re |
| 35 import sys | 37 import sys |
| 36 import re | 38 |
| 39 |
| 40 def _json5_load(lines): |
| 41 # Use json5.loads when json5 is available. Currently we use simple |
| 42 # regexs to convert well-formed JSON5 to PYL format. |
| 43 # Strip away comments and quote unquoted keys. |
| 44 re_comment = re.compile(r"^\s*//.*$|//+ .*$", re.MULTILINE) |
| 45 re_map_keys = re.compile(r"^\s*([$A-Za-z_][\w]*)\s*:", re.MULTILINE) |
| 46 pyl = re.sub(re_map_keys, r"'\1':", re.sub(re_comment, "", lines)) |
| 47 # Convert map values of true/false to Python version True/False. |
| 48 re_true = re.compile(r":\s*true\b") |
| 49 re_false = re.compile(r":\s*false\b") |
| 50 pyl = re.sub(re_true, ":True", re.sub(re_false, ":False", pyl)) |
| 51 return ast.literal_eval(pyl) |
| 52 |
| 53 |
| 54 def _keep_only_required_keys(entry): |
| 55 for key in entry.keys(): |
| 56 if key not in ("name", "longhands", "svg", "inherited"): |
| 57 del entry[key] |
| 58 return entry |
| 37 | 59 |
| 38 | 60 |
| 39 def properties_from_file(file_name): | 61 def properties_from_file(file_name): |
| 62 with open(file_name) as json5_file: |
| 63 doc = _json5_load(json5_file.read()) |
| 64 |
| 40 properties = [] | 65 properties = [] |
| 41 propertyNames = set() | 66 propertyNames = set() |
| 42 with open(file_name, "r") as f: | 67 for entry in doc["data"]: |
| 43 for line in f: | 68 if type(entry) is str: |
| 44 line = line.strip() | 69 entry = {"name": entry} |
| 45 if not line or line.startswith("//") or "alias_for" in line: | 70 if "alias_for" in entry: |
| 46 continue | 71 continue |
| 47 partition = re.split("[, ]", line) | 72 properties.append(_keep_only_required_keys(entry)) |
| 48 name = partition[0] | 73 propertyNames.add(entry["name"]) |
| 49 attributes = partition[1:] | 74 |
| 50 entry = {"name": name} | 75 properties.sort(key=lambda entry: entry["name"]) |
| 51 if "inherited" in attributes: | |
| 52 entry["inherited"] = True | |
| 53 if "svg" in attributes: | |
| 54 entry["svg"] = True | |
| 55 propertyNames.add(name) | |
| 56 longhands = line.partition("longhands=")[2].partition(",")[0] | |
| 57 if longhands: | |
| 58 entry["longhands"] = longhands.split(";") | |
| 59 properties.append(entry) | |
| 60 | 76 |
| 61 # Filter out unsupported longhands. | 77 # Filter out unsupported longhands. |
| 62 for property in properties: | 78 for property in properties: |
| 63 if "longhands" not in property: | 79 longhands = property.get("longhands") |
| 80 if not longhands: |
| 64 continue | 81 continue |
| 65 longhands = property["longhands"] | 82 if type(longhands) is str: |
| 83 longhands = longhands.split(";") |
| 66 longhands = [longhand for longhand in longhands if longhand in propertyN
ames] | 84 longhands = [longhand for longhand in longhands if longhand in propertyN
ames] |
| 67 if not longhands: | 85 if not longhands: |
| 68 del property["longhands"] | 86 del property["longhands"] |
| 69 else: | 87 else: |
| 70 property["longhands"] = longhands | 88 property["longhands"] = longhands |
| 89 |
| 71 return properties | 90 return properties |
| 72 | 91 |
| 73 | 92 |
| 74 properties = properties_from_file(sys.argv[1]) | 93 properties = properties_from_file(sys.argv[1]) |
| 75 with open(sys.argv[2], "w") as f: | 94 with open(sys.argv[2], "w") as f: |
| 76 f.write("SDK.CSSMetadata._generatedProperties = %s;" % json.dumps(properties
)) | 95 f.write("SDK.CSSMetadata._generatedProperties = %s;" % json.dumps(properties
)) |
| OLD | NEW |