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
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,6 +4,11 @@ import Utils
from pluginloader import PluginLoader
from networking import PacketSenderManager, NetworkManager
from optparse import OptionParser
try:
import colorama
colorama.init()
except ImportError:
pass
if __name__ == "__main__":
parser = OptionParser()
@ -21,6 +26,10 @@ if __name__ == "__main__":
action="store_true", default=False,
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("plugins")
pluginLoader.loadPlugins(parser)