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

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_python_generator.py

Issue 538613005: mojo: Start generating structs for python bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase + fix constant array Created 6 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 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 """Generates Python source files from a mojom.Module.""" 5 """Generates Python source files from a mojom.Module."""
6 6
7 import re 7 import re
8 from itertools import ifilter 8 from itertools import ifilter
9 9
10 import mojom.generate.generator as generator 10 import mojom.generate.generator as generator
(...skipping 17 matching lines...) Expand all
28 uccc = UpperCamelCase(name) 28 uccc = UpperCamelCase(name)
29 return uccc[0].lower() + uccc[1:] 29 return uccc[0].lower() + uccc[1:]
30 30
31 def ConstantStyle(name): 31 def ConstantStyle(name):
32 components = NameToComponent(name) 32 components = NameToComponent(name)
33 if components[0] == 'k': 33 if components[0] == 'k':
34 components = components[1:] 34 components = components[1:]
35 return '_'.join([x.upper() for x in components]) 35 return '_'.join([x.upper() for x in components])
36 36
37 def GetNameForElement(element): 37 def GetNameForElement(element):
38 if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element) or
39 mojom.IsStructKind(element)):
40 return UpperCamelCase(element.name)
38 if isinstance(element, mojom.EnumValue): 41 if isinstance(element, mojom.EnumValue):
39 return (GetNameForElement(element.enum) + '.' + 42 return (GetNameForElement(element.enum) + '.' +
40 ConstantStyle(element.name)) 43 ConstantStyle(element.name))
41 if isinstance(element, (mojom.NamedValue, 44 if isinstance(element, (mojom.NamedValue,
42 mojom.Constant)): 45 mojom.Constant)):
43 return ConstantStyle(element.name) 46 return ConstantStyle(element.name)
44 raise Exception('Unexpected element: ' % element) 47 raise Exception('Unexpected element: ' % element)
45 48
46 def ExpressionToText(token): 49 def ExpressionToText(token):
47 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): 50 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)):
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 'expression_to_text': ExpressionToText, 125 'expression_to_text': ExpressionToText,
123 'name': GetNameForElement, 126 'name': GetNameForElement,
124 } 127 }
125 128
126 @UseJinja('python_templates/module.py.tmpl', filters=python_filters) 129 @UseJinja('python_templates/module.py.tmpl', filters=python_filters)
127 def GeneratePythonModule(self): 130 def GeneratePythonModule(self):
128 return { 131 return {
129 'imports': self.GetImports(), 132 'imports': self.GetImports(),
130 'enums': self.module.enums, 133 'enums': self.module.enums,
131 'module': ComputeConstantValues(self.module), 134 'module': ComputeConstantValues(self.module),
135 'structs': self.GetStructs(),
132 } 136 }
133 137
134 def GenerateFiles(self, args): 138 def GenerateFiles(self, args):
135 self.Write(self.GeneratePythonModule(), 139 self.Write(self.GeneratePythonModule(),
136 '%s.py' % self.module.name.replace('.mojom', '_mojom')) 140 '%s.py' % self.module.name.replace('.mojom', '_mojom'))
137 141
138 def GetImports(self): 142 def GetImports(self):
139 for each in self.module.imports: 143 for each in self.module.imports:
140 each['python_module'] = each['module_name'].replace('.mojom', '_mojom') 144 each['python_module'] = each['module_name'].replace('.mojom', '_mojom')
141 return self.module.imports 145 return self.module.imports
142 146
143 def GetJinjaParameters(self): 147 def GetJinjaParameters(self):
144 return { 148 return {
145 'lstrip_blocks': True, 149 'lstrip_blocks': True,
146 'trim_blocks': True, 150 'trim_blocks': True,
147 } 151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698