1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
10 BASE
= os
.path
.dirname(__file__
.replace("\\", "/"))
11 sys
.path
.insert(0, os
.path
.join(BASE
, "Mako-1.1.2-py2.py3-none-any.whl"))
12 sys
.path
.insert(0, BASE
) # For importing `data.py`
14 from mako
import exceptions
15 from mako
.lookup
import TemplateLookup
16 from mako
.template
import Template
20 RE_PYTHON_ADDR
= re
.compile(r
"<.+? object at 0x[0-9a-fA-F]+>")
22 OUT_DIR
= os
.environ
.get("OUT_DIR", "")
53 "Usage: %s [ servo-2013 | servo-2020 | gecko ] [ style-crate | geckolib <template> | html ]"
61 if engine
not in ["servo-2013", "servo-2020", "gecko"] or output
not in [
68 properties
= data
.PropertiesData(engine
=engine
)
70 for kind
in ["longhands", "shorthands"]:
72 for struct
in STYLE_STRUCT_LIST
:
73 file_name
= os
.path
.join(BASE
, kind
, "{}.mako.rs".format(struct
))
74 if kind
== "shorthands" and not os
.path
.exists(file_name
):
75 files
[kind
][struct
] = ""
77 files
[kind
][struct
] = render(
82 properties_template
= os
.path
.join(BASE
, "properties.mako.rs")
83 files
["properties"] = render(
87 __file__
=properties_template
,
90 if output
== "style-crate":
91 write(OUT_DIR
, "properties.rs", files
["properties"])
92 for kind
in ["longhands", "shorthands"]:
93 for struct
in files
[kind
]:
95 os
.path
.join(OUT_DIR
, kind
),
96 "{}.rs".format(struct
),
100 if engine
== "gecko":
101 template
= os
.path
.join(BASE
, "gecko.mako.rs")
102 rust
= render(template
, data
=properties
)
103 write(OUT_DIR
, "gecko_properties.rs", rust
)
105 if engine
in ["servo-2013", "servo-2020"]:
106 if engine
== "servo-2013":
107 pref_attr
= "servo_2013_pref"
108 if engine
== "servo-2020":
109 pref_attr
= "servo_2020_pref"
112 p
.name
: {"pref": getattr(p
, pref_attr
)}
113 for prop
in properties_list
114 if prop
.enabled_in_content()
115 for p
in [prop
] + prop
.alias
117 for kind
, properties_list
in [
118 ("longhands", properties
.longhands
),
119 ("shorthands", properties
.shorthands
),
123 os
.path
.join(BASE
, "properties.html.mako"), properties
=properties_dict
125 as_json
= json
.dumps(properties_dict
, indent
=4, sort_keys
=True)
126 doc_servo
= os
.path
.join(BASE
, "..", "..", "..", "target", "doc", "servo")
127 write(doc_servo
, "css-properties.html", as_html
)
128 write(doc_servo
, "css-properties.json", as_json
)
129 write(OUT_DIR
, "css-properties.json", as_json
)
130 elif output
== "geckolib":
131 if len(sys
.argv
) < 4:
133 template
= sys
.argv
[3]
134 header
= render(template
, data
=properties
)
135 sys
.stdout
.write(header
)
139 print(message
, file=sys
.stderr
)
143 def render(filename
, **context
):
145 lookup
= TemplateLookup(
146 directories
=[BASE
], input_encoding
="utf8", strict_undefined
=True
149 open(filename
, "rb").read(),
151 input_encoding
="utf8",
153 strict_undefined
=True,
155 # Uncomment to debug generated Python code:
156 # write("/tmp", "mako_%s.py" % os.path.basename(filename), template.code)
157 return template
.render(**context
)
159 # Uncomment to see a traceback in generated Python code:
161 abort(exceptions
.text_error_template().render())
164 def write(directory
, filename
, content
):
165 if not os
.path
.exists(directory
):
166 os
.makedirs(directory
)
167 full_path
= os
.path
.join(directory
, filename
)
168 open(full_path
, "w", encoding
="utf-8").write(content
)
170 python_addr
= RE_PYTHON_ADDR
.search(content
)
172 abort('Found "{}" in {} ({})'.format(python_addr
.group(0), filename
, full_path
))
175 if __name__
== "__main__":