| OLD | NEW |
| 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 java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
| 6 | 6 |
| 7 import argparse | 7 import argparse |
| 8 import ast | 8 import ast |
| 9 import os | 9 import os |
| 10 import re | 10 import re |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 if isinstance(element, (mojom.Method, | 117 if isinstance(element, (mojom.Method, |
| 118 mojom.Parameter, | 118 mojom.Parameter, |
| 119 mojom.Field)): | 119 mojom.Field)): |
| 120 return CamelCase(element.name) | 120 return CamelCase(element.name) |
| 121 if isinstance(element, mojom.EnumValue): | 121 if isinstance(element, mojom.EnumValue): |
| 122 return (UpperCamelCase(element.enum_name) + '.' + | 122 return (UpperCamelCase(element.enum_name) + '.' + |
| 123 ConstantStyle(element.name)) | 123 ConstantStyle(element.name)) |
| 124 if isinstance(element, (mojom.NamedValue, | 124 if isinstance(element, (mojom.NamedValue, |
| 125 mojom.Constant)): | 125 mojom.Constant)): |
| 126 return ConstantStyle(element.name) | 126 return ConstantStyle(element.name) |
| 127 raise Exception("Unexpected element: " % element) | 127 raise Exception('Unexpected element: ' % element) |
| 128 | 128 |
| 129 def GetInterfaceResponseName(method): | 129 def GetInterfaceResponseName(method): |
| 130 return UpperCamelCase(method.name + 'Response') | 130 return UpperCamelCase(method.name + 'Response') |
| 131 | 131 |
| 132 def ParseStringAttribute(attribute): | 132 def ParseStringAttribute(attribute): |
| 133 assert isinstance(attribute, basestring) | 133 assert isinstance(attribute, basestring) |
| 134 return attribute | 134 return attribute |
| 135 | 135 |
| 136 def GetJavaTrueFalse(value): | 136 def GetJavaTrueFalse(value): |
| 137 return "true" if value else "false" | 137 return 'true' if value else 'false' |
| 138 | 138 |
| 139 def GetArrayNullabilityFlags(kind): | 139 def GetArrayNullabilityFlags(kind): |
| 140 """Returns nullability flags for an array type, see Decoder.java. | 140 """Returns nullability flags for an array type, see Decoder.java. |
| 141 | 141 |
| 142 As we have dedicated decoding functions for arrays, we have to pass | 142 As we have dedicated decoding functions for arrays, we have to pass |
| 143 nullability information about both the array itself, as well as the array | 143 nullability information about both the array itself, as well as the array |
| 144 element type there. | 144 element type there. |
| 145 """ | 145 """ |
| 146 assert mojom.IsAnyArrayKind(kind) | 146 assert mojom.IsAnyArrayKind(kind) |
| 147 ARRAY_NULLABLE = \ | 147 ARRAY_NULLABLE = \ |
| 148 "org.chromium.mojo.bindings.BindingsHelper.ARRAY_NULLABLE" | 148 'org.chromium.mojo.bindings.BindingsHelper.ARRAY_NULLABLE' |
| 149 ELEMENT_NULLABLE = \ | 149 ELEMENT_NULLABLE = \ |
| 150 "org.chromium.mojo.bindings.BindingsHelper.ELEMENT_NULLABLE" | 150 'org.chromium.mojo.bindings.BindingsHelper.ELEMENT_NULLABLE' |
| 151 NOTHING_NULLABLE = \ | 151 NOTHING_NULLABLE = \ |
| 152 "org.chromium.mojo.bindings.BindingsHelper.NOTHING_NULLABLE" | 152 'org.chromium.mojo.bindings.BindingsHelper.NOTHING_NULLABLE' |
| 153 | 153 |
| 154 flags_to_set = [] | 154 flags_to_set = [] |
| 155 if mojom.IsNullableKind(kind): | 155 if mojom.IsNullableKind(kind): |
| 156 flags_to_set.append(ARRAY_NULLABLE) | 156 flags_to_set.append(ARRAY_NULLABLE) |
| 157 if mojom.IsNullableKind(kind.kind): | 157 if mojom.IsNullableKind(kind.kind): |
| 158 flags_to_set.append(ELEMENT_NULLABLE) | 158 flags_to_set.append(ELEMENT_NULLABLE) |
| 159 | 159 |
| 160 if not flags_to_set: | 160 if not flags_to_set: |
| 161 flags_to_set = [NOTHING_NULLABLE] | 161 flags_to_set = [NOTHING_NULLABLE] |
| 162 return " | ".join(flags_to_set) | 162 return ' | '.join(flags_to_set) |
| 163 | 163 |
| 164 | 164 |
| 165 def AppendEncodeDecodeParams(initial_params, context, kind, bit): | 165 def AppendEncodeDecodeParams(initial_params, context, kind, bit): |
| 166 """ Appends standard parameters shared between encode and decode calls. """ | 166 """ Appends standard parameters shared between encode and decode calls. """ |
| 167 params = list(initial_params) | 167 params = list(initial_params) |
| 168 if (kind == mojom.BOOL): | 168 if (kind == mojom.BOOL): |
| 169 params.append(str(bit)) | 169 params.append(str(bit)) |
| 170 if mojom.IsReferenceKind(kind): | 170 if mojom.IsReferenceKind(kind): |
| 171 if mojom.IsAnyArrayKind(kind): | 171 if mojom.IsAnyArrayKind(kind): |
| 172 params.append(GetArrayNullabilityFlags(kind)) | 172 params.append(GetArrayNullabilityFlags(kind)) |
| 173 else: | 173 else: |
| 174 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) | 174 params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) |
| 175 if mojom.IsAnyArrayKind(kind): | 175 if mojom.IsAnyArrayKind(kind): |
| 176 if mojom.IsFixedArrayKind(kind): | 176 if mojom.IsFixedArrayKind(kind): |
| 177 params.append(str(kind.length)) | 177 params.append(str(kind.length)) |
| 178 else: | 178 else: |
| 179 params.append( | 179 params.append( |
| 180 "org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH"); | 180 'org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH'); |
| 181 if mojom.IsInterfaceKind(kind): | 181 if mojom.IsInterfaceKind(kind): |
| 182 params.append('%s.MANAGER' % GetJavaType(context, kind)) | 182 params.append('%s.MANAGER' % GetJavaType(context, kind)) |
| 183 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): | 183 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): |
| 184 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) | 184 params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) |
| 185 return params | 185 return params |
| 186 | 186 |
| 187 | 187 |
| 188 @contextfilter | 188 @contextfilter |
| 189 def DecodeMethod(context, kind, offset, bit): | 189 def DecodeMethod(context, kind, offset, bit): |
| 190 def _DecodeMethodName(kind): | 190 def _DecodeMethodName(kind): |
| 191 if mojom.IsAnyArrayKind(kind): | 191 if mojom.IsAnyArrayKind(kind): |
| 192 return _DecodeMethodName(kind.kind) + 's' | 192 return _DecodeMethodName(kind.kind) + 's' |
| 193 if mojom.IsEnumKind(kind): | 193 if mojom.IsEnumKind(kind): |
| 194 return _DecodeMethodName(mojom.INT32) | 194 return _DecodeMethodName(mojom.INT32) |
| 195 if mojom.IsInterfaceRequestKind(kind): | 195 if mojom.IsInterfaceRequestKind(kind): |
| 196 return "readInterfaceRequest" | 196 return 'readInterfaceRequest' |
| 197 if mojom.IsInterfaceKind(kind): | 197 if mojom.IsInterfaceKind(kind): |
| 198 return "readServiceInterface" | 198 return 'readServiceInterface' |
| 199 return _spec_to_decode_method[kind.spec] | 199 return _spec_to_decode_method[kind.spec] |
| 200 methodName = _DecodeMethodName(kind) | 200 methodName = _DecodeMethodName(kind) |
| 201 params = AppendEncodeDecodeParams([ str(offset) ], context, kind, bit) | 201 params = AppendEncodeDecodeParams([ str(offset) ], context, kind, bit) |
| 202 return '%s(%s)' % (methodName, ', '.join(params)) | 202 return '%s(%s)' % (methodName, ', '.join(params)) |
| 203 | 203 |
| 204 @contextfilter | 204 @contextfilter |
| 205 def EncodeMethod(context, kind, variable, offset, bit): | 205 def EncodeMethod(context, kind, variable, offset, bit): |
| 206 params = AppendEncodeDecodeParams( | 206 params = AppendEncodeDecodeParams( |
| 207 [ variable, str(offset) ], context, kind, bit) | 207 [ variable, str(offset) ], context, kind, bit) |
| 208 return 'encode(%s)' % ', '.join(params) | 208 return 'encode(%s)' % ', '.join(params) |
| 209 | 209 |
| 210 def GetPackage(module): | 210 def GetPackage(module): |
| 211 if 'JavaPackage' in module.attributes: | 211 if 'JavaPackage' in module.attributes: |
| 212 return ParseStringAttribute(module.attributes['JavaPackage']) | 212 return ParseStringAttribute(module.attributes['JavaPackage']) |
| 213 # Default package. | 213 # Default package. |
| 214 return "org.chromium.mojom." + module.namespace | 214 return 'org.chromium.mojom.' + module.namespace |
| 215 | 215 |
| 216 def GetNameForKind(context, kind): | 216 def GetNameForKind(context, kind): |
| 217 def _GetNameHierachy(kind): | 217 def _GetNameHierachy(kind): |
| 218 hierachy = [] | 218 hierachy = [] |
| 219 if kind.parent_kind: | 219 if kind.parent_kind: |
| 220 hierachy = _GetNameHierachy(kind.parent_kind) | 220 hierachy = _GetNameHierachy(kind.parent_kind) |
| 221 hierachy.append(GetNameForElement(kind)) | 221 hierachy.append(GetNameForElement(kind)) |
| 222 return hierachy | 222 return hierachy |
| 223 | 223 |
| 224 module = context.resolve('module') | 224 module = context.resolve('module') |
| 225 elements = [] | 225 elements = [] |
| 226 if GetPackage(module) != GetPackage(kind.module): | 226 if GetPackage(module) != GetPackage(kind.module): |
| 227 elements += [GetPackage(kind.module)] | 227 elements += [GetPackage(kind.module)] |
| 228 elements += _GetNameHierachy(kind) | 228 elements += _GetNameHierachy(kind) |
| 229 return '.'.join(elements) | 229 return '.'.join(elements) |
| 230 | 230 |
| 231 def GetBoxedJavaType(context, kind): | 231 def GetBoxedJavaType(context, kind): |
| 232 unboxed_type = GetJavaType(context, kind, False) | 232 unboxed_type = GetJavaType(context, kind, False) |
| 233 if unboxed_type in _java_primitive_to_boxed_type: | 233 if unboxed_type in _java_primitive_to_boxed_type: |
| 234 return _java_primitive_to_boxed_type[unboxed_type] | 234 return _java_primitive_to_boxed_type[unboxed_type] |
| 235 return unboxed_type | 235 return unboxed_type |
| 236 | 236 |
| 237 @contextfilter | 237 @contextfilter |
| 238 def GetJavaType(context, kind, boxed=False): | 238 def GetJavaType(context, kind, boxed=False): |
| 239 if boxed: | 239 if boxed: |
| 240 return GetBoxedJavaType(context, kind) | 240 return GetBoxedJavaType(context, kind) |
| 241 if mojom.IsStructKind(kind) or mojom.IsInterfaceKind(kind): | 241 if mojom.IsStructKind(kind) or mojom.IsInterfaceKind(kind): |
| 242 return GetNameForKind(context, kind) | 242 return GetNameForKind(context, kind) |
| 243 if mojom.IsInterfaceRequestKind(kind): | 243 if mojom.IsInterfaceRequestKind(kind): |
| 244 return ("org.chromium.mojo.bindings.InterfaceRequest<%s>" % | 244 return ('org.chromium.mojo.bindings.InterfaceRequest<%s>' % |
| 245 GetNameForKind(context, kind.kind)) | 245 GetNameForKind(context, kind.kind)) |
| 246 if mojom.IsAnyArrayKind(kind): | 246 if mojom.IsAnyArrayKind(kind): |
| 247 return "%s[]" % GetJavaType(context, kind.kind) | 247 return '%s[]' % GetJavaType(context, kind.kind) |
| 248 if mojom.IsEnumKind(kind): | 248 if mojom.IsEnumKind(kind): |
| 249 return "int" | 249 return 'int' |
| 250 return _spec_to_java_type[kind.spec] | 250 return _spec_to_java_type[kind.spec] |
| 251 | 251 |
| 252 @contextfilter | 252 @contextfilter |
| 253 def DefaultValue(context, field): | 253 def DefaultValue(context, field): |
| 254 assert field.default | 254 assert field.default |
| 255 if isinstance(field.kind, mojom.Struct): | 255 if isinstance(field.kind, mojom.Struct): |
| 256 assert field.default == "default" | 256 assert field.default == 'default' |
| 257 return "new %s()" % GetJavaType(context, field.kind) | 257 return 'new %s()' % GetJavaType(context, field.kind) |
| 258 return "(%s) %s" % ( | 258 return '(%s) %s' % ( |
| 259 GetJavaType(context, field.kind), | 259 GetJavaType(context, field.kind), |
| 260 ExpressionToText(context, field.default, kind_spec=field.kind.spec)) | 260 ExpressionToText(context, field.default, kind_spec=field.kind.spec)) |
| 261 | 261 |
| 262 @contextfilter | 262 @contextfilter |
| 263 def ConstantValue(context, constant): | 263 def ConstantValue(context, constant): |
| 264 return "(%s) %s" % ( | 264 return '(%s) %s' % ( |
| 265 GetJavaType(context, constant.kind), | 265 GetJavaType(context, constant.kind), |
| 266 ExpressionToText(context, constant.value, kind_spec=constant.kind.spec)) | 266 ExpressionToText(context, constant.value, kind_spec=constant.kind.spec)) |
| 267 | 267 |
| 268 @contextfilter | 268 @contextfilter |
| 269 def NewArray(context, kind, size): | 269 def NewArray(context, kind, size): |
| 270 if mojom.IsAnyArrayKind(kind.kind): | 270 if mojom.IsAnyArrayKind(kind.kind): |
| 271 return NewArray(context, kind.kind, size) + '[]' | 271 return NewArray(context, kind.kind, size) + '[]' |
| 272 return 'new %s[%s]' % (GetJavaType(context, kind.kind), size) | 272 return 'new %s[%s]' % (GetJavaType(context, kind.kind), size) |
| 273 | 273 |
| 274 @contextfilter | 274 @contextfilter |
| (...skipping 17 matching lines...) Expand all Loading... |
| 292 number = ast.literal_eval(token.lstrip('+ ')) | 292 number = ast.literal_eval(token.lstrip('+ ')) |
| 293 if not isinstance(number, (int, long)): | 293 if not isinstance(number, (int, long)): |
| 294 raise ValueError('got unexpected type %r for int literal %r' % ( | 294 raise ValueError('got unexpected type %r for int literal %r' % ( |
| 295 type(number), token)) | 295 type(number), token)) |
| 296 # If the literal is too large to fit a signed long, convert it to the | 296 # If the literal is too large to fit a signed long, convert it to the |
| 297 # equivalent signed long. | 297 # equivalent signed long. |
| 298 if number >= 2 ** 63: | 298 if number >= 2 ** 63: |
| 299 number -= 2 ** 64 | 299 number -= 2 ** 64 |
| 300 return '%dL' % number | 300 return '%dL' % number |
| 301 if isinstance(token, mojom.BuiltinValue): | 301 if isinstance(token, mojom.BuiltinValue): |
| 302 if token.value == "double.INFINITY": | 302 if token.value == 'double.INFINITY': |
| 303 return "java.lang.Double.POSITIVE_INFINITY" | 303 return 'java.lang.Double.POSITIVE_INFINITY' |
| 304 if token.value == "double.NEGATIVE_INFINITY": | 304 if token.value == 'double.NEGATIVE_INFINITY': |
| 305 return "java.lang.Double.NEGATIVE_INFINITY" | 305 return 'java.lang.Double.NEGATIVE_INFINITY' |
| 306 if token.value == "double.NAN": | 306 if token.value == 'double.NAN': |
| 307 return "java.lang.Double.NaN" | 307 return 'java.lang.Double.NaN' |
| 308 if token.value == "float.INFINITY": | 308 if token.value == 'float.INFINITY': |
| 309 return "java.lang.Float.POSITIVE_INFINITY" | 309 return 'java.lang.Float.POSITIVE_INFINITY' |
| 310 if token.value == "float.NEGATIVE_INFINITY": | 310 if token.value == 'float.NEGATIVE_INFINITY': |
| 311 return "java.lang.Float.NEGATIVE_INFINITY" | 311 return 'java.lang.Float.NEGATIVE_INFINITY' |
| 312 if token.value == "float.NAN": | 312 if token.value == 'float.NAN': |
| 313 return "java.lang.Float.NaN" | 313 return 'java.lang.Float.NaN' |
| 314 return token | 314 return token |
| 315 | 315 |
| 316 def IsPointerArrayKind(kind): | 316 def IsPointerArrayKind(kind): |
| 317 if not mojom.IsAnyArrayKind(kind): | 317 if not mojom.IsAnyArrayKind(kind): |
| 318 return False | 318 return False |
| 319 sub_kind = kind.kind | 319 sub_kind = kind.kind |
| 320 return mojom.IsObjectKind(sub_kind) | 320 return mojom.IsObjectKind(sub_kind) |
| 321 | 321 |
| 322 def GetResponseStructFromMethod(method): | 322 def GetResponseStructFromMethod(method): |
| 323 return generator.GetDataHeader( | 323 return generator.GetDataHeader( |
| 324 False, generator.GetResponseStructFromMethod(method)) | 324 False, generator.GetResponseStructFromMethod(method)) |
| 325 | 325 |
| 326 def GetStructFromMethod(method): | 326 def GetStructFromMethod(method): |
| 327 return generator.GetDataHeader( | 327 return generator.GetDataHeader( |
| 328 False, generator.GetStructFromMethod(method)) | 328 False, generator.GetStructFromMethod(method)) |
| 329 | 329 |
| 330 def GetConstantsMainEntityName(module): | 330 def GetConstantsMainEntityName(module): |
| 331 if 'JavaConstantsClassName' in module.attributes: | 331 if 'JavaConstantsClassName' in module.attributes: |
| 332 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) | 332 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
| 333 # This constructs the name of the embedding classes for module level constants | 333 # This constructs the name of the embedding classes for module level constants |
| 334 # by extracting the mojom's filename and prepending it to Constants. | 334 # by extracting the mojom's filename and prepending it to Constants. |
| 335 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 335 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
| 336 'Constants') | 336 'Constants') |
| 337 | 337 |
| 338 def GetMethodOrdinalName(method): | 338 def GetMethodOrdinalName(method): |
| 339 return ConstantStyle(method.name) + "_ORDINAL" | 339 return ConstantStyle(method.name) + '_ORDINAL' |
| 340 | 340 |
| 341 def HasMethodWithResponse(interface): | 341 def HasMethodWithResponse(interface): |
| 342 for method in interface.methods: | 342 for method in interface.methods: |
| 343 if method.response_parameters: | 343 if method.response_parameters: |
| 344 return True | 344 return True |
| 345 return False | 345 return False |
| 346 | 346 |
| 347 def HasMethodWithoutResponse(interface): | 347 def HasMethodWithoutResponse(interface): |
| 348 for method in interface.methods: | 348 for method in interface.methods: |
| 349 if not method.response_parameters: | 349 if not method.response_parameters: |
| 350 return True | 350 return True |
| 351 return False | 351 return False |
| 352 | 352 |
| 353 class Generator(generator.Generator): | 353 class Generator(generator.Generator): |
| 354 | 354 |
| 355 java_filters = { | 355 java_filters = { |
| 356 "interface_response_name": GetInterfaceResponseName, | 356 'interface_response_name': GetInterfaceResponseName, |
| 357 "constant_value": ConstantValue, | 357 'constant_value': ConstantValue, |
| 358 "default_value": DefaultValue, | 358 'default_value': DefaultValue, |
| 359 "decode_method": DecodeMethod, | 359 'decode_method': DecodeMethod, |
| 360 "expression_to_text": ExpressionToText, | 360 'expression_to_text': ExpressionToText, |
| 361 "encode_method": EncodeMethod, | 361 'encode_method': EncodeMethod, |
| 362 "has_method_with_response": HasMethodWithResponse, | 362 'has_method_with_response': HasMethodWithResponse, |
| 363 "has_method_without_response": HasMethodWithoutResponse, | 363 'has_method_without_response': HasMethodWithoutResponse, |
| 364 "is_fixed_array_kind": mojom.IsFixedArrayKind, | 364 'is_fixed_array_kind': mojom.IsFixedArrayKind, |
| 365 "is_handle": mojom.IsNonInterfaceHandleKind, | 365 'is_handle': mojom.IsNonInterfaceHandleKind, |
| 366 "is_nullable_kind": mojom.IsNullableKind, | 366 'is_nullable_kind': mojom.IsNullableKind, |
| 367 "is_pointer_array_kind": IsPointerArrayKind, | 367 'is_pointer_array_kind': IsPointerArrayKind, |
| 368 "is_struct_kind": mojom.IsStructKind, | 368 'is_struct_kind': mojom.IsStructKind, |
| 369 "java_type": GetJavaType, | 369 'java_type': GetJavaType, |
| 370 "java_true_false": GetJavaTrueFalse, | 370 'java_true_false': GetJavaTrueFalse, |
| 371 "method_ordinal_name": GetMethodOrdinalName, | 371 'method_ordinal_name': GetMethodOrdinalName, |
| 372 "name": GetNameForElement, | 372 'name': GetNameForElement, |
| 373 "new_array": NewArray, | 373 'new_array': NewArray, |
| 374 "response_struct_from_method": GetResponseStructFromMethod, | 374 'response_struct_from_method': GetResponseStructFromMethod, |
| 375 "struct_from_method": GetStructFromMethod, | 375 'struct_from_method': GetStructFromMethod, |
| 376 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 376 'struct_size': lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
| 377 } | 377 } |
| 378 | 378 |
| 379 def GetJinjaExports(self): | 379 def GetJinjaExports(self): |
| 380 return { | 380 return { |
| 381 "module": self.module, | 381 'package': GetPackage(self.module), |
| 382 "package": GetPackage(self.module), | |
| 383 } | 382 } |
| 384 | 383 |
| 385 def GetJinjaExportsForInterface(self, interface): | 384 def GetJinjaExportsForInterface(self, interface): |
| 386 exports = self.GetJinjaExports() | 385 exports = self.GetJinjaExports() |
| 387 exports.update({"interface": interface}) | 386 exports.update({'interface': interface}) |
| 388 if interface.client: | 387 if interface.client: |
| 389 for client in self.module.interfaces: | 388 for client in self.module.interfaces: |
| 390 if client.name == interface.client: | 389 if client.name == interface.client: |
| 391 exports.update({"client": client}) | 390 exports.update({'client': client}) |
| 392 return exports | 391 return exports |
| 393 | 392 |
| 394 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) | 393 @UseJinja('java_templates/enum.java.tmpl', filters=java_filters) |
| 395 def GenerateEnumSource(self, enum): | 394 def GenerateEnumSource(self, enum): |
| 396 exports = self.GetJinjaExports() | 395 exports = self.GetJinjaExports() |
| 397 exports.update({"enum": enum}) | 396 exports.update({'enum': enum}) |
| 398 return exports | 397 return exports |
| 399 | 398 |
| 400 @UseJinja("java_templates/struct.java.tmpl", filters=java_filters) | 399 @UseJinja('java_templates/struct.java.tmpl', filters=java_filters) |
| 401 def GenerateStructSource(self, struct): | 400 def GenerateStructSource(self, struct): |
| 402 exports = self.GetJinjaExports() | 401 exports = self.GetJinjaExports() |
| 403 exports.update({"struct": struct}) | 402 exports.update({'struct': struct}) |
| 404 return exports | 403 return exports |
| 405 | 404 |
| 406 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) | 405 @UseJinja('java_templates/interface.java.tmpl', filters=java_filters) |
| 407 def GenerateInterfaceSource(self, interface): | 406 def GenerateInterfaceSource(self, interface): |
| 408 return self.GetJinjaExportsForInterface(interface) | 407 return self.GetJinjaExportsForInterface(interface) |
| 409 | 408 |
| 410 @UseJinja("java_templates/interface_internal.java.tmpl", filters=java_filters) | 409 @UseJinja('java_templates/interface_internal.java.tmpl', filters=java_filters) |
| 411 def GenerateInterfaceInternalSource(self, interface): | 410 def GenerateInterfaceInternalSource(self, interface): |
| 412 return self.GetJinjaExportsForInterface(interface) | 411 return self.GetJinjaExportsForInterface(interface) |
| 413 | 412 |
| 414 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) | 413 @UseJinja('java_templates/constants.java.tmpl', filters=java_filters) |
| 415 def GenerateConstantsSource(self, module): | 414 def GenerateConstantsSource(self, module): |
| 416 exports = self.GetJinjaExports() | 415 exports = self.GetJinjaExports() |
| 417 exports.update({"main_entity": GetConstantsMainEntityName(module), | 416 exports.update({'main_entity': GetConstantsMainEntityName(module), |
| 418 "constants": module.constants}) | 417 'constants': module.constants}) |
| 419 return exports | 418 return exports |
| 420 | 419 |
| 421 def GenerateFiles(self, unparsed_args): | 420 def GenerateFiles(self, unparsed_args): |
| 422 parser = argparse.ArgumentParser() | 421 parser = argparse.ArgumentParser() |
| 423 parser.add_argument("--java_output_directory", dest="java_output_directory") | 422 parser.add_argument('--java_output_directory', dest='java_output_directory') |
| 424 args = parser.parse_args(unparsed_args) | 423 args = parser.parse_args(unparsed_args) |
| 425 if self.output_dir and args.java_output_directory: | 424 if self.output_dir and args.java_output_directory: |
| 426 self.output_dir = os.path.join(args.java_output_directory, | 425 self.output_dir = os.path.join(args.java_output_directory, |
| 427 GetPackage(self.module).replace('.', '/')) | 426 GetPackage(self.module).replace('.', '/')) |
| 428 if not os.path.exists(self.output_dir): | 427 if not os.path.exists(self.output_dir): |
| 429 try: | 428 try: |
| 430 os.makedirs(self.output_dir) | 429 os.makedirs(self.output_dir) |
| 431 except: | 430 except: |
| 432 # Ignore errors on directory creation. | 431 # Ignore errors on directory creation. |
| 433 pass | 432 pass |
| 434 | 433 |
| 435 for enum in self.module.enums: | 434 for enum in self.module.enums: |
| 436 self.Write(self.GenerateEnumSource(enum), | 435 self.Write(self.GenerateEnumSource(enum), |
| 437 "%s.java" % GetNameForElement(enum)) | 436 '%s.java' % GetNameForElement(enum)) |
| 438 | 437 |
| 439 for struct in self.module.structs: | 438 for struct in self.module.structs: |
| 440 self.Write(self.GenerateStructSource(struct), | 439 self.Write(self.GenerateStructSource(struct), |
| 441 "%s.java" % GetNameForElement(struct)) | 440 '%s.java' % GetNameForElement(struct)) |
| 442 | 441 |
| 443 for interface in self.module.interfaces: | 442 for interface in self.module.interfaces: |
| 444 self.Write(self.GenerateInterfaceSource(interface), | 443 self.Write(self.GenerateInterfaceSource(interface), |
| 445 "%s.java" % GetNameForElement(interface)) | 444 '%s.java' % GetNameForElement(interface)) |
| 446 self.Write(self.GenerateInterfaceInternalSource(interface), | 445 self.Write(self.GenerateInterfaceInternalSource(interface), |
| 447 "%s_Internal.java" % GetNameForElement(interface)) | 446 '%s_Internal.java' % GetNameForElement(interface)) |
| 448 | 447 |
| 449 if self.module.constants: | 448 if self.module.constants: |
| 450 self.Write(self.GenerateConstantsSource(self.module), | 449 self.Write(self.GenerateConstantsSource(self.module), |
| 451 "%s.java" % GetConstantsMainEntityName(self.module)) | 450 '%s.java' % GetConstantsMainEntityName(self.module)) |
| 452 | 451 |
| 453 def GetJinjaParameters(self): | 452 def GetJinjaParameters(self): |
| 454 return { | 453 return { |
| 455 'lstrip_blocks': True, | 454 'lstrip_blocks': True, |
| 456 'trim_blocks': True, | 455 'trim_blocks': True, |
| 457 } | 456 } |
| 458 | 457 |
| 459 def GetGlobals(self): | 458 def GetGlobals(self): |
| 460 return { | 459 return { |
| 460 'namespace': self.module.namespace, |
| 461 'module': self.module, | 461 'module': self.module, |
| 462 } | 462 } |
| OLD | NEW |