|
| 1 | + |
| 2 | +# A script to automate creation of a new QGIS plugin using the plugin_template |
| 3 | +# Copyright 2007 Martin Dobias |
| 4 | +# |
| 5 | +# Original authors of Perl version: Gary Sherman and Tim Sutton |
| 6 | + |
| 7 | +import os, sys, shutil, re |
| 8 | + |
| 9 | +def template_file(file): |
| 10 | + return os.path.join('plugin_template', file) |
| 11 | + |
| 12 | +def plugin_file(pluginDir, file): |
| 13 | + return os.path.join(pluginDir, file) |
| 14 | + |
| 15 | +# make sure we are in a the plugins directory otherwise the changes this script will make will |
| 16 | +# wreak havoc.... |
| 17 | + |
| 18 | +myDir = os.getcwd() |
| 19 | +print "Checking that we are in the <qgis dir>/src/plugins/ directory....", |
| 20 | + |
| 21 | +pluginsDirectory = os.path.join('src','plugins') |
| 22 | + |
| 23 | +if myDir[-len(pluginsDirectory):] == pluginsDirectory: |
| 24 | + print "yes" |
| 25 | +else: |
| 26 | + print "no" |
| 27 | + print myDir |
| 28 | + print "Please relocate to the plugins directory before attempting to run this script." |
| 29 | + sys.exit(1) |
| 30 | + |
| 31 | + |
| 32 | +# get the needed information from the user |
| 33 | +print "Enter the directory name under qgis/src/plugins/ where your new plugin will be created." |
| 34 | +print "We suggest using a lowercase underscore separated name e.g. clever_plugin" |
| 35 | +print "Directory for the new plugin:", |
| 36 | + |
| 37 | +pluginDir = raw_input() |
| 38 | + |
| 39 | +print |
| 40 | +print "Enter the name that will be used when creating the plugin library." |
| 41 | +print "The name should be entered as a mixed case name with no spaces. e.g. CleverTool" |
| 42 | +print "The plugin name will be used in the following ways:" |
| 43 | +print "1) it will be 'lower cased' and used as the root of the generated lib name" |
| 44 | +print " e.g. libqgis_plugin_clevertool" |
| 45 | +print "2) in its upper cased form it will be used as the basis for class names, in particular" |
| 46 | +print " CleverToolGuiBase <- The base class for the plugins configuration dialog / gui generated by uic" |
| 47 | +print " CleverToolGui <- The concrete class for the plugins configuration dialog" |
| 48 | +print "3) CleverTool <- The class that includes the plugin loader instructions and" |
| 49 | +print " and calls to your custom application logic" |
| 50 | +print "4) clevertool.h, clevertool.cpp etc. <- the filenames used to hold the above derived classes" |
| 51 | +print "Plugin name:", |
| 52 | + |
| 53 | +pluginName = raw_input() |
| 54 | +pluginLCaseName = pluginName.lower() |
| 55 | + |
| 56 | +print |
| 57 | +print "Enter a short description (typically one line)" |
| 58 | +print "e.g. The clever plugin does clever stuff in QGIS" |
| 59 | +print "Plugin description:", |
| 60 | +pluginDescription = raw_input() |
| 61 | + |
| 62 | +print |
| 63 | +print "Enter the name of the application menu that will be created for your plugin" |
| 64 | +print "Clever Tools" |
| 65 | +print "Menu name:", |
| 66 | +menuName = raw_input() |
| 67 | + |
| 68 | +print |
| 69 | +print "Enter the name of the menu entry (under the menu that you have just defined) that" |
| 70 | +print "will be used to invoke your plugin. e.g. Clever Plugin" |
| 71 | +print "Menu item name:", |
| 72 | +menuItemName = raw_input() |
| 73 | + |
| 74 | +# print a summary of what's about to happen |
| 75 | +# and ask if we should proceed |
| 76 | +print |
| 77 | +print "Summary of plugin parameters:" |
| 78 | +print "---------------------------------------------" |
| 79 | +print "Plugin directory: ", pluginDir |
| 80 | +print "Name of the plugin: ", pluginName |
| 81 | +print "Description of the plugin:", pluginDescription |
| 82 | +print "Menu name: ", menuName |
| 83 | +print "Menu item name: ", menuItemName |
| 84 | +print |
| 85 | +print "Warning - Proceeding will make changes to CMakeLists.txt in this directory." |
| 86 | +print "Create the plugin? [y/n]:", |
| 87 | + |
| 88 | +createIt = raw_input() |
| 89 | + |
| 90 | +if createIt.lower() != 'y': |
| 91 | + print "Plugin creation cancelled, exiting" |
| 92 | + sys.exit(2) |
| 93 | + |
| 94 | +# create the plugin and modify the build files |
| 95 | + |
| 96 | +# create the new plugin directory |
| 97 | +os.mkdir(pluginDir) |
| 98 | + |
| 99 | +# copy files to appropriate names |
| 100 | +shutil.copy(template_file('CMakeLists.txt'), pluginDir) |
| 101 | +shutil.copy(template_file('README.whatnext'), os.path.join(pluginDir, 'README')) |
| 102 | +shutil.copy(template_file('plugin.qrc'), os.path.join(pluginDir, pluginLCaseName + '.qrc')) |
| 103 | +shutil.copy(template_file('plugin.png'), os.path.join(pluginDir, pluginLCaseName + '.png')) |
| 104 | +shutil.copy(template_file('plugin.cpp'), os.path.join(pluginDir, pluginLCaseName + '.cpp')) |
| 105 | +shutil.copy(template_file('plugin.h'), os.path.join(pluginDir, pluginLCaseName + '.h')) |
| 106 | +shutil.copy(template_file('plugingui.cpp'), os.path.join(pluginDir, pluginLCaseName + 'gui.cpp')) |
| 107 | +shutil.copy(template_file('plugingui.h'), os.path.join(pluginDir, pluginLCaseName + 'gui.h')) |
| 108 | +shutil.copy(template_file('plugingui.ui'), os.path.join(pluginDir, pluginLCaseName + 'gui.ui')) |
| 109 | + |
| 110 | +# Substitute the plugin specific vars in the various files |
| 111 | +# This is a brute force approach but its quick and dirty :) |
| 112 | +# |
| 113 | + |
| 114 | +files = [ plugin_file(pluginDir, 'CMakeLists.txt'), |
| 115 | + plugin_file(pluginDir, pluginLCaseName + '.qrc'), |
| 116 | + plugin_file(pluginDir, pluginLCaseName + '.cpp'), |
| 117 | + plugin_file(pluginDir, pluginLCaseName + '.h'), |
| 118 | + plugin_file(pluginDir, pluginLCaseName + 'gui.cpp'), |
| 119 | + plugin_file(pluginDir, pluginLCaseName + 'gui.h'), |
| 120 | + plugin_file(pluginDir, pluginLCaseName + 'gui.ui') ] |
| 121 | + |
| 122 | +# replace occurences of [pluginlcasename], [pluginname], [plugindescription], [menuname], [menutiem] |
| 123 | +# in template with the values from user |
| 124 | +replacements = [ ('\\[pluginlcasename\\]', pluginLCaseName), |
| 125 | + ('\\[pluginname\\]', pluginName), |
| 126 | + ('\\[plugindescription\\]', pluginDescription), |
| 127 | + ('\\[menuname\\]', menuName), |
| 128 | + ('\\[menuitemname\\]', menuItemName) ] |
| 129 | + |
| 130 | +for file in files: |
| 131 | + |
| 132 | + # read contents of the file |
| 133 | + f = open(file) |
| 134 | + content = f.read() |
| 135 | + f.close() |
| 136 | + |
| 137 | + # replace everything neccessary |
| 138 | + for repl in replacements: |
| 139 | + content = re.sub(repl[0], repl[1], content) |
| 140 | + |
| 141 | + # write changes to the file |
| 142 | + f = open(file, "w") |
| 143 | + f.write(content) |
| 144 | + f.close() |
| 145 | + |
| 146 | + |
| 147 | +# Add an entry to src/plugins/CMakeLists.txt |
| 148 | +f = open('CMakeLists.txt','a') |
| 149 | +f.write('\nSUBDIRS ('+pluginDir+')\n') |
| 150 | +f.close() |
| 151 | + |
| 152 | +print "Your plugin %s has been created in %s, CMakeLists.txt has been modified." % (pluginName, pluginDir) |
| 153 | +print |
| 154 | +print "Once your plugin has successfully built, please see %s/README for" % (pluginDir) |
| 155 | +print "hints on how to get started." |
| 156 | + |
0 commit comments