Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | #!/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 Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 30 | import json |
| 31 | import os |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 32 | import sys |
| 33 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 34 | PYJSON5_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'third_party', 'pyjson5', 'src') |
| 35 | sys.path.append(PYJSON5_DIR) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 36 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 37 | import json5 # pylint: disable=import-error |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def _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 | |
| 47 | def properties_from_file(file_name): |
| 48 | with open(file_name) as json5_file: |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 49 | doc = json5.loads(json5_file.read()) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 50 | |
| 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 | |
| 85 | properties = properties_from_file(sys.argv[1]) |
| 86 | with open(sys.argv[2], "w") as f: |
| 87 | f.write("SDK.CSSMetadata._generatedProperties = %s;" % json.dumps(properties)) |