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

1099.1.100 by Nick Chadwick
Created a new help system.
1
import os, inspect
2
3
import genshi
4
import genshi.template
5
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
6
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
1099.1.100 by Nick Chadwick
Created a new help system.
7
from ivle.webapp.base.xhtml import XHTMLView
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
8
from ivle.webapp.errors import NotFound, Forbidden
1099.1.100 by Nick Chadwick
Created a new help system.
9
10
def generate_toc(plugins, req):
11
    toc = {}
12
    for plugin in plugins:
13
        if hasattr(plugin, 'help'):
14
            # Get the dir the plugin resides in
15
            plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
16
            add_dict(toc, plugin.help, plugindir)
17
    return toc
18
19
def add_dict(newdict, curdict, plugin):
20
    for key in curdict:
21
        if key not in newdict:
22
            newdict[key] = {}
23
        if isinstance(curdict[key], dict):
24
            add_dict(newdict[key], curdict[key], plugin)
25
        else:
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
26
            newdict[key] = os.path.join(plugin, curdict[key])
1099.1.100 by Nick Chadwick
Created a new help system.
27
    return newdict
28
29
class HelpView(XHTMLView):
30
    """Shows the help file for the specified path."""
1116 by William Grant
Move the old tutorial views into the 'subjects' tab, so they get the right
31
    tab = 'help'
1099.1.100 by Nick Chadwick
Created a new help system.
32
    template = 'helpview.html'
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
33
1099.1.100 by Nick Chadwick
Created a new help system.
34
    def __init__(self, req, path):
35
        self.paths = path.split('/')
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
36
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
37
    def authorize(self, req):
38
        return req.user is not None
39
1099.1.100 by Nick Chadwick
Created a new help system.
40
    def populate(self, req, ctx):
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
41
        self.plugin_styles[Plugin] = ['help.css']
42
1092.1.59 by William Grant
Move the plugin loading/indexing logic into ivle.config.Config.
43
        helpfile = generate_toc(req.config.plugin_index[ViewPlugin], req)
1099.1.100 by Nick Chadwick
Created a new help system.
44
        try:
45
            for path in self.paths:
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
46
                if len(path) > 0:
47
                    helpfile = helpfile[path]
48
        except (KeyError, TypeError):
49
            # Traversal failed. We 404.
50
            raise NotFound()
51
52
        if not isinstance(helpfile, basestring):
53
            # It's a virtual directory.
54
            raise Forbidden()
55
56
        ctx['helpfile'] = helpfile
57
58
59
class HelpToCView(XHTMLView):
1099.1.100 by Nick Chadwick
Created a new help system.
60
    """Displays the help Table of Contents."""
1116 by William Grant
Move the old tutorial views into the 'subjects' tab, so they get the right
61
    tab = 'help'
1099.1.100 by Nick Chadwick
Created a new help system.
62
    template = 'toc.html'
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
63
1099.1.168 by William Grant
Add authorization information to the help ToC view.
64
    def authorize(self, req):
65
        return req.user is not None
66
1099.1.100 by Nick Chadwick
Created a new help system.
67
    def populate(self, req, ctx):
1092.1.59 by William Grant
Move the plugin loading/indexing logic into ivle.config.Config.
68
        ctx['toc'] = generate_toc(req.config.plugin_index[ViewPlugin], req)
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
69
1099.1.100 by Nick Chadwick
Created a new help system.
70
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
71
class Plugin(ViewPlugin, MediaPlugin):
1099.1.100 by Nick Chadwick
Created a new help system.
72
    """The plugin for viewing help files."""
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
73
1099.1.100 by Nick Chadwick
Created a new help system.
74
    urls = [
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
75
        ('+help', HelpToCView),
1099.1.100 by Nick Chadwick
Created a new help system.
76
        ('+help/*path', HelpView)
77
    ]
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
78
1099.1.115 by William Grant
Add tabs to the new framework. Move the app icons into the apps themselves.
79
    tabs = [
1118 by matt.giuca
Rewrote tooltips for the four tabs visible by default.
80
        ('help', 'Help', 'Get help with IVLE', 'help.png', '+help', 100)
1099.1.115 by William Grant
Add tabs to the new framework. Move the app icons into the apps themselves.
81
    ]
82
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
83
    media = 'media'