Attachment 'gedit-py-plugin.py'

Download

   1 #!/usr/bin/python
   2 # -*- coding: utf-8 -*-
   3 
   4 from optparse import OptionParser
   5 import sys
   6 import time
   7 
   8 template_plugin = """# -*- coding: utf-8 -*-
   9 #  ${DESCRIPTION}
  10 # 
  11 #  Copyright (C) ${YEAR} ${AUTHOR}
  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 #  This program is distributed in the hope that it will be useful,
  19 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 #  GNU General Public License for more details.
  22 #   
  23 #  You should have received a copy of the GNU General Public License
  24 #  along with this program; if not, write to the Free Software
  25 #  Foundation, Inc., 59 Temple Place, Suite 330,
  26 #  Boston, MA 02111-1307, USA.
  27 
  28 import gedit, gtk
  29 from gettext import gettext as _
  30 
  31 # Menu item example, insert a new item in the Tools menu
  32 ui_str = \"""<ui>
  33   <menubar name="MenuBar">
  34     <menu name="ToolsMenu" action="Tools">
  35       <placeholder name="ToolsOps_2">
  36         <menuitem name="${NAME_CAMEL}" action="${NAME_CAMEL}"/>
  37       </placeholder>
  38     </menu>
  39   </menubar>
  40 </ui>
  41 \"""
  42 
  43 class ${NAME_CAMEL}PluginInstance:
  44 	def __init__(self, plugin, window):
  45 		self._window = window
  46 		self._plugin = plugin
  47 		
  48 		# Insert menu items
  49 		self._insert_menu()
  50 
  51 	def stop(self):
  52 		# Remove any installed menu items
  53 		self._remove_menu()
  54 		
  55 		self._window = None
  56 		self._plugin = None
  57 		self._action_group = None
  58 		
  59 	def _insert_menu(self):
  60 		# Get the GtkUIManager
  61 		manager = self._window.get_ui_manager()
  62 		
  63 		# Create a new action group
  64 		self._action_group = gtk.ActionGroup("${NAME_CAMEL}PluginActions")
  65 		self._action_group.add_actions([("${NAME_CAMEL}", None, _("${NAME}"), None, _("Example menu item"), lambda a: self.on_example_menu_item_activate())])
  66 		
  67 		# Insert the action group
  68 		manager.insert_action_group(self._action_group, -1)
  69 		
  70 		# Merge the UI
  71 		self._ui_id = manager.add_ui_from_string(ui_str)
  72 
  73 	def _remove_menu(self):
  74 		# Get the GtkUIManager
  75 		manager = self._window.get_ui_manager()
  76 		
  77 		# Remove the ui
  78 		manager.remove_ui(self._ui_id)
  79 		
  80 		# Remove the action group
  81 		manager.remove_action_group(self._action_group)
  82 		
  83 		# Make sure the manager updates
  84 		manager.ensure_update()
  85 
  86 	def update(self):
  87 		# Called whenever the window has been updated (active tab
  88 		# changed, etc.)
  89 		return
  90 
  91 	# Menu activate handlers
  92 	def on_example_menu_item_activate(self):
  93 		# Do something here when the menu item is activated
  94 		return
  95 
  96 class ${NAME_CAMEL}Plugin(gedit.Plugin):
  97 	DATA_TAG = "${NAME_CAMEL}PluginInstance"
  98 	
  99 	def __init__(self):
 100 		gedit.Plugin.__init__(self)
 101 
 102 	def _get_instance(self, window):
 103 		return window.get_data(self.DATA_TAG)
 104 	
 105 	def _set_instance(self, window, instance):
 106 		window.set_data(self.DATA_TAG, instance)
 107 	
 108 	def activate(self, window):
 109 		self._set_instance(window, ${NAME_CAMEL}PluginInstance(self, window))
 110 	
 111 	def deactivate(self, window):
 112 		self._get_instance(window).stop()
 113 		self._set_instance(window, None)
 114 		
 115 	def update_ui(self, window):
 116 		self._get_instance(window).update()
 117 """
 118 
 119 template_desktop = """[Gedit Plugin]
 120 Loader=python
 121 Module=${NAME_LOWER}
 122 IAge=2
 123 Name=${NAME}
 124 Description=${DESCRIPTION}
 125 Authors=${AUTHOR}
 126 Copyright=Copyright © ${YEAR} ${AUTHOR}
 127 Website=http://www.gedit.org
 128 """
 129 
 130 def parse_options():
 131 	parser = OptionParser()
 132 	parser.add_option("-n", "--name", action="store", type="string",
 133 			dest="name", help="the name of the plugin (e.g. Color picker)", 
 134 			metavar="NAME")
 135 	parser.add_option("-a", "--author", action="store", type="string",
 136 			dest="author", help="the name of the person who will write the plugin (usually you)", 
 137 			metavar="AUTHOR", default="<author>")
 138 	parser.add_option("-d", "--description", action="store", type="string",
 139 			dest="description", help="the description of the plugin (e.g. Pick a color from a dialog and insert its hexadecimal representation.)", 
 140 			metavar="DESCRIPTION", default="<description>")
 141 
 142 	return parser.parse_args()
 143 
 144 (options, args) = parse_options()
 145 
 146 if not options.name:
 147 	print "You need to specify the name of the plugin"
 148 	sys.exit(1)
 149 
 150 name = options.name.lower()
 151 lower = str.join('', name.split(' '))
 152 camel = ''
 153 
 154 for part in name.split(' '):
 155 	camel += part.capitalize()
 156 
 157 subst = {	'NAME': name.capitalize(), 
 158 		'NAME_LOWER': lower,
 159 		'NAME_CAMEL': camel,
 160 		'DESCRIPTION': options.description,
 161 		'AUTHOR': options.author,
 162 		'YEAR': time.strftime("%Y")}
 163 
 164 templates = {	lower + '.py': template_plugin,
 165 		lower + '.gedit-plugin': template_desktop}
 166 
 167 for filename in templates:
 168 	template = templates[filename]
 169 
 170 	print 'Generating', filename
 171 	
 172 	for target in subst:
 173 		replace = subst[target]
 174 		template = template.replace('${' + target + '}', replace)
 175 
 176 	f = open(filename, 'w')
 177 	f.write(template)
 178 	f.close()	

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-02-25 09:43:45, 48.9 KB) [[attachment:examplepy.png]]
  • [get | view] (2021-02-25 09:43:45, 4.8 KB) [[attachment:gedit-py-plugin.py]]
  • [get | view] (2021-02-25 09:43:45, 5.2 KB) [[attachment:gedit-py-plugin.pyc]]
  • [get | view] (2021-02-25 09:43:45, 29.7 KB) [[attachment:geditPref.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.