pyEnv - Pluginbasierte Python Shell (Code, Stil, Umsetzung?)
Verfasst: Montag 25. August 2008, 18:17
Hey, Ich habe mal wieder nen bisschen rumgebastelt. Ich habe ein relatives simples Plugin System zustande gebracht:
Ich wollte mal wissen was ihr vom Code und er Umsetzung haltet.
Um selber Plugins zu schreiben einfach einen Unterordner "plugins" erstellen, module erstellen und dort funktionen definieren. Die funktionen kann man nachher über ihre Namen in der Konsole aufrufen.
MfG,
CracKPod
Code: Alles auswählen
#!/usr/bin/python
import types
import os
class pyEnv(object):
def __init__(self):
self.commands = {}
for plugin in self.plugins:
plugin = __import__(os.path.join('plugins', plugin))
for key, value in plugin.__dict__.iteritems():
if type(value) == types.FunctionType:
self.register_command(key, value)
def register_command(self, command, callback):
if not self.commands.has_key(command):
self.commands[command] = callback
def parse(self, string):
tokens = string.split()
command = tokens[0].lower()
if self.commands.has_key(command):
self.commands[command](tokens[1:])
@property
def plugins(self):
plugins = []
for file in os.listdir(os.path.join('plugins')):
if not file.startswith('__') and file.endswith('.py'):
plugins.append(file.split('.py')[0])
return plugins
pyenv = pyEnv()
while True:
command = str(raw_input('>>> '))
if command:
pyenv.parse(command)
Um selber Plugins zu schreiben einfach einen Unterordner "plugins" erstellen, module erstellen und dort funktionen definieren. Die funktionen kann man nachher über ihre Namen in der Konsole aufrufen.
Code: Alles auswählen
def say(args):
print ' '.join(args)
...
>>> say Hallo wie gehts?
Hallo wie gehts?
>>>
MfG,
CracKPod