Added optional colorama dependency to allow colours in windows console

Added flag to disable ansi colours. Fixes #10
Now passing plugin loader to plugins onEnable
This commit is contained in:
Ammar Askar 2012-12-31 07:20:06 +05:00
parent 6025e97932
commit 4988ab6ae1
7 changed files with 30 additions and 18 deletions

View File

@ -24,7 +24,7 @@ In your plugin.py file define a single class with the exact same spellings as yo
things happen in the minecraft world. See the [PacketDumper plugin](https://github.com/ammaraskar/pyCraft/blob/master/plugins/PacketDumper.py) as an example. Currently these methods are things happen in the minecraft world. See the [PacketDumper plugin](https://github.com/ammaraskar/pyCraft/blob/master/plugins/PacketDumper.py) as an example. Currently these methods are
used: used:
```onEnable(self, parser)``` This fires when your plugin is loaded and passes the option parser as an argument allowing you to add custom command line options ```onEnable(self, parser, pluginloader)``` This fires when your plugin is loaded and passes the option parser as an argument allowing you to add custom command line options
```onDisable(self)``` This fires when pyCraft is exited cleanly, use it for cleaning up ```onDisable(self)``` This fires when pyCraft is exited cleanly, use it for cleaning up

View File

@ -3,6 +3,7 @@ import types
from io import BytesIO from io import BytesIO
from pynbt import NBTFile from pynbt import NBTFile
def readBoolean(FileObject): def readBoolean(FileObject):
return struct.unpack('?', FileObject.read(1))[0] return struct.unpack('?', FileObject.read(1))[0]

View File

@ -184,11 +184,8 @@ class PacketListener(threading.Thread):
if (response == ""): if (response == ""):
continue continue
except Exception, e: except Exception, e:
if (self.window): print "Ping timeout"
self.window.Status.SetLabel("Ping timeout") sys.exit()
else:
print "Ping timeout"
sys.exit()
break break
if (response == "\x00"): if (response == "\x00"):
packet = PacketListenerManager.handle00(self.FileObject, self.socket) packet = PacketListenerManager.handle00(self.FileObject, self.socket)
@ -198,8 +195,11 @@ class PacketListener(threading.Thread):
elif (response == "\x03"): elif (response == "\x03"):
packet = PacketListenerManager.handle03(self.FileObject) packet = PacketListenerManager.handle03(self.FileObject)
# Add "\x1b" because it is essential for ANSI escapes emitted by translate_escapes # Add "\x1b" because it is essential for ANSI escapes emitted by translate_escapes
filtered_string = filter(lambda x: x in string.printable + "\x1b", if not self.connection.options.disableAnsiColours:
Utils.translate_escapes(packet['Message'])) filtered_string = filter(lambda x: x in string.printable + "\x1b",
Utils.translate_escapes(packet['Message']))
else:
filtered_string = filter(lambda x: x in string.printable, packet['Message'])
print filtered_string print filtered_string
elif (response == "\x04"): elif (response == "\x04"):

View File

@ -3,13 +3,12 @@ import imp
class PluginLoader(): class PluginLoader():
path = "" path = ""
plugins = [] plugins = {}
listeners = [] listeners = []
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path
def loadPlugins(self, parser): def loadPlugins(self, parser):
for item in os.listdir(self.path): for item in os.listdir(self.path):
split = os.path.splitext(item) split = os.path.splitext(item)
@ -21,9 +20,9 @@ class PluginLoader():
pluginClass = None pluginClass = None
try: try:
pluginClass = getattr(m, name)() pluginClass = getattr(m, name)()
self.plugins.append(pluginClass) self.plugins[name] = pluginClass
try: try:
pluginClass.onEnable(parser) pluginClass.onEnable(parser, self)
except AttributeError: except AttributeError:
pass pass
try: try:
@ -34,28 +33,31 @@ class PluginLoader():
print "Plugin " + name + " is malformed" print "Plugin " + name + " is malformed"
def disablePlugins(self): def disablePlugins(self):
for plugin in self.plugins: for plugin in self.plugins.values():
try: try:
plugin.onDisable() plugin.onDisable()
except AttributeError: except AttributeError:
pass pass
def notifyOptions(self, options): def notifyOptions(self, options):
for plugin in self.plugins: for plugin in self.plugins.values():
try: try:
plugin.optionsParsed(options) plugin.optionsParsed(options)
except AttributeError: except AttributeError:
pass pass
def notify(self, methodName): def notify(self, methodName):
for plugin in self.plugins: for plugin in self.plugins.values():
try: try:
getattr(plugin, methodName)() getattr(plugin, methodName)()
except AttributeError: except AttributeError:
pass pass
def getPlugins(self): def getPlugins(self):
return self.plugins return self.plugins.values()
def getPlugin(self, name):
return self.plugins[name]
def getPacketListeners(self): def getPacketListeners(self):
return self.listeners return self.listeners

View File

@ -2,7 +2,7 @@ class IRC:
options = None options = None
writeFile = None writeFile = None
def onEnable(self, parser): def onEnable(self, parser, pluginloader):
parser.add_option("-q", "--irc-out-file", dest="ircDump", default="ircdump.txt", parser.add_option("-q", "--irc-out-file", dest="ircDump", default="ircdump.txt",
help="file to dump messages to") help="file to dump messages to")

View File

@ -7,7 +7,7 @@ class PacketDumper:
options = None options = None
writeFile = None writeFile = None
def onEnable(self, parser): def onEnable(self, parser, pluginloader):
parser.add_option("-d", "--dump-packets", parser.add_option("-d", "--dump-packets",
action="store_true", dest="dumpPackets", default=False, action="store_true", dest="dumpPackets", default=False,
help="run with this argument to dump packets") help="run with this argument to dump packets")

View File

@ -4,6 +4,11 @@ import Utils
from pluginloader import PluginLoader from pluginloader import PluginLoader
from networking import PacketSenderManager, NetworkManager from networking import PacketSenderManager, NetworkManager
from optparse import OptionParser from optparse import OptionParser
try:
import colorama
colorama.init()
except ImportError:
pass
if __name__ == "__main__": if __name__ == "__main__":
parser = OptionParser() parser = OptionParser()
@ -21,6 +26,10 @@ if __name__ == "__main__":
action="store_true", default=False, action="store_true", default=False,
help="run in offline mode i.e don't attempt to auth via minecraft.net") help="run in offline mode i.e don't attempt to auth via minecraft.net")
parser.add_option("-c", "--disable-console-colours", dest="disableAnsiColours",
action="store_true", default=False,
help="print minecraft chat colours as their equivalent ansi colours")
# pluginLoader # pluginLoader
pluginLoader = PluginLoader("plugins") pluginLoader = PluginLoader("plugins")
pluginLoader.loadPlugins(parser) pluginLoader.loadPlugins(parser)