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

Unified Diff: mojo/public/tools/bindings/pylib/mojom/generate/module.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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/pylib/mojom/generate/module.py
diff --git a/mojo/public/tools/bindings/pylib/mojom/generate/module.py b/mojo/public/tools/bindings/pylib/mojom/generate/module.py
index 906d3fde279d4e9546e72e181915c4454157f599..c8e17b7b707192169b93eceb85450ca4271d5287 100644
--- a/mojo/public/tools/bindings/pylib/mojom/generate/module.py
+++ b/mojo/public/tools/bindings/pylib/mojom/generate/module.py
@@ -12,7 +12,6 @@
# method = interface.AddMethod('Tat', 0)
# method.AddParameter('baz', 0, mojom.INT32)
-
# We use our own version of __repr__ when displaying the AST, as the
# AST currently doesn't capture which nodes are reference (e.g. to
# types) and which nodes are definitions. This allows us to e.g. print
@@ -83,7 +82,7 @@ class Kind(object):
Attributes:
spec: A string uniquely identifying the type. May be None.
module: {Module} The defining module. Set to None for built-in types.
- parent_kind: The enclosing type. For example, a struct defined
+ parent_kind: The enclosing type. For example, an enum defined
inside an interface has that interface as its parent. May be None.
"""
def __init__(self, spec=None, module=None):
@@ -225,15 +224,15 @@ ATTRIBUTE_SYNC = 'Sync'
class NamedValue(object):
- def __init__(self, module, parent_kind, name):
+ def __init__(self, module, parent_kind, mojom_name):
self.module = module
self.parent_kind = parent_kind
- self.name = name
+ self.mojom_name = mojom_name
def GetSpec(self):
- return (self.module.namespace + '.' +
- (self.parent_kind and (self.parent_kind.name + '.') or "") +
- self.name)
+ return (self.module.mojom_namespace + '.' +
+ (self.parent_kind and (self.parent_kind.mojom_name + '.') or "") +
+ self.mojom_name)
class BuiltinValue(object):
@@ -243,35 +242,47 @@ class BuiltinValue(object):
class ConstantValue(NamedValue):
def __init__(self, module, parent_kind, constant):
- NamedValue.__init__(self, module, parent_kind, constant.name)
+ NamedValue.__init__(self, module, parent_kind, constant.mojom_name)
self.constant = constant
+ @property
+ def name(self):
+ return self.constant.name
+
class EnumValue(NamedValue):
def __init__(self, module, enum, field):
- NamedValue.__init__(self, module, enum.parent_kind, field.name)
+ NamedValue.__init__(self, module, enum.parent_kind, field.mojom_name)
+ self.field = field
self.enum = enum
def GetSpec(self):
- return (self.module.namespace + '.' +
- (self.parent_kind and (self.parent_kind.name + '.') or "") +
- self.enum.name + '.' + self.name)
+ return (self.module.mojom_namespace + '.' +
+ (self.parent_kind and (self.parent_kind.mojom_name + '.') or "") +
+ self.enum.mojom_name + '.' + self.mojom_name)
+
+ @property
+ def name(self):
+ return self.field.name
class Constant(object):
- def __init__(self, name=None, kind=None, value=None, parent_kind=None):
- self.name = name
+ def __init__(self, mojom_name=None, kind=None, value=None, parent_kind=None):
+ self.mojom_name = mojom_name
self.kind = kind
self.value = value
self.parent_kind = parent_kind
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeConstant(self.mojom_name)
+
class Field(object):
- def __init__(self, name=None, kind=None, ordinal=None, default=None,
+ def __init__(self, mojom_name=None, kind=None, ordinal=None, default=None,
attributes=None):
if self.__class__.__name__ == 'Field':
raise Exception()
- self.name = name
+ self.mojom_name = mojom_name
self.kind = kind
self.ordinal = ordinal
self.default = default
@@ -280,7 +291,10 @@ class Field(object):
def Repr(self, as_ref=True):
# Fields are only referenced by objects which define them and thus
# they are always displayed as non-references.
- return GenericRepr(self, {'name': False, 'kind': True})
+ return GenericRepr(self, {'mojom_name': False, 'kind': True})
+
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeField(self.mojom_name)
@property
def min_version(self):
@@ -298,65 +312,86 @@ class Struct(ReferenceKind):
"""A struct with typed fields.
Attributes:
- name: {str} The name of the struct type.
+ mojom_name: {str} The name of the struct type as defined in mojom.
+ name: {str} The stylized name.
native_only: {bool} Does the struct have a body (i.e. any fields) or is it
purely a native struct.
fields: {List[StructField]} The members of the struct.
+ enums: {List[Enum]} The enums defined in the struct scope.
+ constants: {List[Constant]} The constants defined in the struct scope.
attributes: {dict} Additional information about the struct, such as
if it's a native struct.
"""
+ ReferenceKind.AddSharedProperty('mojom_name')
ReferenceKind.AddSharedProperty('name')
ReferenceKind.AddSharedProperty('native_only')
ReferenceKind.AddSharedProperty('fields')
+ ReferenceKind.AddSharedProperty('enums')
+ ReferenceKind.AddSharedProperty('constants')
ReferenceKind.AddSharedProperty('attributes')
- def __init__(self, name=None, module=None, attributes=None):
- if name is not None:
- spec = 'x:' + name
+ def __init__(self, mojom_name=None, module=None, attributes=None):
+ if mojom_name is not None:
+ spec = 'x:' + mojom_name
else:
spec = None
ReferenceKind.__init__(self, spec, False, module)
- self.name = name
+ self.mojom_name = mojom_name
self.native_only = False
self.fields = []
+ self.enums = []
+ self.constants = []
self.attributes = attributes
def Repr(self, as_ref=True):
if as_ref:
- return '<%s name=%r module=%s>' % (
- self.__class__.__name__, self.name,
+ return '<%s mojom_name=%r module=%s>' % (
+ self.__class__.__name__, self.mojom_name,
Repr(self.module, as_ref=True))
else:
- return GenericRepr(self, {'name': False, 'fields': False, 'module': True})
+ return GenericRepr(self,
+ {'mojom_name': False, 'fields': False, 'module': True})
- def AddField(self, name, kind, ordinal=None, default=None, attributes=None):
- field = StructField(name, kind, ordinal, default, attributes)
+ def AddField(self, mojom_name, kind, ordinal=None, default=None,
+ attributes=None):
+ field = StructField(mojom_name, kind, ordinal, default, attributes)
self.fields.append(field)
return field
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeStruct(self.mojom_name)
+ for field in self.fields:
+ field.Stylize(stylizer)
+ for enum in self.enums:
+ enum.Stylize(stylizer)
+ for constant in self.constants:
+ constant.Stylize(stylizer)
+
class Union(ReferenceKind):
"""A union of several kinds.
Attributes:
- name: {str} The name of the union type.
+ mojom_name: {str} The name of the union type as defined in mojom.
+ name: {str} The stylized name.
fields: {List[UnionField]} The members of the union.
attributes: {dict} Additional information about the union, such as
which Java class name to use to represent it in the generated
bindings.
"""
+ ReferenceKind.AddSharedProperty('mojom_name')
ReferenceKind.AddSharedProperty('name')
ReferenceKind.AddSharedProperty('fields')
ReferenceKind.AddSharedProperty('attributes')
- def __init__(self, name=None, module=None, attributes=None):
- if name is not None:
- spec = 'x:' + name
+ def __init__(self, mojom_name=None, module=None, attributes=None):
+ if mojom_name is not None:
+ spec = 'x:' + mojom_name
else:
spec = None
ReferenceKind.__init__(self, spec, False, module)
- self.name = name
+ self.mojom_name = mojom_name
self.fields = []
self.attributes = attributes
@@ -368,11 +403,16 @@ class Union(ReferenceKind):
else:
return GenericRepr(self, {'fields': True, 'is_nullable': False})
- def AddField(self, name, kind, ordinal=None, attributes=None):
- field = UnionField(name, kind, ordinal, None, attributes)
+ def AddField(self, mojom_name, kind, ordinal=None, attributes=None):
+ field = UnionField(mojom_name, kind, ordinal, None, attributes)
self.fields.append(field)
return field
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeUnion(self.mojom_name)
+ for field in self.fields:
+ field.Stylize(stylizer)
+
class Array(ReferenceKind):
"""An array.
@@ -477,17 +517,20 @@ class AssociatedInterfaceRequest(ReferenceKind):
class Parameter(object):
- def __init__(self, name=None, kind=None, ordinal=None, default=None,
+ def __init__(self, mojom_name=None, kind=None, ordinal=None, default=None,
attributes=None):
- self.name = name
+ self.mojom_name = mojom_name
self.ordinal = ordinal
self.kind = kind
self.default = default
self.attributes = attributes
def Repr(self, as_ref=True):
- return '<%s name=%r kind=%s>' % (self.__class__.__name__, self.name,
- self.kind.Repr(as_ref=True))
+ return '<%s mojom_name=%r kind=%s>' % (
+ self.__class__.__name__, self.mojom_name, self.kind.Repr(as_ref=True))
+
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeParameter(self.mojom_name)
@property
def min_version(self):
@@ -496,35 +539,50 @@ class Parameter(object):
class Method(object):
- def __init__(self, interface, name, ordinal=None, attributes=None):
+ def __init__(self, interface, mojom_name, ordinal=None, attributes=None):
self.interface = interface
- self.name = name
+ self.mojom_name = mojom_name
self.ordinal = ordinal
self.parameters = []
+ self.param_struct = None
self.response_parameters = None
+ self.response_param_struct = None
self.attributes = attributes
def Repr(self, as_ref=True):
if as_ref:
- return '<%s name=%r>' % (self.__class__.__name__, self.name)
+ return '<%s mojom_name=%r>' % (self.__class__.__name__, self.mojom_name)
else:
- return GenericRepr(self, {'name': False, 'parameters': True,
+ return GenericRepr(self, {'mojom_name': False, 'parameters': True,
'response_parameters': True})
- def AddParameter(self, name, kind, ordinal=None, default=None,
+ def AddParameter(self, mojom_name, kind, ordinal=None, default=None,
attributes=None):
- parameter = Parameter(name, kind, ordinal, default, attributes)
+ parameter = Parameter(mojom_name, kind, ordinal, default, attributes)
self.parameters.append(parameter)
return parameter
- def AddResponseParameter(self, name, kind, ordinal=None, default=None,
+ def AddResponseParameter(self, mojom_name, kind, ordinal=None, default=None,
attributes=None):
if self.response_parameters == None:
self.response_parameters = []
- parameter = Parameter(name, kind, ordinal, default, attributes)
+ parameter = Parameter(mojom_name, kind, ordinal, default, attributes)
self.response_parameters.append(parameter)
return parameter
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeMethod(self.mojom_name)
+ for param in self.parameters:
+ param.Stylize(stylizer)
+ if self.response_parameters is not None:
+ for param in self.response_parameters:
+ param.Stylize(stylizer)
+
+ if self.param_struct:
+ self.param_struct.Stylize(stylizer)
+ if self.response_param_struct:
+ self.response_param_struct.Stylize(stylizer)
+
@property
def min_version(self):
return self.attributes.get(ATTRIBUTE_MIN_VERSION) \
@@ -537,36 +595,45 @@ class Method(object):
class Interface(ReferenceKind):
+ ReferenceKind.AddSharedProperty('mojom_name')
ReferenceKind.AddSharedProperty('name')
ReferenceKind.AddSharedProperty('methods')
+ ReferenceKind.AddSharedProperty('enums')
+ ReferenceKind.AddSharedProperty('constants')
ReferenceKind.AddSharedProperty('attributes')
- def __init__(self, name=None, module=None, attributes=None):
- if name is not None:
- spec = 'x:' + name
+ def __init__(self, mojom_name=None, module=None, attributes=None):
+ if mojom_name is not None:
+ spec = 'x:' + mojom_name
else:
spec = None
ReferenceKind.__init__(self, spec, False, module)
- self.name = name
+ self.mojom_name = mojom_name
self.methods = []
+ self.enums = []
+ self.constants = []
self.attributes = attributes
def Repr(self, as_ref=True):
if as_ref:
- return '<%s name=%r>' % (self.__class__.__name__, self.name)
+ return '<%s mojom_name=%r>' % (self.__class__.__name__, self.mojom_name)
else:
- return GenericRepr(self, {'name': False, 'attributes': False,
+ return GenericRepr(self, {'mojom_name': False, 'attributes': False,
'methods': False})
- def AddMethod(self, name, ordinal=None, attributes=None):
- method = Method(self, name, ordinal, attributes)
+ def AddMethod(self, mojom_name, ordinal=None, attributes=None):
+ method = Method(self, mojom_name, ordinal, attributes)
self.methods.append(method)
return method
- # TODO(451323): Remove when the language backends no longer rely on this.
- @property
- def client(self):
- return None
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeInterface(self.mojom_name)
+ for method in self.methods:
+ method.Stylize(stylizer)
+ for enum in self.enums:
+ enum.Stylize(stylizer)
+ for constant in self.constants:
+ constant.Stylize(stylizer)
class AssociatedInterface(ReferenceKind):
@@ -585,13 +652,16 @@ class AssociatedInterface(ReferenceKind):
class EnumField(object):
- def __init__(self, name=None, value=None, attributes=None,
+ def __init__(self, mojom_name=None, value=None, attributes=None,
numeric_value=None):
- self.name = name
+ self.mojom_name = mojom_name
self.value = value
self.attributes = attributes
self.numeric_value = numeric_value
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeEnumField(self.mojom_name)
+
@property
def min_version(self):
return self.attributes.get(ATTRIBUTE_MIN_VERSION) \
@@ -599,11 +669,11 @@ class EnumField(object):
class Enum(Kind):
- def __init__(self, name=None, module=None, attributes=None):
- self.name = name
+ def __init__(self, mojom_name=None, module=None, attributes=None):
+ self.mojom_name = mojom_name
self.native_only = False
- if name is not None:
- spec = 'x:' + name
+ if mojom_name is not None:
+ spec = 'x:' + mojom_name
else:
spec = None
Kind.__init__(self, spec, module)
@@ -612,9 +682,14 @@ class Enum(Kind):
def Repr(self, as_ref=True):
if as_ref:
- return '<%s name=%r>' % (self.__class__.__name__, self.name)
+ return '<%s mojom_name=%r>' % (self.__class__.__name__, self.mojom_name)
else:
- return GenericRepr(self, {'name': False, 'fields': False})
+ return GenericRepr(self, {'mojom_name': False, 'fields': False})
+
+ def Stylize(self, stylizer):
+ self.name = stylizer.StylizeEnum(self.mojom_name)
+ for field in self.fields:
+ field.Stylize(stylizer)
@property
def extensible(self):
@@ -623,14 +698,18 @@ class Enum(Kind):
class Module(object):
- def __init__(self, path=None, namespace=None, attributes=None):
+ def __init__(self, path=None, mojom_namespace=None,
+ attributes=None):
self.path = path
- self.namespace = namespace
+ self.mojom_namespace = mojom_namespace
self.structs = []
self.unions = []
self.interfaces = []
+ self.enums = []
+ self.constants = []
self.kinds = {}
self.attributes = attributes
+ self.imports = []
def __repr__(self):
# Gives us a decent __repr__ for modules.
@@ -638,28 +717,44 @@ class Module(object):
def Repr(self, as_ref=True):
if as_ref:
- return '<%s path=%r namespace=%r>' % (
- self.__class__.__name__, self.path, self.namespace)
+ return '<%s path=%r mojom_namespace=%r>' % (
+ self.__class__.__name__, self.path, self.mojom_namespace)
else:
- return GenericRepr(self, {'path': False, 'namespace': False,
+ return GenericRepr(self, {'path': False, 'mojom_namespace': False,
'attributes': False, 'structs': False,
'interfaces': False, 'unions': False})
- def AddInterface(self, name, attributes=None):
- interface = Interface(name, self, attributes)
+ def AddInterface(self, mojom_name, attributes=None):
+ interface = Interface(mojom_name, self, attributes)
self.interfaces.append(interface)
return interface
- def AddStruct(self, name, attributes=None):
- struct = Struct(name, self, attributes)
+ def AddStruct(self, mojom_name, attributes=None):
+ struct = Struct(mojom_name, self, attributes)
self.structs.append(struct)
return struct
- def AddUnion(self, name, attributes=None):
- union = Union(name, self, attributes)
+ def AddUnion(self, mojom_name, attributes=None):
+ union = Union(mojom_name, self, attributes)
self.unions.append(union)
return union
+ def Stylize(self, stylizer):
+ self.namespace = stylizer.StylizeModule(self.mojom_namespace)
+ for struct in self.structs:
+ struct.Stylize(stylizer)
+ for union in self.unions:
+ union.Stylize(stylizer)
+ for interface in self.interfaces:
+ interface.Stylize(stylizer)
+ for enum in self.enums:
+ enum.Stylize(stylizer)
+ for constant in self.constants:
+ constant.Stylize(stylizer)
+
+ for imported_module in self.imports:
+ imported_module.Stylize(stylizer)
+
def IsBoolKind(kind):
return kind.spec == BOOL.spec

Powered by Google App Engine
This is Rietveld 408576698