generate_driver.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # ####################################################################
00004 # Copyright (c) 2013, Shadow Robot Company, All rights reserved.
00005 #
00006 # This library is free software; you can redistribute it and/or
00007 # modify it under the terms of the GNU Lesser General Public
00008 # License as published by the Free Software Foundation; either
00009 # version 3.0 of the License, or (at your option) any later version.
00010 #
00011 # This library is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 # Lesser General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU Lesser General Public
00017 # License along with this library.
00018 # ####################################################################
00019 
00020 
00021 class DriverGenerator(object):
00022     """
00023     Generates a new RoNeX module driver using the templates.
00024     """
00025 
00026     _module_name = None
00027     _product_id = None
00028     _substitutions = {}
00029 
00030     def __init__(self, module_name, product_id):
00031         print "Generating a driver skeleton for the RoNeX module[" + product_id + "]: " + module_name
00032 
00033         self._module_name = module_name
00034         self._product_id = product_id
00035 
00036         self.build_substitutions()
00037 
00038         # generates the header
00039         header_path = "../include/sr_ronex_drivers/" + \
00040                       self._substitutions["AUTOMATIC_GENERATOR_FILE_NAME"]+".hpp"
00041         self.generate_cpp_code("templates/driver.header", header_path)
00042 
00043         # generates the cpp
00044         source_path = "../src/" + \
00045                       self._substitutions["AUTOMATIC_GENERATOR_FILE_NAME"]+".cpp"
00046         self.generate_cpp_code("templates/driver.source", source_path)
00047 
00048         # add the cpp to the CMakeLists
00049         self.generate_cmake()
00050 
00051         # add the required lines to the plugin.xml
00052         self.generate_plugin_xml()
00053 
00054     def build_substitutions(self):
00055         """
00056         Creates a dictionary for the substitutions needed in the templates.
00057         """
00058         # for cpp and hpp
00059         self._substitutions["AUTOMATIC_GENERATOR_FILE_NAME"] = "sr_board_" + self._module_name.lower()
00060         self._substitutions["AUTOMATIC_GENERATOR_REPLACE_MODULE_NAME"] = self._module_name.upper()
00061         # product id without the "0x"
00062         self._substitutions["AUTOMATIC_GENERATOR_REPLACE_PRODUCT_ID"] = self._product_id[2:]
00063 
00064         # for CMake
00065         self._substitutions["#AUTOMATIC_GENERATOR_INSERT_ABOVE"] = \
00066             "src/" + self._substitutions["AUTOMATIC_GENERATOR_FILE_NAME"] +\
00067             ".cpp\n    #AUTOMATIC_GENERATOR_INSERT_ABOVE"
00068 
00069         # for ethercat_device_plugin.xml
00070         # convert the product id to int
00071         self._substitutions["AUTOMATIC_GENERATOR_PRODUCT_ID"] = str(int(self._product_id, 16))
00072         # load the xml template
00073         with open("templates/plugin.xml", "r") as template:
00074             self._substitutions["<!-- AUTOMATIC_GENERATOR_INSERT_ABOVE -->"] = template.read()
00075         # replace the different infos from the template above
00076         self._substitutions["<!-- AUTOMATIC_GENERATOR_INSERT_ABOVE -->"] = \
00077             self._substitute(self._substitutions["<!-- AUTOMATIC_GENERATOR_INSERT_ABOVE -->"]) +\
00078             "\n  <!-- AUTOMATIC_GENERATOR_INSERT_ABOVE -->"
00079 
00080     def generate_cpp_code(self, template_path, new_file_path):
00081         """
00082         Opens the cpp templates, do the proper substitutions and write the result to the correct file.
00083         """
00084         print "Generating : " + new_file_path
00085 
00086         with open(template_path, "r") as template, \
00087                 open(new_file_path, "w") as new_file:
00088             for line in template:
00089                 new_file.write(self._substitute(line))
00090 
00091     def generate_cmake(self):
00092         """
00093         Adds the cpp file to the cmake.
00094         """
00095         print "Adding src/" + self._substitutions["AUTOMATIC_GENERATOR_FILE_NAME"] + ".cpp to CMakeLists.txt"
00096 
00097         new_lines = []
00098         with open("../CMakeLists.txt", "r") as cmake_read:
00099             for line in cmake_read:
00100                 new_lines.append(self._substitute(line))
00101 
00102         with open("../CMakeLists.txt", "w") as cmake_write:
00103             cmake_write.writelines(new_lines)
00104 
00105     def generate_plugin_xml(self):
00106         """
00107         Adds the required lines to the plugin xml
00108         """
00109         print "Adding lines to the ethercat_device_plugins.xml"
00110 
00111         new_lines = []
00112         with open("../ethercat_device_plugin.xml", "r") as plugin_xml_read:
00113             for line in plugin_xml_read:
00114                 new_lines.append(self._substitute(line))
00115 
00116         with open("../ethercat_device_plugin.xml", "w") as plugin_xml_write:
00117             plugin_xml_write.writelines(new_lines)
00118 
00119     def _substitute(self, line):
00120         """
00121         Substitutes the values from the _substitutions dict in the given line
00122         @param line a line from the template
00123         @return the modified line
00124         """
00125         for keyword, replace_with in self._substitutions.iteritems():
00126             line = line.replace(keyword, replace_with)
00127         return line
00128 
00129 
00130 if __name__ == "__main__":
00131     from argparse import ArgumentParser
00132     parser = ArgumentParser()
00133 
00134     parser.add_argument("--module_name", "-m", type=str, required=True,
00135                         help="The name of the new RoNeX module (e.g. ADC, GIO, etc...)")
00136     parser.add_argument("--product_id", "-p", type=str, required=True,
00137                         help="The product id in hex (e.g. 0x2000002)")
00138     args = parser.parse_args()
00139 
00140     driver_generator = DriverGenerator(args.module_name, args.product_id)


sr_ronex_drivers
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless
autogenerated on Thu Jun 6 2019 21:22:00