707028568c3ccea4ee07c49e45fa6c2a6ee698d0
[platform/framework/web/crosswalk.git] / src / mojo / public / tools / bindings / pylib / mojom / generate / generator.py
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Code shared by the various language-specific code generators."""
6
7 from functools import partial
8 import os.path
9
10 import module as mojom
11 import pack
12
13 def GetStructFromMethod(method):
14   """Converts a method's parameters into the fields of a struct."""
15   params_class = "%s_%s_Params" % (method.interface.name, method.name)
16   struct = mojom.Struct(params_class, module=method.interface.module)
17   for param in method.parameters:
18     struct.AddField(param.name, param.kind, param.ordinal)
19   struct.packed = pack.PackedStruct(struct)
20   return struct
21
22 def GetResponseStructFromMethod(method):
23   """Converts a method's response_parameters into the fields of a struct."""
24   params_class = "%s_%s_ResponseParams" % (method.interface.name, method.name)
25   struct = mojom.Struct(params_class, module=method.interface.module)
26   for param in method.response_parameters:
27     struct.AddField(param.name, param.kind, param.ordinal)
28   struct.packed = pack.PackedStruct(struct)
29   return struct
30
31 def GetDataHeader(exported, struct):
32   struct.packed = pack.PackedStruct(struct)
33   struct.bytes = pack.GetByteLayout(struct.packed)
34   struct.exported = exported
35   return struct
36
37 def IsStringKind(kind):
38   return kind.spec == 's'
39
40 def IsEnumKind(kind):
41   return isinstance(kind, mojom.Enum)
42
43 def IsObjectKind(kind):
44   return isinstance(kind, (mojom.Struct, mojom.Array)) or IsStringKind(kind)
45
46 def IsHandleKind(kind):
47   return kind.spec.startswith('h') or \
48          isinstance(kind, mojom.Interface) or \
49          isinstance(kind, mojom.InterfaceRequest)
50
51 def IsInterfaceKind(kind):
52   return isinstance(kind, mojom.Interface)
53
54 def IsInterfaceRequestKind(kind):
55   return isinstance(kind, mojom.InterfaceRequest)
56
57 def IsMoveOnlyKind(kind):
58   return IsObjectKind(kind) or IsHandleKind(kind)
59
60 def StudlyCapsToCamel(studly):
61   return studly[0].lower() + studly[1:]
62
63 class Generator(object):
64   # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all
65   # files to stdout.
66   def __init__(self, module, output_dir=None):
67     self.module = module
68     self.output_dir = output_dir
69
70   def GetStructsFromMethods(self):
71     result = []
72     for interface in self.module.interfaces:
73       for method in interface.methods:
74         result.append(GetStructFromMethod(method))
75         if method.response_parameters != None:
76           result.append(GetResponseStructFromMethod(method))
77     return map(partial(GetDataHeader, False), result)
78
79   def GetStructs(self):
80     return map(partial(GetDataHeader, True), self.module.structs)
81
82   def Write(self, contents, filename):
83     if self.output_dir is None:
84       print contents
85       return
86     with open(os.path.join(self.output_dir, filename), "w+") as f:
87       f.write(contents)
88
89   def GenerateFiles(self, args):
90     raise NotImplementedError("Subclasses must override/implement this method")