~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
6
import ivle.conf
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
7
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
1099.1.100 by Nick Chadwick
Created a new help system.
8
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
9
from ivle.webapp.errors import NotFound, Forbidden
1099.1.100 by Nick Chadwick
Created a new help system.
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:
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
27
            newdict[key] = os.path.join(plugin, curdict[key])
1099.1.100 by Nick Chadwick
Created a new help system.
28
    return newdict
29
30
class HelpView(XHTMLView):
31
    """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
32
    tab = 'help'
1099.1.100 by Nick Chadwick
Created a new help system.
33
    template = 'helpview.html'
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
34
1099.1.100 by Nick Chadwick
Created a new help system.
35
    def __init__(self, req, path):
36
        self.paths = path.split('/')
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
37
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
38
    def authorize(self, req):
39
        return req.user is not None
40
1099.1.100 by Nick Chadwick
Created a new help system.
41
    def populate(self, req, ctx):
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
42
        self.plugin_styles[Plugin] = ['help.css']
43
1092.1.59 by William Grant
Move the plugin loading/indexing logic into ivle.config.Config.
44
        helpfile = generate_toc(req.config.plugin_index[ViewPlugin], req)
1099.1.100 by Nick Chadwick
Created a new help system.
45
        try:
46
            for path in self.paths:
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
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):
1099.1.100 by Nick Chadwick
Created a new help system.
61
    """Displays the help Table of Contents."""
1116 by William Grant
Move the old tutorial views into the 'subjects' tab, so they get the right
62
    tab = 'help'
1099.1.100 by Nick Chadwick
Created a new help system.
63
    template = 'toc.html'
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
64
1099.1.168 by William Grant
Add authorization information to the help ToC view.
65
    def authorize(self, req):
66
        return req.user is not None
67
1099.1.100 by Nick Chadwick
Created a new help system.
68
    def populate(self, req, ctx):
1092.1.59 by William Grant
Move the plugin loading/indexing logic into ivle.config.Config.
69
        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
70
1099.1.100 by Nick Chadwick
Created a new help system.
71
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
72
class Plugin(ViewPlugin, MediaPlugin):
1099.1.100 by Nick Chadwick
Created a new help system.
73
    """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
74
1099.1.100 by Nick Chadwick
Created a new help system.
75
    urls = [
1099.1.102 by William Grant
Clean up ivle.webapp.help and improve error handling when traversing to help
76
        ('+help', HelpToCView),
1099.1.100 by Nick Chadwick
Created a new help system.
77
        ('+help/*path', HelpView)
78
    ]
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
79
1099.1.115 by William Grant
Add tabs to the new framework. Move the app icons into the apps themselves.
80
    tabs = [
1118 by matt.giuca
Rewrote tooltips for the four tabs visible by default.
81
        ('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.
82
    ]
83
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
84
    media = 'media'