Commit 20bd7f0f authored by Gero Müller's avatar Gero Müller
Browse files

update extension loading

parent 457e5632
......@@ -18,7 +18,7 @@ for wroot, wdirs, wfiles in os.walk('vispa'):
setup (
name='vispa',
version='0.9.1',
version='0.9.2',
description='VISPA - Integrated Development Environment for Physicists',
author='VISPA Project',
author_email='vispa@lists.rwth-aachen.de',
......
......@@ -10,6 +10,10 @@ import csv
import token
import tokenize
from StringIO import StringIO
import pkgutil
import importlib
import inspect
import vispa.extensions
try:
import ConfigParser as cp
......@@ -183,3 +187,119 @@ class Netstat(object):
return Netstat.get_socket_owner_ipv6(local[4][0], local[4][1], remote[4][0], remote[4][1])
return None
# Base class definitin for Extensions
class AbstractExtension(object):
def __init__(self, server):
self.server = server
def get_name(self):
raise NotImplementedError
def dependencies(self):
raise NotImplementedError
def setup(self, extensionList):
raise NotImplementedError
def css(self, file):
self.server.controller.add_common_css(vispa.url.static(file, extension=self.get_name()))
def js(self, file):
self.server.controller.add_common_js(vispa.url.static(file, extension=self.get_name()))
def controller(self, controller):
self.server.controller.mount_extension_controller(self.get_name(), controller)
def remote(self, folder="remote"):
local = os.path.join(os.path.dirname(inspect.getabsfile(self.__class__)), folder)
remote = os.path.join('vispa', 'extensions', self.get_name(), folder)
vispa.rpc.add_directory_files(local, remote)
_extensions = {}
def loadExtensions(server):
# loop through all extensions and import their files
# so that 'AbstractExtension' will know about its subclasses
modulenames = []
ignored = config('extensions', 'ignore', [])
vispa.extensions.__path__.append(vispa.datapath('extensions'))
for _importer, modulename, ispkg in pkgutil.iter_modules(vispa.extensions.__path__):
if not ispkg:
continue
if modulename in ignored:
logger.info('Ignore extension: %s '% modulename)
continue
try:
importlib.import_module('vispa.extensions.%s' % modulename)
modulenames.append(modulename)
except:
_, message, _ = sys.exc_info()
logger.warning('Exception importing extension %s: %s' % (modulename, message))
log_exception()
for _importer, modulename, ispkg in pkgutil.iter_modules():
if not ispkg:
continue
if not modulename.startswith('vispa_'):
logger.info('Not a vispa extension: %s '% modulename)
continue
if modulename in ignored:
logger.info('Ignore extension: %s '% modulename)
continue
try:
importlib.import_module(modulename)
modulenames.append(modulename)
except:
_, message, _ = sys.exc_info()
logger.warning('Exception importing extension %s: %s' % (modulename, message))
log_exception()
load = [x.strip() for x in config('extensions', 'import', '').split(',')]
load = filter(lambda x: len(x) > 0, load)
for ext in load:
logger.info('Import extension: %s '% ext)
try:
importlib.import_module(ext)
modulenames.append(ext.split('.')[-1])
except:
_, message, _ = sys.exc_info()
logger.warning('Exception importing extension %s: %s' % (modulename, message))
log_exception()
# instantiate all subclasses of the 'AbstractExtension'
dependencies = {}
for cls in AbstractExtension.__subclasses__():
# check: is the modulename accepted?
modulename = cls.__module__.split('.')[-1]
if modulename in ignored:
continue
logger.debug('Loading Extension "%s"' % cls)
extension = cls(server)
name = extension.get_name()
if name in _extensions.keys():
raise Exception("Fail to load extension: '%s'. It already exsits!" % name)
_extensions[name] = extension
# update the 'dependencies' dict
for dep in extension.dependencies():
if dep not in dependencies.keys():
dependencies[dep] = [name]
else:
dependencies[dep].append(name)
# check dependencies
for dep in dependencies.keys():
if dep not in _extensions:
data = dep, str(dependencies[dep])
raise Exception("The following dependency could not be found: '%s'. The following extensions depend on it: %s" % data)
# setup all valid extenions
for extension in _extensions.values():
extension.setup(_extensions)
def getExtension(name):
return _extensions[name]
def getExtensions():
return _extensions.values()
# -*- coding: utf-8 -*-
import vispa
import pkgutil
import importlib
import logging
import inspect
import os
import sys
logger = logging.getLogger(__name__)
# Base class definitin for Extensions
class AbstractExtension(object):
def __init__(self, server):
self.server = server
def get_name(self):
raise NotImplementedError
def dependencies(self):
raise NotImplementedError
def setup(self, extensionList):
raise NotImplementedError
def css(self, file):
self.server.controller.add_common_css(vispa.url.static(file, extension=self.get_name()))
def js(self, file):
self.server.controller.add_common_js(vispa.url.static(file, extension=self.get_name()))
def controller(self, controller):
self.server.controller.mount_extension_controller(self.get_name(), controller)
def remote(self, folder="remote"):
local = os.path.join(os.path.dirname(inspect.getabsfile(self.__class__)), folder)
remote = os.path.join('vispa', 'extensions', self.get_name(), folder)
vispa.rpc.add_directory_files(local, remote)
_extensions = {}
def load(server):
# loop through all extensions and import their files
# so that 'AbstractExtension' will know about its subclasses
modulenames = []
ignored = vispa.config('extensions', 'ignore', [])
vispa.extensions.__path__.append(vispa.datapath('extensions'))
for _importer, modulename, ispkg in pkgutil.iter_modules(vispa.extensions.__path__):
if not ispkg or modulename in ignored:
logger.info('Ignore extension: %s '% modulename)
continue
try:
importlib.import_module('vispa.extensions.%s' % modulename)
modulenames.append(modulename)
except:
_, message, _ = sys.exc_info()
logger.warning('Exception importing extension %s: %s' % (modulename, message))
# instantiate all subclasses of the 'AbstractExtension'
dependencies = {}
for cls in AbstractExtension.__subclasses__():
# check: is the modulename accepted?
modulename = cls.__module__.split('.')[-1]
if modulename in ignored:
continue
logger.debug('Loading Extension "%s"' % cls)
extension = cls(server)
name = extension.get_name()
if name in _extensions.keys():
raise Exception("Fail to load extension: '%s'. It already exsits!" % name)
_extensions[name] = extension
# update the 'dependencies' dict
for dep in extension.dependencies():
if dep not in dependencies.keys():
dependencies[dep] = [name]
else:
dependencies[dep].append(name)
# check dependencies
for dep in dependencies.keys():
if dep not in _extensions:
data = dep, str(dependencies[dep])
raise Exception("The following dependency could not be found: '%s'. The following extensions depend on it: %s" % data)
# setup all valid extenions
for extension in _extensions.values():
extension.setup(_extensions)
def get(name):
return _extensions[name]
def get_all():
return _extensions.values()
......@@ -2,7 +2,7 @@
import cherrypy
from vispa.extensions import AbstractExtension
from vispa import AbstractExtension
from vispa.controller import AbstractController
import vispa.rpc
......
......@@ -2,7 +2,7 @@
# imports
import vispa.rpc
from vispa.extensions import AbstractExtension
from vispa import AbstractExtension
# we import the controller from a seperate file
from controller import DemoController
......
......@@ -2,7 +2,7 @@
# imports
import vispa.rpc
from vispa.extensions import AbstractExtension
from vispa import AbstractExtension
# we import the controller from a seperate file
from controller import DummyController
......
......@@ -2,7 +2,7 @@
import cherrypy
from vispa.extensions import AbstractExtension
from vispa import AbstractExtension
from vispa.controller import AbstractController
class FileBrowserController(AbstractController):
......
......@@ -5,7 +5,7 @@ import cherrypy
import vispa
import vispa.rpc
from vispa.extensions import AbstractExtension
from vispa import AbstractExtension
# import the controller
from controller import JobmanagementController
......
......@@ -6,7 +6,7 @@ import time
import vispa.rpc
from vispa.controller import AbstractController
from vispa.extensions import AbstractExtension
from vispa import AbstractExtension
class TerminalController(AbstractController):
......
......@@ -156,7 +156,7 @@ class Server(object):
from vispa.controller.platform import PlatformController
self.controller = vispa.controller.platform.PlatformController()
vispa.extensions.load(self)
vispa.loadExtensions(self)
script_name = vispa.url.dynamic('/', encoding='utf-8')
app = cherrypy.tree.mount(self.controller, script_name, self.__default_mount_config())
if os.path.isfile(cherrypy_conf):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment