File: plugin.py

package info (click to toggle)
geany-plugins 1.37%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,548 kB
  • sloc: ansic: 112,728; sh: 4,283; makefile: 1,630; python: 947
file content (131 lines) | stat: -rw-r--r-- 2,876 bytes parent folder | download | duplicates (3)
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
"""
All plugins need to import this module and inherit from the Plugin class.  A
basic plugin could look something like this:

	import geany
	from geany.plugin import Plugin

	class OpenNewDocumentPlugin(Plugin):

		__plugin_name__ = "Open new document plugin"

		_doc = None

		def __init__(self):
			Plugin.__init__(self)
			print "About to open a new document"
			self.open_document('/path/to/some/file')

		def open_document(self, filename):
			self._doc = geany.document.open(filename)

		def close_document(self):
			self._doc.close()
			self._doc = None

		def cleanup(self):
			if self._doc is not None:
				self._doc.close()

The guts of the API are exposed to plugins through the `geany` package and
its modules.

Plugins should be placed in either the system plugin directory (something like
/usr/local/lib/geany) or in the user plugin directory (something like
~/.config/geany/plugins).  Only files with a `.py` extension will be loaded.
"""


from geany.logger import PluginLogger
import keybindings

class Plugin(object):
	"""
	Base class for all plugins.  All plugins must inherit from this in order
	to be properly detected.
	"""

	# Child classes should implement these (at least __plugin__name__)
	#__plugin_name__ = None
	#__plugin_description__ = None
	#__plugin_version__ = None
	#__plugin_author__ = None

	_events = {
		"document-open": [],
		# TODO: add more events here
	}


	def __init__(self):
		"""
		When the plugin is loaded its __init__() function will be called
		so that's a good place to put plugin initialization code.
		"""
		self.logger = PluginLogger(self.name)


	def cleanup(self):
		"""
		When the plugin is unloaded the cleanup() function will be called, so
		it's a good place to put and clean-up/tear-down code.
		"""
		pass


	@property
	def __plugin_name__(self):
		"""
		Plugins must implement a __plugin_name__ attribute that returns the
		string name of the plugin.
		"""
		raise NotImplementedError(
			"Plugins must implement the __plugin_name__ attribute.")


	@property
	def name(self):
		"""
		Get plugin's name.
		"""
		return self.__plugin_name__


	@property
	def description(self):
		"""
		Get plugin's description.
		"""
		if hasattr(self, '__plugin_description__'):
			return self.__plugin_description__
		else:
			return ""


	@property
	def version(self):
		"""
		Get plugin's version.
		"""
		if hasattr(self, '__plugin_version__'):
			return self.__plugin_version__
		else:
			return ""


	@property
	def author(self):
		"""
		Get plugin's author.
		"""
		if hasattr(self, '__plugin_author__'):
			return self.__plugin_author__
		else:
			return ""

	def set_key_group(self, section_name, count, callback = None):
		"""
		Sets up a GeanyKeyGroup for this plugin. You can use that group to add keybindings
		with group.add_key_item().
		"""
		return keybindings.set_key_group(self, section_name, count, callback)