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

Side by Side Diff: Source/core/scripts/make_event_factory.py

Issue 14905002: Use jinja2 templating engine for Python code generators (Closed) Base URL: https://chromium.googlesource.com/chromium/blink@master
Patch Set: Created 7 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 unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (C) 2013 Google Inc. All rights reserved. 2 # Copyright (C) 2013 Google Inc. All rights reserved.
3 # 3 #
4 # Redistribution and use in source and binary forms, with or without 4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are 5 # modification, are permitted provided that the following conditions are
6 # met: 6 # met:
7 # 7 #
8 # * Redistributions of source code must retain the above copyright 8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer. 9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above 10 # * Redistributions in binary form must reproduce the above
(...skipping 16 matching lines...) Expand all
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 29
30 import os.path 30 import os.path
31 import sys 31 import sys
32 import shutil 32 import shutil
33 33
34 from in_file import InFile 34 from in_file import InFile
35 import in_generator 35 import in_generator
36 import license 36 import license
37 37 import template_expander
38
39 IMPLEMENTATION_TEMPLATE = """%(license)s
40 #include "config.h"
41 #include "%(namespace)sFactory.h"
42
43 #include "%(namespace)sHeaders.h"
44 #include "RuntimeEnabledFeatures.h"
45
46 namespace WebCore {
47
48 PassRefPtr<%(namespace)s> %(namespace)sFactory::create(const String& type)
49 {
50 %(factory_implementation)s
51 return 0;
52 }
53
54 } // namespace WebCore
55 """
56 38
57 39
58 class EventFactoryWriter(in_generator.Writer): 40 class EventFactoryWriter(in_generator.Writer):
59 defaults = { 41 defaults = {
60 'interfaceName' : None, 42 'interfaceName' : None,
61 'conditional' : None, 43 'conditional' : None,
62 'runtimeConditional': None, 44 'runtimeConditional': None,
63 } 45 }
64 default_parameters = { 46 default_parameters = {
65 'namespace': '', 47 'namespace': '',
66 } 48 }
67 class_name = 'EventFactory' 49 class_name = 'EventFactory'
68 50
51 def __init__(self, in_file_path):
52 super(EventFactoryWriter, self).__init__(in_file_path)
53 self._events = self.in_file.name_dictionaries
54 for event in self._events:
55 event['name'] = os.path.basename(event['name'])
56 event['interfaceName'] = event['interfaceName'] or event['name']
57
69 def _namespace(self): 58 def _namespace(self):
70 return self.in_file.parameters['namespace'] 59 return self.in_file.parameters['namespace']
71 60
72 def _events(self):
73 return self.in_file.name_dictionaries
74
75 def _factory_implementation(self, event):
76 runtime_condition = ''
77 if event['runtimeConditional']:
78 runtime_condition = ' && RuntimeEnabledFeatures::' + event['runtimeC onditional'] + '()'
79 name = os.path.basename(event['name'])
80 interface_name = event['interfaceName'] if event['interfaceName'] else n ame
81 implementation = """ if (type == "%(name)s"%(runtime_condition)s)
82 return %(interface_name)s::create();""" % {
83 'name': name,
84 'runtime_condition': runtime_condition,
85 'interface_name': interface_name,
86 }
87 return self.wrap_with_condition(implementation, event['conditional'])
88
89 def generate_header(self): 61 def generate_header(self):
90 pass 62 pass
91 63
92 def generate_implementation(self): 64 def generate_implementation(self):
93 return IMPLEMENTATION_TEMPLATE % { 65 params = {
94 'namespace': self._namespace().strip('"'), 66 'namespace': self._namespace().strip('"'),
95 'license': license.license_for_generated_cpp(), 67 'license': license.license_for_generated_cpp(),
96 'factory_implementation': "\n".join(map(self._factory_implementation , self._events())), 68 'events': self._events,
97 } 69 }
98 70 return template_expander.applytemplate("../dom/EventFactory.cpp.tmpl", p arams)
abarth-chromium 2013/05/06 18:27:32 Should we receive the path to EventFactory.cpp.tmp
99 71
100 if __name__ == "__main__": 72 if __name__ == "__main__":
101 in_generator.Maker(EventFactoryWriter).main(sys.argv) 73 in_generator.Maker(EventFactoryWriter).main(sys.argv)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698