Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/generator.py

Issue 2888503002: Mojo bindings generator: introduce Stylizer to specify naming rules. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Code shared by the various language-specific code generators.""" 5 """Code shared by the various language-specific code generators."""
6 6
7 from functools import partial 7 from functools import partial
8 import os.path 8 import os.path
9 import re 9 import re
10 10
(...skipping 14 matching lines...) Expand all
25 if |lower_initial| is set to True), and joins the words. Please note that for 25 if |lower_initial| is set to True), and joins the words. Please note that for
26 each word, all the characters except the first one are untouched. 26 each word, all the characters except the first one are untouched.
27 """ 27 """
28 result = ''.join( 28 result = ''.join(
29 word[0].upper() + word[1:] for word in identifier.split(dilimiter)) 29 word[0].upper() + word[1:] for word in identifier.split(dilimiter))
30 if lower_initial: 30 if lower_initial:
31 result = result[0].lower() + result[1:] 31 result = result[0].lower() + result[1:]
32 return result 32 return result
33 33
34 34
35 class Stylizer(object):
36 """Stylizers specify naming rules to map mojom names to names in generated
37 code. For example, if you would like method_name in mojom to be mapped to
38 MethodName in the generated code, you need to define a subclass of Stylizer
39 and override StylizeMethod to do the conversion."""
40
41 def StylizeConstant(self, mojom_name):
42 return mojom_name
43
44 def StylizeField(self, mojom_name):
45 return mojom_name
46
47 def StylizeStruct(self, mojom_name):
48 return mojom_name
49
50 def StylizeUnion(self, mojom_name):
51 return mojom_name
52
53 def StylizeParameter(self, mojom_name):
54 return mojom_name
55
56 def StylizeMethod(self, mojom_name):
57 return mojom_name
58
59 def StylizeInterface(self, mojom_name):
60 return mojom_name
61
62 def StylizeEnumField(self, mojom_name):
63 return mojom_name
64
65 def StylizeEnum(self, mojom_name):
66 return mojom_name
67
68 def StylizeModule(self, mojom_namespace):
69 return mojom_namespace
70
71
35 def WriteFile(contents, full_path): 72 def WriteFile(contents, full_path):
36 # Make sure the containing directory exists. 73 # Make sure the containing directory exists.
37 full_dir = os.path.dirname(full_path) 74 full_dir = os.path.dirname(full_path)
38 fileutil.EnsureDirectoryExists(full_dir) 75 fileutil.EnsureDirectoryExists(full_dir)
39 76
40 # Dump the data to disk. 77 # Dump the data to disk.
41 with open(full_path, "w+") as f: 78 with open(full_path, "w+") as f:
42 f.write(contents) 79 f.write(contents)
43 80
44 81
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 if method.response_parameters is not None: 115 if method.response_parameters is not None:
79 method.response_param_struct = _GetResponseStructFromMethod(method) 116 method.response_param_struct = _GetResponseStructFromMethod(method)
80 interface.version = max( 117 interface.version = max(
81 interface.version, 118 interface.version,
82 method.response_param_struct.versions[-1].version) 119 method.response_param_struct.versions[-1].version)
83 else: 120 else:
84 method.response_param_struct = None 121 method.response_param_struct = None
85 122
86 def _GetStructFromMethod(method): 123 def _GetStructFromMethod(method):
87 """Converts a method's parameters into the fields of a struct.""" 124 """Converts a method's parameters into the fields of a struct."""
88 params_class = "%s_%s_Params" % (method.interface.name, method.name) 125 params_class = "%s_%s_Params" % (method.interface.mojom_name,
126 method.mojom_name)
89 struct = mojom.Struct(params_class, module=method.interface.module) 127 struct = mojom.Struct(params_class, module=method.interface.module)
90 for param in method.parameters: 128 for param in method.parameters:
91 struct.AddField(param.name, param.kind, param.ordinal, 129 struct.AddField(param.mojom_name, param.kind, param.ordinal,
92 attributes=param.attributes) 130 attributes=param.attributes)
93 _AddStructComputedData(False, struct) 131 _AddStructComputedData(False, struct)
94 return struct 132 return struct
95 133
96 def _GetResponseStructFromMethod(method): 134 def _GetResponseStructFromMethod(method):
97 """Converts a method's response_parameters into the fields of a struct.""" 135 """Converts a method's response_parameters into the fields of a struct."""
98 params_class = "%s_%s_ResponseParams" % (method.interface.name, method.name) 136 params_class = "%s_%s_ResponseParams" % (method.interface.mojom_name,
137 method.mojom_name)
99 struct = mojom.Struct(params_class, module=method.interface.module) 138 struct = mojom.Struct(params_class, module=method.interface.module)
100 for param in method.response_parameters: 139 for param in method.response_parameters:
101 struct.AddField(param.name, param.kind, param.ordinal, 140 struct.AddField(param.mojom_name, param.kind, param.ordinal,
102 attributes=param.attributes) 141 attributes=param.attributes)
103 _AddStructComputedData(False, struct) 142 _AddStructComputedData(False, struct)
104 return struct 143 return struct
105 144
106 for struct in module.structs: 145 for struct in module.structs:
107 _AddStructComputedData(True, struct) 146 _AddStructComputedData(True, struct)
108 for union in module.unions: 147 for union in module.unions:
109 _AddUnionComputedData(union) 148 _AddUnionComputedData(union)
110 for interface in module.interfaces: 149 for interface in module.interfaces:
111 _AddInterfaceComputedData(interface) 150 _AddInterfaceComputedData(interface)
(...skipping 11 matching lines...) Expand all
123 self.typemap = typemap or {} 162 self.typemap = typemap or {}
124 self.variant = variant 163 self.variant = variant
125 self.bytecode_path = bytecode_path 164 self.bytecode_path = bytecode_path
126 self.for_blink = for_blink 165 self.for_blink = for_blink
127 self.use_once_callback = use_once_callback 166 self.use_once_callback = use_once_callback
128 self.use_new_js_bindings = use_new_js_bindings 167 self.use_new_js_bindings = use_new_js_bindings
129 self.export_attribute = export_attribute 168 self.export_attribute = export_attribute
130 self.export_header = export_header 169 self.export_header = export_header
131 self.generate_non_variant_code = generate_non_variant_code 170 self.generate_non_variant_code = generate_non_variant_code
132 171
133 # Prepend the filename with a directory that matches the directory of the
134 # original .mojom file, relative to the import root.
135 def MatchMojomFilePath(self, filename):
136 return os.path.join(os.path.dirname(self.module.path), filename)
137
138 def Write(self, contents, filename): 172 def Write(self, contents, filename):
139 if self.output_dir is None: 173 if self.output_dir is None:
140 print contents 174 print contents
141 return 175 return
142 full_path = os.path.join(self.output_dir, filename) 176 full_path = os.path.join(self.output_dir, filename)
143 WriteFile(contents, full_path) 177 WriteFile(contents, full_path)
144 178
145 def GenerateFiles(self, args): 179 def GenerateFiles(self, args):
146 raise NotImplementedError("Subclasses must override/implement this method") 180 raise NotImplementedError("Subclasses must override/implement this method")
147 181
148 def GetJinjaParameters(self): 182 def GetJinjaParameters(self):
149 """Returns default constructor parameters for the jinja environment.""" 183 """Returns default constructor parameters for the jinja environment."""
150 return {} 184 return {}
151 185
152 def GetGlobals(self): 186 def GetGlobals(self):
153 """Returns global mappings for the template generation.""" 187 """Returns global mappings for the template generation."""
154 return {} 188 return {}
155 189
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/mojom_bindings_generator.py ('k') | mojo/public/tools/bindings/pylib/mojom/generate/module.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698