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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os, inspect

import genshi
import genshi.template

from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
from ivle.webapp.base.xhtml import XHTMLView
from ivle.webapp.errors import NotFound, Forbidden

def generate_toc(plugins, req):
    toc = {}
    for plugin in plugins:
        if hasattr(plugin, 'help'):
            # Get the dir the plugin resides in
            plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
            add_dict(toc, plugin.help, plugindir)
    return toc

def add_dict(newdict, curdict, plugin):
    for key in curdict:
        if key not in newdict:
            newdict[key] = {}
        if isinstance(curdict[key], dict):
            add_dict(newdict[key], curdict[key], plugin)
        else:
            newdict[key] = os.path.join(plugin, curdict[key])
    return newdict

class HelpView(XHTMLView):
    """Shows the help file for the specified path."""
    tab = 'help'
    template = 'helpview.html'

    def __init__(self, req, path):
        self.paths = path.split('/')

    def authorize(self, req):
        return req.user is not None

    def populate(self, req, ctx):
        self.plugin_styles[Plugin] = ['help.css']

        helpfile = generate_toc(req.config.plugin_index[ViewPlugin], req)
        try:
            for path in self.paths:
                if len(path) > 0:
                    helpfile = helpfile[path]
        except (KeyError, TypeError):
            # Traversal failed. We 404.
            raise NotFound()

        if not isinstance(helpfile, basestring):
            # It's a virtual directory.
            raise Forbidden()

        ctx['helpfile'] = helpfile


class HelpToCView(XHTMLView):
    """Displays the help Table of Contents."""
    tab = 'help'
    template = 'toc.html'

    def authorize(self, req):
        return req.user is not None

    def populate(self, req, ctx):
        ctx['toc'] = generate_toc(req.config.plugin_index[ViewPlugin], req)


class Plugin(ViewPlugin, MediaPlugin):
    """The plugin for viewing help files."""

    urls = [
        ('+help', HelpToCView),
        ('+help/*path', HelpView)
    ]

    tabs = [
        ('help', 'Help', 'Get help with IVLE', 'help.png', '+help', 100)
    ]

    media = 'media'