Skip to content

Commit 2d8c17e

Browse files
committed
Extract some Python code from properties.mako.rs into a separate file.
1 parent ff5478c commit 2d8c17e

File tree

6 files changed

+330
-305
lines changed

6 files changed

+330
-305
lines changed

components/style/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() {
3535
let status = Command::new(python)
3636
.arg(&script)
3737
.arg(product)
38-
.arg("rust")
38+
.arg("style-crate")
3939
.status()
4040
.unwrap();
4141
if !status.success() {

components/style/properties/build.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,47 @@
1313
from mako import exceptions
1414
from mako.template import Template
1515

16+
import data
17+
1618

1719
def main():
18-
usage = "Usage: %s [ servo | gecko ] [ rust | html ]" % sys.argv[0]
20+
usage = "Usage: %s [ servo | gecko ] [ style-crate | geckolib | html ]" % sys.argv[0]
1921
if len(sys.argv) < 3:
2022
abort(usage)
2123
product = sys.argv[1]
2224
output = sys.argv[2]
23-
if product not in ["servo", "gecko"] or output not in ["rust", "html"]:
25+
if product not in ["servo", "gecko"] or output not in ["style-crate", "geckolib", "html"]:
2426
abort(usage)
2527

26-
template, rust = render("properties.mako.rs", PRODUCT=product)
27-
if output == "rust":
28+
properties = data.PropertiesData(product=product)
29+
rust = render(os.path.join(BASE, "properties.mako.rs"), product=product, data=properties)
30+
if output == "style-crate":
31+
write(os.environ["OUT_DIR"], "properties.rs", rust)
32+
if output == "geckolib":
33+
template = os.path.join(BASE, "..", "..", "..", "ports", "geckolib", "properties.mako.rs")
34+
rust = render(template, data=properties)
2835
write(os.environ["OUT_DIR"], "properties.rs", rust)
2936
elif output == "html":
30-
write_html(template)
37+
write_html(properties)
3138

3239

3340
def abort(message):
3441
sys.stderr.write(message + b"\n")
3542
sys.exit(1)
3643

3744

38-
def render(name, **context):
45+
def render(filename, **context):
3946
try:
40-
template = Template(open(os.path.join(BASE, name), "rb").read(), input_encoding="utf8")
41-
return template, template.render(**context).encode("utf8")
47+
template = Template(open(filename, "rb").read(),
48+
input_encoding="utf8",
49+
strict_undefined=True,
50+
filename=filename)
51+
# Uncomment to debug generated Python code:
52+
#write("/tmp", "mako_%s.py" % os.path.basename(filename), template.code)
53+
return template.render(**context).encode("utf8")
4254
except:
55+
# Uncomment to see a traceback in generated Python code:
56+
#raise
4357
abort(exceptions.text_error_template().render().encode("utf8"))
4458

4559

@@ -49,19 +63,18 @@ def write(directory, filename, content):
4963
open(os.path.join(directory, filename), "wb").write(content)
5064

5165

52-
def write_html(template):
66+
def write_html(properties):
5367
properties = dict(
5468
(p.name, {
5569
"flag": p.experimental,
5670
"shorthand": hasattr(p, "sub_properties")
5771
})
58-
for p in template.module.LONGHANDS + template.module.SHORTHANDS
72+
for p in properties.longhands + properties.shorthands
5973
)
60-
_, html = render("properties.html.mako", properties=properties)
61-
6274
doc_servo = os.path.join(BASE, "..", "..", "..", "target", "doc", "servo")
63-
write(doc_servo, "css-properties.json", json.dumps(properties, indent=4))
75+
html = render(os.path.join(BASE, "properties.html.mako"), properties=properties)
6476
write(doc_servo, "css-properties.html", html)
77+
write(doc_servo, "css-properties.json", json.dumps(properties, indent=4))
6578

6679

6780
if __name__ == "__main__":

components/style/properties/data.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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 http://mozilla.org/MPL/2.0/.
4+
5+
import re
6+
7+
8+
def to_rust_ident(name):
9+
name = name.replace("-", "_")
10+
if name in ["static", "super", "box", "move"]: # Rust keywords
11+
name += "_"
12+
return name
13+
14+
15+
def to_camel_case(ident):
16+
return re.sub("_([a-z])", lambda m: m.group(1).upper(), ident.strip("_").capitalize())
17+
18+
19+
class Keyword(object):
20+
def __init__(self, name, values, gecko_constant_prefix=None,
21+
extra_gecko_values=None, extra_servo_values=None):
22+
self.name = name
23+
self.values = values
24+
self.gecko_constant_prefix = gecko_constant_prefix or \
25+
"NS_STYLE_" + self.name.upper().replace("-", "_")
26+
self.extra_gecko_values = (extra_gecko_values or "").split()
27+
self.extra_servo_values = (extra_servo_values or "").split()
28+
29+
def gecko_values(self):
30+
return self.values + self.extra_gecko_values
31+
32+
def servo_values(self):
33+
return self.values + self.extra_servo_values
34+
35+
def values_for(self, product):
36+
if product == "gecko":
37+
return self.gecko_values()
38+
elif product == "servo":
39+
return self.servo_values()
40+
else:
41+
raise Exception("Bad product: " + product)
42+
43+
def gecko_constant(self, value):
44+
return self.gecko_constant_prefix + "_" + value.upper().replace("-", "_")
45+
46+
47+
class Longhand(object):
48+
def __init__(self, style_struct, name, derived_from=None, keyword=None,
49+
custom_cascade=False, experimental=False, internal=False,
50+
gecko_ffi_name=None):
51+
self.name = name
52+
self.keyword = keyword
53+
self.ident = to_rust_ident(name)
54+
self.camel_case = to_camel_case(self.ident)
55+
self.style_struct = style_struct
56+
self.experimental = ("layout.%s.enabled" % name) if experimental else None
57+
self.custom_cascade = custom_cascade
58+
self.internal = internal
59+
self.gecko_ffi_name = gecko_ffi_name or "m" + self.camel_case
60+
self.derived_from = (derived_from or "").split()
61+
62+
63+
class Shorthand(object):
64+
def __init__(self, name, sub_properties, experimental=False, internal=False):
65+
self.name = name
66+
self.ident = to_rust_ident(name)
67+
self.camel_case = to_camel_case(self.ident)
68+
self.derived_from = None
69+
self.experimental = ("layout.%s.enabled" % name) if experimental else None
70+
self.sub_properties = sub_properties
71+
self.internal = internal
72+
73+
74+
class Method(object):
75+
def __init__(self, name, return_type=None, arg_types=None, is_mut=False):
76+
self.name = name
77+
self.return_type = return_type
78+
self.arg_types = arg_types or []
79+
self.is_mut = is_mut
80+
81+
def arg_list(self):
82+
args = ["_: " + x for x in self.arg_types]
83+
args = ["&mut self" if self.is_mut else "&self"] + args
84+
return ", ".join(args)
85+
86+
def signature(self):
87+
sig = "fn %s(%s)" % (self.name, self.arg_list())
88+
if self.return_type:
89+
sig = sig + " -> " + self.return_type
90+
return sig
91+
92+
def declare(self):
93+
return self.signature() + ";"
94+
95+
def stub(self):
96+
return self.signature() + "{ unimplemented!() }"
97+
98+
99+
class StyleStruct(object):
100+
def __init__(self, name, inherited, gecko_ffi_name=None, additional_methods=None):
101+
self.servo_struct_name = "Servo" + name
102+
self.gecko_struct_name = "Gecko" + name
103+
self.trait_name = name
104+
self.trait_name_lower = name.lower()
105+
self.ident = to_rust_ident(self.trait_name_lower)
106+
self.longhands = []
107+
self.inherited = inherited
108+
self.gecko_ffi_name = gecko_ffi_name
109+
self.additional_methods = additional_methods or []
110+
111+
112+
class PropertiesData(object):
113+
def __init__(self, product):
114+
self.product = product
115+
self.style_structs = []
116+
self.current_style_struct = None
117+
self.longhands = []
118+
self.longhands_by_name = {}
119+
self.derived_longhands = {}
120+
self.shorthands = []
121+
122+
def new_style_struct(self, *args, **kwargs):
123+
style_struct = StyleStruct(*args, **kwargs)
124+
self.style_structs.append(style_struct)
125+
self.current_style_struct = style_struct
126+
127+
128+
def active_style_structs(self):
129+
return [s for s in self.style_structs if s.additional_methods or s.longhands]
130+
131+
132+
def switch_to_style_struct(self, name):
133+
for style_struct in self.style_structs:
134+
if style_struct.trait_name == name:
135+
self.current_style_struct = style_struct
136+
return
137+
raise Exception("Failed to find the struct named " + name)
138+
139+
def declare_longhand(self, name, products="gecko servo", **kwargs):
140+
products = products.split()
141+
if not self.product in products:
142+
return
143+
144+
longand = Longhand(self.current_style_struct, name, **kwargs)
145+
self.current_style_struct.longhands.append(longand)
146+
self.longhands.append(longand)
147+
self.longhands_by_name[name] = longand
148+
149+
for name in longand.derived_from:
150+
self.derived_longhands.setdefault(name, []).append(longand)
151+
152+
return longand
153+
154+
def declare_shorthand(self, name, sub_properties, *args, **kwargs):
155+
sub_properties = [self.longhands_by_name[s] for s in sub_properties]
156+
shorthand = Shorthand(name, sub_properties, *args, **kwargs)
157+
self.shorthands.append(shorthand)
158+
return shorthand

0 commit comments

Comments
 (0)