| Index: mojo/public/tools/bindings/generators/mojom_python_generator.py
|
| diff --git a/mojo/public/tools/bindings/generators/mojom_python_generator.py b/mojo/public/tools/bindings/generators/mojom_python_generator.py
|
| index db5e8b696b06e1c2f5e3578e1c81091638c9c4bc..295c656f4c5bf742a1f33b4a584c8bd42c288606 100644
|
| --- a/mojo/public/tools/bindings/generators/mojom_python_generator.py
|
| +++ b/mojo/public/tools/bindings/generators/mojom_python_generator.py
|
| @@ -90,9 +90,13 @@ def ConstantStyle(name):
|
| components = components[1:]
|
| return '_'.join([x.upper() for x in components])
|
|
|
| +def FieldStyle(name):
|
| + components = NameToComponent(name)
|
| + return '_'.join([x.lower() for x in components])
|
| +
|
| def GetNameForElement(element):
|
| if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element) or
|
| - mojom.IsStructKind(element)):
|
| + mojom.IsStructKind(element) or isinstance(element, mojom.Method)):
|
| return UpperCamelCase(element.name)
|
| if isinstance(element, mojom.EnumValue):
|
| return (GetNameForElement(element.enum) + '.' +
|
| @@ -100,7 +104,9 @@ def GetNameForElement(element):
|
| if isinstance(element, (mojom.NamedValue,
|
| mojom.Constant)):
|
| return ConstantStyle(element.name)
|
| - raise Exception('Unexpected element: ' % element)
|
| + if isinstance(element, mojom.Field):
|
| + return FieldStyle(element.name)
|
| + raise Exception('Unexpected element: %s' % element)
|
|
|
| def ExpressionToText(token):
|
| if isinstance(token, (mojom.EnumValue, mojom.NamedValue)):
|
| @@ -120,7 +126,7 @@ def ExpressionToText(token):
|
|
|
| return token
|
|
|
| -def GetStructClass(kind):
|
| +def GetFullyQualifiedName(kind):
|
| name = []
|
| if kind.imported_from:
|
| name.append(kind.imported_from['python_module'])
|
| @@ -155,7 +161,7 @@ def GetFieldType(kind, field=None):
|
| return '_descriptor.MapType(%s)' % ', '.join(arguments)
|
|
|
| if mojom.IsStructKind(kind):
|
| - arguments = [ 'lambda: %s' % GetStructClass(kind) ]
|
| + arguments = [ 'lambda: %s' % GetFullyQualifiedName(kind) ]
|
| if mojom.IsNullableKind(kind):
|
| arguments.append('nullable=True')
|
| return '_descriptor.StructType(%s)' % ', '.join(arguments)
|
| @@ -170,7 +176,7 @@ def GetFieldDescriptor(packed_field):
|
| class_name = 'SingleFieldGroup'
|
| if field.kind == mojom.BOOL:
|
| class_name = 'FieldDescriptor'
|
| - arguments = [ '%r' % field.name ]
|
| + arguments = [ '%r' % GetNameForElement(field) ]
|
| arguments.append(GetFieldType(field.kind, field))
|
| arguments.append(str(packed_field.index))
|
| arguments.append(str(packed_field.ordinal))
|
| @@ -182,12 +188,20 @@ def GetFieldDescriptor(packed_field):
|
| return '_descriptor.%s(%s)' % (class_name, ', '.join(arguments))
|
|
|
| def GetFieldGroup(byte):
|
| - if len(byte.packed_fields) > 1:
|
| + if byte.packed_fields[0].field.kind == mojom.BOOL:
|
| descriptors = map(GetFieldDescriptor, byte.packed_fields)
|
| return '_descriptor.BooleanGroup([%s])' % ', '.join(descriptors)
|
| assert len(byte.packed_fields) == 1
|
| return GetFieldDescriptor(byte.packed_fields[0])
|
|
|
| +def GetResponseStructFromMethod(method):
|
| + return generator.GetDataHeader(
|
| + False, generator.GetResponseStructFromMethod(method))
|
| +
|
| +def GetStructFromMethod(method):
|
| + return generator.GetDataHeader(
|
| + False, generator.GetStructFromMethod(method))
|
| +
|
| def ComputeStaticValues(module):
|
| in_progress = set()
|
| computed = set()
|
| @@ -274,14 +288,18 @@ class Generator(generator.Generator):
|
| python_filters = {
|
| 'expression_to_text': ExpressionToText,
|
| 'field_group': GetFieldGroup,
|
| + 'fully_qualified_name': GetFullyQualifiedName,
|
| 'name': GetNameForElement,
|
| + 'response_struct_from_method': GetResponseStructFromMethod,
|
| + 'struct_from_method': GetStructFromMethod,
|
| }
|
|
|
| @UseJinja('python_templates/module.py.tmpl', filters=python_filters)
|
| def GeneratePythonModule(self):
|
| return {
|
| - 'imports': self.GetImports(),
|
| 'enums': self.module.enums,
|
| + 'imports': self.GetImports(),
|
| + 'interfaces': self.GetQualifiedInterfaces(),
|
| 'module': ComputeStaticValues(self.module),
|
| 'structs': self.GetStructs(),
|
| }
|
| @@ -296,6 +314,25 @@ class Generator(generator.Generator):
|
| each['python_module'] = MojomToPythonImport(each['module_name'])
|
| return self.module.imports
|
|
|
| + def GetQualifiedInterfaces(self):
|
| + """
|
| + Returns the list of interfaces of the module. Each interface that has a
|
| + client will have a reference to the representation of the client interface
|
| + in the 'qualified_client' field.
|
| + """
|
| + interfaces = self.module.interfaces
|
| + all_interfaces = [] + interfaces
|
| + for each in self.module.imports:
|
| + all_interfaces += each['module'].interfaces
|
| + interfaces_by_name = dict((x.name, x) for x in all_interfaces)
|
| + for interface in interfaces:
|
| + if interface.client:
|
| + assert interface.client in interfaces_by_name, (
|
| + 'Unable to find interface %s declared as client of %s.' %
|
| + (interface.client, interface.name))
|
| + interface.qualified_client = interfaces_by_name[interface.client]
|
| + return sorted(interfaces, key=lambda i: (bool(i.client), i.name))
|
| +
|
| def GetJinjaParameters(self):
|
| return {
|
| 'lstrip_blocks': True,
|
|
|