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