forked from qgis/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_builder.py
executable file
·156 lines (123 loc) · 5.66 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
# A script to automate creation of a new QGIS plugin using the plugin_template
# Copyright 2007 Martin Dobias
#
# Original authors of Perl version: Gary Sherman and Tim Sutton
import os, sys, shutil, 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 a 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....",
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 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 "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 cancelled, 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('plugingui.ui'), os.path.join(pluginDir, pluginLCaseName + 'gui.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, 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 + 'gui.ui') ]
# replace occurences of [pluginlcasename], [pluginname], [plugindescription], [menuname], [menutiem]
# in template with the values from user
replacements = [ ('\\[pluginlcasename\\]', pluginLCaseName),
('\\[pluginname\\]', pluginName),
('\\[plugindescription\\]', pluginDescription),
('\\[menuname\\]', menuName),
('\\[menuitemname\\]', menuItemName) ]
for file in files:
# read contents of the file
f = open(file)
content = f.read()
f.close()
# replace everything neccessary
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."