~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/help/__init__.py

Merge from new-dispatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os, inspect
 
2
 
 
3
import genshi
 
4
import genshi.template
 
5
 
 
6
import ivle.conf
 
7
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
 
8
from ivle.webapp.base.xhtml import XHTMLView
 
9
from ivle.webapp.errors import NotFound, Forbidden
 
10
 
 
11
def generate_toc(plugins, req):
 
12
    toc = {}
 
13
    for plugin in plugins:
 
14
        if hasattr(plugin, 'help'):
 
15
            # Get the dir the plugin resides in
 
16
            plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
 
17
            add_dict(toc, plugin.help, plugindir)
 
18
    return toc
 
19
 
 
20
def add_dict(newdict, curdict, plugin):
 
21
    for key in curdict:
 
22
        if key not in newdict:
 
23
            newdict[key] = {}
 
24
        if isinstance(curdict[key], dict):
 
25
            add_dict(newdict[key], curdict[key], plugin)
 
26
        else:
 
27
            newdict[key] = os.path.join(plugin, curdict[key])
 
28
    return newdict
 
29
 
 
30
class HelpView(XHTMLView):
 
31
    """Shows the help file for the specified path."""
 
32
 
 
33
    template = 'helpview.html'
 
34
 
 
35
    def __init__(self, req, path):
 
36
        self.paths = path.split('/')
 
37
 
 
38
    def authorize(self, req):
 
39
        return req.user is not None
 
40
 
 
41
    def populate(self, req, ctx):
 
42
        self.plugin_styles[Plugin] = ['help.css']
 
43
 
 
44
        helpfile = generate_toc(req.plugin_index[ViewPlugin], req)
 
45
        try:
 
46
            for path in self.paths:
 
47
                if len(path) > 0:
 
48
                    helpfile = helpfile[path]
 
49
        except (KeyError, TypeError):
 
50
            # Traversal failed. We 404.
 
51
            raise NotFound()
 
52
 
 
53
        if not isinstance(helpfile, basestring):
 
54
            # It's a virtual directory.
 
55
            raise Forbidden()
 
56
 
 
57
        ctx['helpfile'] = helpfile
 
58
 
 
59
 
 
60
class HelpToCView(XHTMLView):
 
61
    """Displays the help Table of Contents."""
 
62
    appname = 'help'
 
63
    template = 'toc.html'
 
64
 
 
65
    def populate(self, req, ctx):
 
66
        ctx['toc'] = generate_toc(req.plugin_index[ViewPlugin], req)
 
67
 
 
68
 
 
69
class Plugin(ViewPlugin, MediaPlugin):
 
70
    """The plugin for viewing help files."""
 
71
 
 
72
    urls = [
 
73
        ('+help', HelpToCView),
 
74
        ('+help/*path', HelpView)
 
75
    ]
 
76
 
 
77
    media = 'media'