platform.py 3.43 KB
Newer Older
1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-

# Imports
import cherrypy
import os
import json
import sys
import traceback
9
10
11
12
13
import pkgutil
import importlib
import vispa
import vispa.extensions
from vispa.extensions import AbstractExtension
14
from vispa.models.workspace import Workspace
murban's avatar
murban committed
15
from vispa.filesystem import FileSystem
16
17
from vispa.helpers import browser
from vispa.models.user import User
Marcel's avatar
Marcel committed
18
from vispa.controller import StaticController
19
from vispa.controller.platform import PlatformController
20
from vispa.bus import Bus
21
22
import logging

mkomm's avatar
mkomm committed
23
systemlog = logging.getLogger(__name__)
24

25
26
27
28

class Platform(object):

    def __init__(self, var_dir, base_url="/"):
29
        systemlog.debug("Startup Platform")
30
        self.bus = Bus()
31
32
33
34
35
36
37
        self.controller = PlatformController(self)
        self.base_url = base_url
        self.common_js = []
        self.common_css = []
        self.extensions = {}

    def load_extensions(self, platform):
38
39
        # load the modules containing the extesions
        # that inherit from 'Extension'
Gero Müller's avatar
Gero Müller committed
40
        ignore_extensions = vispa.config('extensions', 'ignore', [])
41
42
        for importer, modulename, ispkg in pkgutil.iter_modules(vispa.extensions.__path__):
            if not ispkg or modulename in ignore_extensions:
43
                continue
Marcel's avatar
Marcel committed
44
            module = importlib.import_module('vispa.extensions.%s.extension' % modulename)
45
        # instantiate all subclasses of 'Extension'
46
        dependencies = {}
47
        for cls in AbstractExtension.__subclasses__():
48
            systemlog.debug("Loading Extension '%s'" % str(cls))
49
            extension = cls()
50
51
52
53
54
55
56
57
            name = extension.get_name()
            if self.extensions.has_key(name):
                raise Exception("Fail to load extension: '%s'. It already exsits!" % name)
            self.extensions[name] = extension
            # update the 'dependencies' dict
            for depm in extension.dependencies():
                if dep not in dependencies.keys():
                    dependencies[dep] = [name]
58
                else:
59
60
61
62
63
64
65
                    dependencies[dep].append(name)
        # check dependencies
        for dep in dependencies.keys():
            if dep not in self.extensions.keys():
                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
66
67
68
69
70
71
72
73
74
75
        for extension in self.extensions.values():
            extension.setup(self, self.extensions)

    def add_common_js(self, filepath):
        self.common_js.append(filepath)

    def add_common_css(self, filepath):
        self.common_css.append(filepath)

    def mount_extension_controller(self, mountpoint, controller):
Marcel's avatar
Marcel committed
76
77
        if hasattr(self.controller.extensions, mountpoint):
            systemlog.warning('Controller mountpoint already exists: %s' % mountpoint)
78
        else:
Marcel's avatar
Marcel committed
79
80
81
82
83
            systemlog.info("Mounting controller '%s'" % os.path.join(os.sep, mountpoint))
            setattr(self.controller.extensions, mountpoint, controller)
            # mount the static controller
            mountpoint_static = os.path.join('vispa/extensions', mountpoint, 'static/')
            setattr(controller, 'static', StaticController(mountpoint_static))
84
85
86
87
88

    def get_extension(self, name):
        extn = None
        ctrl = None
        try:
Marcel's avatar
Marcel committed
89
            extn = self.extensions[name]
90
91
92
93
            ctrl = getattr(self.controller.extensions, name)
        except:
            pass
        return extn, ctrl