~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
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
9
from ivle.webapp.publisher.decorators import forward_route, reverse_route
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
10
from ivle.webapp.publisher import ROOT
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
11
from ivle.webapp import ApplicationRoot
1099.1.100 by Nick Chadwick
Created a new help system.
12
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
13
def generate_toc(plugins):
1294.4.7 by David Coles
Add some docstrings to the very confusing functions.
14
    """Create a root HelpTree with content from the plugins."""
15
    toc = HelpTree(None, 'Help', {})
1099.1.100 by Nick Chadwick
Created a new help system.
16
    for plugin in plugins:
17
        if hasattr(plugin, 'help'):
18
            # Get the dir the plugin resides in
19
            plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
20
            add_dict(toc, plugin.help, plugindir)
21
    return toc
22
23
def add_dict(newdict, curdict, plugin):
1294.4.7 by David Coles
Add some docstrings to the very confusing functions.
24
    """Deeply merge curdict into newdict."""
1099.1.100 by Nick Chadwick
Created a new help system.
25
    for key in curdict:
26
        if isinstance(curdict[key], dict):
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
27
            if key not in newdict:
28
                newdict[key] = HelpTree(newdict, key, {})
1099.1.100 by Nick Chadwick
Created a new help system.
29
            add_dict(newdict[key], curdict[key], plugin)
30
        else:
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
31
            newdict[key] = HelpEntry(newdict, key, os.path.join(plugin, curdict[key]))
1099.1.100 by Nick Chadwick
Created a new help system.
32
    return newdict
33
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
34
class HelpTreeView(XHTMLView):
35
    tab = 'help'
36
    template = 'toc.html'
37
    
1099.1.100 by Nick Chadwick
Created a new help system.
38
    """Shows the help file for the specified path."""
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
39
    def authorize(self, req):
40
        return req.user is not None
41
42
    def populate(self, req, ctx):
43
        self.plugin_styles[Plugin] = ['help.css']
44
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
45
        ctx['req'] = req
46
        ctx['context'] = self.context
47
        ctx['HelpTree'] = HelpTree
48
        ctx['HelpEntry'] = HelpEntry
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
49
50
class HelpEntryView(XHTMLView):
1116 by William Grant
Move the old tutorial views into the 'subjects' tab, so they get the right
51
    tab = 'help'
1099.1.100 by Nick Chadwick
Created a new help system.
52
    template = 'helpview.html'
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
53
    
54
    """Shows the help file for the specified path."""
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
55
    def authorize(self, req):
56
        return req.user is not None
57
1099.1.100 by Nick Chadwick
Created a new help system.
58
    def populate(self, req, ctx):
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
59
        self.plugin_styles[Plugin] = ['help.css']
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
60
        
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
61
        ctx['helpfile'] = self.context.file
62
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
63
class HelpTree(dict):
64
    def __init__(self, parent, name, tree):
65
        super(HelpTree, self).__init__(tree)
66
        self.parent = parent
67
        self.name = name
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
68
69
class HelpEntry(object):
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
70
    def __init__(self, parent, name, file):
71
        self.parent = parent
72
        self.name = name
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
73
        self.file = file
74
75
@forward_route(ApplicationRoot, '+help')
76
def root_to_helptree(root):
1294.4.8 by David Coles
Refactor root_to_helptree; it now gets generate_toc to build the root.
77
    return generate_toc(root.config.plugin_index[ViewPlugin])
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
78
79
@forward_route(HelpTree, argc=1)
80
def helptree_to_help(help, subhelp_name):
81
    try:
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
82
        return help[subhelp_name]
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
83
    except KeyError:
84
        # No help entry of that name
85
        return None
1099.1.100 by Nick Chadwick
Created a new help system.
86
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
87
@reverse_route(HelpTree)
88
def helptree_url(helptree):
89
    if helptree.parent is None:
90
        return (ROOT, '+help')
91
    return (helptree.parent, helptree.name)
92
93
@reverse_route(HelpEntry)
94
def helpentry_url(helpentry):
95
    return (helpentry.parent, helpentry.name)
96
1294.4.9 by David Coles
Add breadcrumbs for help items and trees.
97
class HelpBreadcrumb(object):
98
    def __init__(self, req, context):
99
        self.req = req
100
        self.context = context
101
102
    @property
103
    def url(self):
104
        return self.req.publisher.generate(self.context)
105
106
    @property
107
    def text(self):
108
        return self.context.name
109
1294.4.10 by David Coles
Add a special HelpTreeBreadcrumb, which has a submenu for the items.
110
class HelpTreeBreadcrumb(HelpBreadcrumb):
111
    @property
112
    def menu(self):
113
        return dict((item.name, self.req.publisher.generate(item))
114
                    for item in self.context.values())
115
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
116
class Plugin(ViewPlugin, MediaPlugin):
1099.1.100 by Nick Chadwick
Created a new help system.
117
    """The plugin for viewing help files."""
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
118
    forward_routes = (root_to_helptree, helptree_to_help)
1294.4.6 by David Coles
Let the publisher generate help entry URLs for us.
119
    reverse_routes = (helptree_url, helpentry_url)
120
1294.4.5 by David Coles
Basic port of Help over to new object publishing system.
121
    views = [(HelpTree, '+index', HelpTreeView),
122
             (HelpEntry, '+index', HelpEntryView)]
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
123
1294.4.9 by David Coles
Add breadcrumbs for help items and trees.
124
    breadcrumbs = {HelpEntry: HelpBreadcrumb,
1294.4.10 by David Coles
Add a special HelpTreeBreadcrumb, which has a submenu for the items.
125
                   HelpTree: HelpTreeBreadcrumb,
1294.4.9 by David Coles
Add breadcrumbs for help items and trees.
126
                  }
127
1099.1.115 by William Grant
Add tabs to the new framework. Move the app icons into the apps themselves.
128
    tabs = [
1118 by matt.giuca
Rewrote tooltips for the four tabs visible by default.
129
        ('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.
130
    ]
131
1099.1.104 by William Grant
Restore the media files (just help.css) to the new help system.
132
    media = 'media'