~sir-rainbow/+junk/scribes-on-win

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- coding: utf-8 -*-
# Copyright © 2007 Lateef Alabi-Oki
#
# This file is part of Scribes.
#
# Scribes 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.
#
# Scribes is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Scribes; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

"""
This modules documents a class that implements the plugin system for
Scribes.

@author: Lateef Alabi-Oki
@organization: The Scribes Project
@copyright: Copyright © 2007 Lateef Alabi-Oki
@license: GNU GPLv2 or Later
@contact: mystilleef@gmail.com
"""

class PluginManager(object):
	"""
	This class creates an object that loads and unloads plugins.
	"""

	def __init__(self, editor):
		"""
		Initialize object.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.

		@param editor: Reference to the editor object.
		@type editor: A editor object.
		"""
		try:
#			self.__precompile_methods() this is a test file.
			from Exceptions import PluginFolderNotFoundError
			self.__init_attributes(editor)
			self.__check_plugin_folders()
			self.__set_plugin_search_path()
			self.__load_plugins()
			self.__registration_id = self.__editor.register_object()
			self.__signal_id_1 = editor.connect("close-document", self.__quit_cb)
			self.__signal_id_2 = editor.connect("close-document-no-save", self.__quit_cb)
#			from gobject import idle_add, PRIORITY_LOW
#			idle_add(self.__precompile_methods, priority=PRIORITY_LOW)
		except PluginFolderNotFoundError:
			print "Error: No plugin folder found"

	def __init_attributes(self, editor):
		"""
		Initialize data attributes.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.

		@param editor: Reference to the editor instance.
		@type editor: A editor object.
		"""
		self.__editor = editor
		# A set of initialized plugins. Each element in the set is a
		# tuple with format (plugin_name, plugin_version, plugin_object).
		self.__plugin_objects = set([])
		# A set of all plugin modules. Each element in the set is a tuple
		# with the format, (plugin_name, plugin_version, module_object).
		self.__plugin_modules = set([])
		self.__registration_id = None
		self.__is_quiting = False
		return

	def __init_module(self, filename, plugin_folder):
		"""
		Initialize a plugin module file.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.

		@param filename: A possible plugin file.
		@type filename: A String object.
		"""
		from Exceptions import PluginModuleValidationError
		from Exceptions import DuplicatePluginError, DoNotLoadError
		try:
			if not (filename.startswith("Plugin") and filename.endswith(".py")): return False
			from os import path
			filepath = path.join(plugin_folder, filename)
			from imp import load_source
			module = load_source(filename[:-3], filepath)
			plugin_name, plugin_version, PluginClass = self.__get_module_info(module)
			self.__plugin_modules.add(module)
			self.__unload_duplicate_plugins(plugin_name, plugin_version)
			plugin_object = self.__load_plugin(PluginClass)
			self.__plugin_objects.add((plugin_name, plugin_version, plugin_object))
		except PluginModuleValidationError:
			print "Validation Error: ", filename
		except DuplicatePluginError:
			print "Duplicate Plugin: ", (plugin_name, plugin_version)
		except DoNotLoadError:
			print "Not loading: ", (filename)
			self.__plugin_modules.add(module)
		return False

	def __load_plugin(self, PluginClass):
		"""
		Initialize a plugin.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.

		@param PluginClass: The class object of a plugin.
		@type PluginClass: A Class object.

		@return: A plugin object.
		@rtype: A Plugin object.
		"""
		plugin_object = PluginClass(self.__editor)
		plugin_object.load()
		return plugin_object

	def __unload_plugin(self, plugin_info):
		plugin_object = plugin_info[2]
		plugin_object.unload()
		self.__plugin_objects.remove(plugin_info)
		if self.__plugin_objects: return False
		if self.__is_quiting: self.__destroy()
		return False

	def __load_plugins(self):
		"""
		Initialize plugins found in plugin modules.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.
		"""
		from os import listdir
		from gobject import idle_add, PRIORITY_LOW
		core_files = listdir(self.__editor.core_plugin_folder)
		#from thread import start_new_thread
		for filename in core_files:
		#	start_new_thread(self.__init_module, (filename, self.__editor.core_plugin_folder))
			idle_add(self.__init_module, filename, self.__editor.core_plugin_folder, priority=PRIORITY_LOW)
		home_files = listdir(self.__editor.home_plugin_folder)
		for filename in home_files:
			#start_new_thread(self.__init_module, (filename, self.__editor.home_plugin_folder))
			idle_add(self.__init_module, filename, self.__editor.home_plugin_folder, priority=PRIORITY_LOW)
		return False

	def __unload_plugins(self):
		"""
		Unload all plugins.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.
		"""
		from gobject import idle_add, PRIORITY_LOW
		#from thread import start_new_thread
		for plugin_info in self.__plugin_objects.copy():
			#start_new_thread(self.__unload_plugin, (plugin_info,))
			idle_add(self.__unload_plugin, plugin_info, priority=PRIORITY_LOW)
		return False

	def __get_module_info(self, module):
		"""
		Extract metadata from plugin module.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.

		@param module: A plugin module.
		@type module: A Module object.

		@return: plugin name, plugin version and plugin class object
		@rtype: A Tuple object.
		"""
		try:
			if not hasattr(module, "autoload"):
				raise Exception
			if not getattr(module, "autoload"):
				raise ValueError
			if hasattr(module, "version") is False:
				raise Exception
			plugin_version = getattr(module, "version")
			if hasattr(module, "class_name") is False:
				raise Exception
			plugin_name = class_name = getattr(module, "class_name")
			if hasattr(module, class_name) is False:
				raise Exception
			PluginClass = getattr(module, class_name)
			if hasattr(PluginClass, "__init__") is False:
				raise Exception
			if hasattr(PluginClass, "load") is False:
				raise Exception
			if hasattr(PluginClass, "unload") is False:
				raise Exception
		except ValueError:
			from Exceptions import DoNotLoadError
			raise DoNotLoadError
		except:
			from Exceptions import PluginModuleValidationError
			raise PluginModuleValidationError
		return plugin_name, plugin_version, PluginClass

	def __unload_duplicate_plugins(self, name, version):
		"""
		Unload old duplicate plugin versions to avoid conflict.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.

		@param name: Name of a plugin.
		@type name: A String object.

		@param version: Version of a plugin.
		@type version: A Float object.
		"""
		for info in self.__plugin_objects.copy():
			if name in info:
				if (version > info[1]):
					info[2].unload()
					self.__plugin_objects.remove(info)
				else:
					from Exceptions import DuplicatePluginError
					raise DuplicatePluginError
				break
		return

	def __check_plugin_folders(self):
		"""
		Check plugin folders exist.

		If the plugin folder in ~/.gnome2/Scribes does not exist, create
		it.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.
		"""
		from os import makedirs, path
		from Exceptions import PluginFolderNotFoundError
		filename = path.join(self.__editor.core_plugin_folder, "__init__.py")
		if not path.exists(filename): raise PluginFolderNotFoundError
		filename = path.join(self.__editor.home_plugin_folder, "__init__.py")
		if path.exists(filename): return
		try:
			makedirs(self.__editor.home_plugin_folder)
		except OSError:
			pass
		try:
			handle = open(filename, "w")
			handle.close()
		except IOError:
			raise PluginFolderNotFoundError
		return

	def __set_plugin_search_path(self):
		"""
		Add plug-in folders to Python's search path.

		@param self: Reference to the ScribesPluginManager instance.
		@type self: A ScribesPluginManager object.
		"""
		from sys import path
		if not (self.__editor.core_plugin_folder in path): path.insert(0, self.__editor.core_plugin_folder)
		if not (self.__editor.home_plugin_folder in path): path.insert(0, self.__editor.home_plugin_folder)
		return

	def __destroy(self):
		"""
		Destroy this object.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.
		"""
		self.__plugin_modules.clear()
		self.__plugin_objects.clear()
		self.__editor.unregister_object(self.__registration_id)
		self.__editor.disconnect_signal(self.__signal_id_1, self.__editor)
		self.__editor.disconnect_signal(self.__signal_id_2, self.__editor)
		del self
		self = None
		return

	def __precompile_methods(self):
		try:
			from psyco import bind
			bind(self.__init_module)
			bind(self.__load_plugin)
		except:
			pass
		return False

	def __quit_cb(self, *args):
		"""
		Handles callback when the "close-document" or "close-document-no-save"
		signal is emitted.

		The function is called when the editor is closing.

		@param self: Reference to the PluginManager instance.
		@type self: A PluginManager object.
		"""
		self.__is_quiting = True
		# Unload plugins and destroy PluginManager.
		from gobject import idle_add
		idle_add(self.__unload_plugins)
		return