Upstream version 9.38.198.0
[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 import re
10
11 import module as mojom
12 import pack
13
14 def GetStructFromMethod(method):
15   """Converts a method's parameters into the fields of a struct."""
16   params_class = "%s_%s_Params" % (method.interface.name, method.name)
17   struct = mojom.Struct(params_class, module=method.interface.module)
18   for param in method.parameters:
19     struct.AddField(param.name, param.kind, param.ordinal)
20   struct.packed = pack.PackedStruct(struct)
21   return struct
22
23 def GetResponseStructFromMethod(method):
24   """Converts a method's response_parameters into the fields of a struct."""
25   params_class = "%s_%s_ResponseParams" % (method.interface.name, method.name)
26   struct = mojom.Struct(params_class, module=method.interface.module)
27   for param in method.response_parameters:
28     struct.AddField(param.name, param.kind, param.ordinal)
29   struct.packed = pack.PackedStruct(struct)
30   return struct
31
32 def GetDataHeader(exported, struct):
33   struct.packed = pack.PackedStruct(struct)
34   struct.bytes = pack.GetByteLayout(struct.packed)
35   struct.exported = exported
36   return struct
37
38 def ExpectedArraySize(kind):
39   if mojom.IsFixedArrayKind(kind):
40     return kind.length
41   return 0
42
43 def StudlyCapsToCamel(studly):
44   return studly[0].lower() + studly[1:]
45
46 def CamelCaseToAllCaps(camel_case):
47   return '_'.join(
48       word for word in re.split(r'([A-Z][^A-Z]+)', camel_case) if word).upper()
49
50 class Generator(object):
51   # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all
52   # files to stdout.
53   def __init__(self, module, output_dir=None):
54     self.module = module
55     self.output_dir = output_dir
56
57   def GetStructsFromMethods(self):
58     result = []
59     for interface in self.module.interfaces:
60       for method in interface.methods:
61         result.append(GetStructFromMethod(method))
62         if method.response_parameters != None:
63           result.append(GetResponseStructFromMethod(method))
64     return map(partial(GetDataHeader, False), result)
65
66   def GetStructs(self):
67     return map(partial(GetDataHeader, True), self.module.structs)
68
69   def Write(self, contents, filename):
70     if self.output_dir is None:
71       print contents
72       return
73     with open(os.path.join(self.output_dir, filename), "w+") as f:
74       f.write(contents)
75
76   def GenerateFiles(self, args):
77     raise NotImplementedError("Subclasses must override/implement this method")
78
79   def GetJinjaParameters(self):
80     """Returns default constructor parameters for the jinja environment."""
81     return {}
82
83   def GetGlobals(self):
84     """Returns global mappings for the template generation."""
85     return {}