Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env python
      2 # Copyright (C) 2013 Google Inc. All rights reserved.
      3 #
      4 # Redistribution and use in source and binary forms, with or without
      5 # modification, are permitted provided that the following conditions are
      6 # met:
      7 #
      8 #     * Redistributions of source code must retain the above copyright
      9 # notice, this list of conditions and the following disclaimer.
     10 #     * Redistributions in binary form must reproduce the above
     11 # copyright notice, this list of conditions and the following disclaimer
     12 # in the documentation and/or other materials provided with the
     13 # distribution.
     14 #     * Neither the name of Google Inc. nor the names of its
     15 # contributors may be used to endorse or promote products derived from
     16 # this software without specific prior written permission.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     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.
     29 
     30 import os.path
     31 import sys
     32 import shutil
     33 
     34 from in_file import InFile
     35 import name_macros
     36 import license
     37 
     38 
     39 IMPLEMENTATION_TEMPLATE = """%(license)s
     40 #include "config.h"
     41 #include "%(class_name)sFactory.h"
     42 
     43 #include "%(class_name)sHeaders.h"
     44 #include "RuntimeEnabledFeatures.h"
     45 
     46 namespace WebCore {
     47 
     48 PassRefPtr<%(class_name)s> %(class_name)sFactory::create(const String& type)
     49 {
     50 %(factory_implementation)s
     51     return 0;
     52 }
     53 
     54 } // namespace WebCore
     55 """
     56 
     57 
     58 class EventFactoryWriter(name_macros.Writer):
     59     defaults = {
     60         'ImplementedAs': None,
     61         'Conditional': None,
     62         'EnabledAtRuntime': None,
     63     }
     64     default_parameters = {
     65         'namespace': '',
     66     }
     67 
     68     def __init__(self, in_file_path, enabled_conditions):
     69         super(EventFactoryWriter, self).__init__(in_file_path, enabled_conditions)
     70         self._outputs[(self.class_name + ".cpp")] = self.generate_implementation
     71 
     72     def _events(self):
     73         return self.in_file.name_dictionaries
     74 
     75     def _factory_implementation(self, event):
     76         runtime_condition = ''
     77         if event['EnabledAtRuntime']:
     78             runtime_condition = ' && RuntimeEnabledFeatures::' + event['EnabledAtRuntime'] + '()'
     79         name = os.path.basename(event['name'])
     80         class_name = self._class_name_for_entry(event)
     81         implementation = """    if (type == "%(name)s"%(runtime_condition)s)
     82         return %(class_name)s::create();""" % {
     83             'name': name,
     84             'runtime_condition': runtime_condition,
     85             'class_name': class_name,
     86         }
     87         return self.wrap_with_condition(implementation, event['Conditional'])
     88 
     89     def generate_implementation(self):
     90         return IMPLEMENTATION_TEMPLATE % {
     91             'class_name': self.class_name,
     92             'license': license.license_for_generated_cpp(),
     93             'factory_implementation': "\n".join(map(self._factory_implementation, self._events())),
     94         }
     95 
     96 
     97 if __name__ == "__main__":
     98     name_macros.Maker(EventFactoryWriter).main(sys.argv)
     99