blob: 96aa40b4cf053b73e7bb7649ba67872c38828573 [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
Yang Guo4fd355c2019-09-19 10:59:03 +020030import json
31import os
Blink Reformat4c46d092018-04-07 15:32:37 +000032import sys
33
Yang Guo4fd355c2019-09-19 10:59:03 +020034PYJSON5_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'third_party', 'pyjson5', 'src')
35sys.path.append(PYJSON5_DIR)
Blink Reformat4c46d092018-04-07 15:32:37 +000036
Yang Guo4fd355c2019-09-19 10:59:03 +020037import json5 # pylint: disable=import-error
Blink Reformat4c46d092018-04-07 15:32:37 +000038
39
40def _keep_only_required_keys(entry):
41 for key in entry.keys():
42 if key not in ("name", "longhands", "svg", "inherited"):
43 del entry[key]
44 return entry
45
46
47def properties_from_file(file_name):
48 with open(file_name) as json5_file:
Yang Guo4fd355c2019-09-19 10:59:03 +020049 doc = json5.loads(json5_file.read())
Blink Reformat4c46d092018-04-07 15:32:37 +000050
51 properties = []
52 property_names = {}
53 for entry in doc["data"]:
54 if type(entry) is str:
55 entry = {"name": entry}
56 if "alias_for" in entry:
57 continue
58 properties.append(_keep_only_required_keys(entry))
59 property_names[entry["name"]] = entry
60
61 properties.sort(key=lambda entry: entry["name"])
62
63 # Filter out unsupported longhands.
64 for property in properties:
65 longhands = property.get("longhands")
66 if not longhands:
67 continue
68 if type(longhands) is str:
69 longhands = longhands.split(";")
70 longhands = [longhand for longhand in longhands if longhand in property_names]
71 if not longhands:
72 del property["longhands"]
73 else:
74 property["longhands"] = longhands
75 all_inherited = True
76 for longhand in longhands:
77 longhand_property = property_names[longhand]
78 all_inherited = all_inherited and ("inherited" in longhand_property) and longhand_property["inherited"]
79 if all_inherited:
80 property["inherited"] = True
81
82 return properties
83
84
85properties = properties_from_file(sys.argv[1])
86with open(sys.argv[2], "w") as f:
87 f.write("SDK.CSSMetadata._generatedProperties = %s;" % json.dumps(properties))