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

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/generator.py

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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 unified diff | Download patch
OLDNEW
(Empty)
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.IsArrayKind(kind):
40 return kind.length
41 return None
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 def WriteFile(contents, full_path):
51 # Make sure the containing directory exists.
52 full_dir = os.path.dirname(full_path)
53 if not os.path.exists(full_dir):
54 os.makedirs(full_dir)
55
56 # Dump the data to disk.
57 with open(full_path, "w+") as f:
58 f.write(contents)
59
60 class Generator(object):
61 # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all
62 # files to stdout.
63 def __init__(self, module, output_dir=None):
64 self.module = module
65 self.output_dir = output_dir
66
67 def GetStructsFromMethods(self):
68 result = []
69 for interface in self.module.interfaces:
70 for method in interface.methods:
71 result.append(GetStructFromMethod(method))
72 if method.response_parameters != None:
73 result.append(GetResponseStructFromMethod(method))
74 return map(partial(GetDataHeader, False), result)
75
76 def GetStructs(self):
77 return map(partial(GetDataHeader, True), self.module.structs)
78
79 # Prepend the filename with a directory that matches the directory of the
80 # original .mojom file, relative to the import root.
81 def MatchMojomFilePath(self, filename):
82 return os.path.join(os.path.dirname(self.module.path), filename)
83
84 def Write(self, contents, filename):
85 if self.output_dir is None:
86 print contents
87 return
88 full_path = os.path.join(self.output_dir, filename)
89 WriteFile(contents, full_path)
90
91 def GenerateFiles(self, args):
92 raise NotImplementedError("Subclasses must override/implement this method")
93
94 def GetJinjaParameters(self):
95 """Returns default constructor parameters for the jinja environment."""
96 return {}
97
98 def GetGlobals(self):
99 """Returns global mappings for the template generation."""
100 return {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698