annotate clients/editor/scripts/plugin.py @ 359:ad5818097cd6

* Some cleanups for Editor class to match new coding standards * Minor cleanups in imports
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 26 Sep 2009 10:38:24 +0000
parents 8b125ec749d7
children 3827b510546f
rev   line source
355
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
1 # -*- coding: utf-8 -*-
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
2
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
3 # ####################################################################
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
4 # Copyright (C) 2005-2009 by the FIFE team
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
5 # http://www.fifengine.de
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
6 # This file is part of FIFE.
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
7 #
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
8 # FIFE is free software; you can redistribute it and/or
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
9 # modify it under the terms of the GNU Lesser General Public
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
10 # License as published by the Free Software Foundation; either
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
11 # version 2.1 of the License, or (at your option) any later version.
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
12 #
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
13 # This library is distributed in the hope that it will be useful,
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
16 # Lesser General Public License for more details.
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
17 #
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
18 # You should have received a copy of the GNU Lesser General Public
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
19 # License along with this library; if not, write to the
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
20 # Free Software Foundation, Inc.,
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
22 # ####################################################################
8b125ec749d7 Started cleaning up the editor source code:
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
23
255
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24 import os
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
25
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
26 class PluginManager:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
27 """ Currently, pluginmanager iterates through the plugin directory
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28 adding the plugins if they are set to True in settings.xml. If a plugin
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29 isn't set in settings.xml it assumes it is not to be loaded, and saves it
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30 as False in settings.xml.
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32 If a plugin fails to load due to exceptions, they are caught and a line
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33 of the error is printed to console.
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
34 """
359
ad5818097cd6 * Some cleanups for Editor class to match new coding standards
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 355
diff changeset
35 def __init__(self, settings, *args, **kwargs):
ad5818097cd6 * Some cleanups for Editor class to match new coding standards
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 355
diff changeset
36 self._settings = settings
255
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
37
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
38 self._pluginDir = "plugins"
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
39 self._plugins = []
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 files = []
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 for f in os.listdir(self._pluginDir):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 path = os.path.join(self._pluginDir, f)
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 if os.path.isfile(path) and os.path.splitext(f)[1] == ".py" and f != "__init__.py":
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 files.append(os.path.splitext(f)[0])
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47 for f in files:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48 importPlugin = self._settings.get("Plugins", f, False)
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
49 if importPlugin:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
50 try:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
51 print "Importing plugin:", f
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52 exec "import plugins."+f
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
53 plugin = eval("plugins."+f+"."+f+"()")
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
54 if isinstance(plugin, Plugin) is False:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
55 print f+" is not an instance of Plugin!"
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
56 else:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
57 plugin.enable()
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58 self._plugins.append(plugin)
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 except BaseException, error:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60 print "Error: ", error
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61 print "Invalid plugin:", f
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62 else:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 print "Not importing plugin:", f
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
64
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
65 self._settings.set("Plugins", f, importPlugin)
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
66
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
67 self._settings.saveSettings()
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
69
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
70 class Plugin:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
71 """ The base class for all plugins. All plugins should override these functions. """
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
73 def enable(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
74 raise NotImplementedError, "Plugin has not implemented the enable() function!"
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
75
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
76 def disable(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 raise NotImplementedError, "Plugin has not implemented the disable() function!"
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
79 def isEnabled(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
80 raise NotImplementedError, "Plugin has not implemented the isEnabled() function!"
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
81
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
82 def getName(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
83 raise NotImplementedError, "Plugin has not implemented the getName() function!"
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
84
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
85 #--- These are not so important ---#
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
86 def getAuthor(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
87 return "Unknown"
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
88
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
89 def getDescription(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
90 return ""
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
91
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
92 def getLicense(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
93 return ""
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
94
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
95 def getVersion(self):
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
96 return "0.1"