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