blob: 45f962944c8697cc78f0e46a4667c248f4d740e2 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001#!/usr/bin/env python
2# Copyright (c) 2014 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29
30try:
31 import simplejson as json
32except ImportError:
33 import json
34
35import ast
36import re
37import sys
38
39
40def _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
54def _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
59
60
61def properties_from_file(file_name):
62 with open(file_name) as json5_file:
63 doc = _json5_load(json5_file.read())
64
65 properties = []
66 property_names = {}
67 for entry in doc["data"]:
68 if type(entry) is str:
69 entry = {"name": entry}
70 if "alias_for" in entry:
71 continue
72 properties.append(_keep_only_required_keys(entry))
73 property_names[entry["name"]] = entry
74
75 properties.sort(key=lambda entry: entry["name"])
76
77 # Filter out unsupported longhands.
78 for property in properties:
79 longhands = property.get("longhands")
80 if not longhands:
81 continue
82 if type(longhands) is str:
83 longhands = longhands.split(";")
84 longhands = [longhand for longhand in longhands if longhand in property_names]
85 if not longhands:
86 del property["longhands"]
87 else:
88 property["longhands"] = longhands
89 all_inherited = True
90 for longhand in longhands:
91 longhand_property = property_names[longhand]
92 all_inherited = all_inherited and ("inherited" in longhand_property) and longhand_property["inherited"]
93 if all_inherited:
94 property["inherited"] = True
95
96 return properties
97
98
99properties = properties_from_file(sys.argv[1])
100with open(sys.argv[2], "w") as f:
101 f.write("SDK.CSSMetadata._generatedProperties = %s;" % json.dumps(properties))