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

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

Issue 548343005: mojo: Specialize native type arrays. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Follow review 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 19 matching lines...) Expand all
30 mojom.DPPIPE: "_descriptor.TYPE_HANDLE", 30 mojom.DPPIPE: "_descriptor.TYPE_HANDLE",
31 mojom.MSGPIPE: "_descriptor.TYPE_HANDLE", 31 mojom.MSGPIPE: "_descriptor.TYPE_HANDLE",
32 mojom.SHAREDBUFFER: "_descriptor.TYPE_HANDLE", 32 mojom.SHAREDBUFFER: "_descriptor.TYPE_HANDLE",
33 mojom.NULLABLE_HANDLE: "_descriptor.TYPE_NULLABLE_HANDLE", 33 mojom.NULLABLE_HANDLE: "_descriptor.TYPE_NULLABLE_HANDLE",
34 mojom.NULLABLE_DCPIPE: "_descriptor.TYPE_NULLABLE_HANDLE", 34 mojom.NULLABLE_DCPIPE: "_descriptor.TYPE_NULLABLE_HANDLE",
35 mojom.NULLABLE_DPPIPE: "_descriptor.TYPE_NULLABLE_HANDLE", 35 mojom.NULLABLE_DPPIPE: "_descriptor.TYPE_NULLABLE_HANDLE",
36 mojom.NULLABLE_MSGPIPE: "_descriptor.TYPE_NULLABLE_HANDLE", 36 mojom.NULLABLE_MSGPIPE: "_descriptor.TYPE_NULLABLE_HANDLE",
37 mojom.NULLABLE_SHAREDBUFFER: "_descriptor.TYPE_NULLABLE_HANDLE", 37 mojom.NULLABLE_SHAREDBUFFER: "_descriptor.TYPE_NULLABLE_HANDLE",
38 } 38 }
39 39
40 # int64 integers are not handled by array.array. int64/uint64 array are
41 # supported but storage is not optimized (ie. they are plain python list, not
42 # array.array)
43 _kind_to_typecode = {
44 mojom.INT8: "'b'",
45 mojom.UINT8: "'B'",
46 mojom.INT16: "'h'",
47 mojom.UINT16: "'H'",
48 mojom.INT32: "'i'",
49 mojom.UINT32: "'I'",
50 mojom.FLOAT: "'f'",
51 mojom.DOUBLE: "'d'",
52 }
53
40 54
41 def NameToComponent(name): 55 def NameToComponent(name):
42 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> 56 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar ->
43 # HTTP_Entry2_FooBar) 57 # HTTP_Entry2_FooBar)
44 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name) 58 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name)
45 # insert '_' between non upper and start of upper blocks (e.g., 59 # insert '_' between non upper and start of upper blocks (e.g.,
46 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar) 60 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar)
47 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) 61 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name)
48 return [x.lower() for x in name.split('_')] 62 return [x.lower() for x in name.split('_')]
49 63
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 106
93 def GetStructClass(kind): 107 def GetStructClass(kind):
94 name = [] 108 name = []
95 if kind.imported_from: 109 if kind.imported_from:
96 name.append(kind.imported_from['python_module']) 110 name.append(kind.imported_from['python_module'])
97 name.append(GetNameForElement(kind)) 111 name.append(GetNameForElement(kind))
98 return '.'.join(name) 112 return '.'.join(name)
99 113
100 def GetFieldType(kind, field=None): 114 def GetFieldType(kind, field=None):
101 if mojom.IsAnyArrayKind(kind): 115 if mojom.IsAnyArrayKind(kind):
102 arguments = [ GetFieldType(kind.kind) ] 116 if kind.kind in _kind_to_typecode:
117 arguments = [ _kind_to_typecode[kind.kind] ]
118 else:
119 arguments = [ GetFieldType(kind.kind) ]
103 if mojom.IsNullableKind(kind): 120 if mojom.IsNullableKind(kind):
104 arguments.append("nullable=True") 121 arguments.append("nullable=True")
105 if mojom.IsFixedArrayKind(kind): 122 if mojom.IsFixedArrayKind(kind):
106 arguments.append("length=%d" % kind.length) 123 arguments.append("length=%d" % kind.length)
107 return "_descriptor.ArrayType(%s)" % ", ".join(arguments) 124 if kind.kind in _kind_to_typecode:
125 return "_descriptor.NativeArrayType(%s)" % ", ".join(arguments)
126 else:
127 return "_descriptor.PointerArrayType(%s)" % ", ".join(arguments)
108 128
109 if mojom.IsStructKind(kind): 129 if mojom.IsStructKind(kind):
110 arguments = [ GetStructClass(kind) ] 130 arguments = [ GetStructClass(kind) ]
111 if mojom.IsNullableKind(kind): 131 if mojom.IsNullableKind(kind):
112 arguments.append("nullable=True") 132 arguments.append("nullable=True")
113 return "_descriptor.StructType(%s)" % ", ".join(arguments) 133 return "_descriptor.StructType(%s)" % ", ".join(arguments)
114 134
115 if mojom.IsEnumKind(kind): 135 if mojom.IsEnumKind(kind):
116 return GetFieldType(mojom.INT32) 136 return GetFieldType(mojom.INT32)
117 137
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 def GetImports(self): 253 def GetImports(self):
234 for each in self.module.imports: 254 for each in self.module.imports:
235 each['python_module'] = each['module_name'].replace('.mojom', '_mojom') 255 each['python_module'] = each['module_name'].replace('.mojom', '_mojom')
236 return self.module.imports 256 return self.module.imports
237 257
238 def GetJinjaParameters(self): 258 def GetJinjaParameters(self):
239 return { 259 return {
240 'lstrip_blocks': True, 260 'lstrip_blocks': True,
241 'trim_blocks': True, 261 'trim_blocks': True,
242 } 262 }
OLDNEW
« no previous file with comments | « mojo/public/python/mojo/bindings/descriptor.py ('k') | mojo/python/tests/bindings_structs_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698