Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from file import PluginDirectory
from interpreter import Interpreter
from plugin_loader import PluginLoader
class Application:
def run(self):
plugin_loader = PluginLoader()
plugin_directory = PluginDirectory()
commands = plugin_loader.load(plugin_directory)
interpreter = Interpreter(commands)
while True:
print interpreter.interpret(raw_input())
import inspect
import imp
import os
import tempfile
from node import TreeNode
class File(TreeNode):
def __init__(self, path=None):
super(File, self).__init__()
self.classes = {}
self.__init_classes(path)
self.__init_structure(path)
def __init_classes(self, path):
# XXX: add a test for this case
if path is None or os.path.isdir(path):
return
with open(path, 'r') as f:
file_content = f.read()
# http://docs.python.org/library/tempfile.html#tempfile.mktemp
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.py')
temp_file.write(file_content)
temp_file.close()
try:
module = imp.load_source('', temp_file.name)
except Exception, e:
print e
module_classes = inspect.getmembers(module, inspect.isclass)
for name, klass in module_classes:
self.classes[name.lower()] = klass
os.unlink(temp_file.name)
os.unlink(temp_file.name + 'c')
def __init_structure(self, path):
if not isinstance(path, str) or not path:
return
current_node = self
parts = path.split('/')
for part in reversed(parts):
current_node.name = part
current_node.parent = File()
current_node = current_node.parent
class PluginDirectory(File):
def __init__(self):
super(PluginDirectory, self).__init__(self.plugin_path)
@property
def plugin_path(self):
return os.path.join(self.current_directory, 'commands')
@property
def current_directory(self):
# http://code.activestate.com/recipes/474083/#c8
return os.path.dirname(os.path.realpath(__file__))
from application import Application
if __name__ == '__main__':
application = Application()
application.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.