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