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

« back to all changes in this revision

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

  • Committer: David Coles
  • Date: 2009-08-13 05:15:17 UTC
  • mto: (1294.2.124 ui-the-third)
  • mto: This revision was merged to the branch mainline in revision 1353.
  • Revision ID: coles.david@gmail.com-20090813051517-b5zxrzdt9rc8kxyv
Let the publisher generate help entry URLs for us.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from ivle.webapp.base.xhtml import XHTMLView
8
8
from ivle.webapp.errors import NotFound, Forbidden
9
9
from ivle.webapp.publisher.decorators import forward_route, reverse_route
 
10
from ivle.webapp.publisher import ROOT
10
11
from ivle.webapp import ApplicationRoot
11
12
 
12
 
def generate_toc(plugins, req):
13
 
    toc = {}
 
13
def generate_toc(plugins):
 
14
    toc = HelpTree(None, None, {})
14
15
    for plugin in plugins:
15
16
        if hasattr(plugin, 'help'):
16
17
            # Get the dir the plugin resides in
20
21
 
21
22
def add_dict(newdict, curdict, plugin):
22
23
    for key in curdict:
23
 
        if key not in newdict:
24
 
            newdict[key] = {}
25
24
        if isinstance(curdict[key], dict):
 
25
            if key not in newdict:
 
26
                newdict[key] = HelpTree(newdict, key, {})
26
27
            add_dict(newdict[key], curdict[key], plugin)
27
28
        else:
28
 
            newdict[key] = os.path.join(plugin, curdict[key])
 
29
            newdict[key] = HelpEntry(newdict, key, os.path.join(plugin, curdict[key]))
29
30
    return newdict
30
31
 
31
32
class HelpTreeView(XHTMLView):
39
40
    def populate(self, req, ctx):
40
41
        self.plugin_styles[Plugin] = ['help.css']
41
42
 
42
 
        ctx['toc'] = self.context.tree
 
43
        ctx['req'] = req
 
44
        ctx['context'] = self.context
 
45
        ctx['HelpTree'] = HelpTree
 
46
        ctx['HelpEntry'] = HelpEntry
43
47
 
44
48
class HelpEntryView(XHTMLView):
45
49
    tab = 'help'
51
55
 
52
56
    def populate(self, req, ctx):
53
57
        self.plugin_styles[Plugin] = ['help.css']
54
 
 
 
58
        
55
59
        ctx['helpfile'] = self.context.file
56
60
 
57
 
class HelpTree(object):
58
 
    def __init__(self, tree):
59
 
        self.tree = tree
 
61
class HelpTree(dict):
 
62
    def __init__(self, parent, name, tree):
 
63
        super(HelpTree, self).__init__(tree)
 
64
        self.parent = parent
 
65
        self.name = name
60
66
 
61
67
class HelpEntry(object):
62
 
    def __init__(self, file):
 
68
    def __init__(self, parent, name, file):
 
69
        self.parent = parent
 
70
        self.name = name
63
71
        self.file = file
64
72
 
65
73
@forward_route(ApplicationRoot, '+help')
66
74
def root_to_helptree(root):
67
 
    return HelpTree(generate_toc(root.config.plugin_index[ViewPlugin], None))
 
75
    return HelpTree(None, None, generate_toc(root.config.plugin_index[ViewPlugin]))
68
76
 
69
77
@forward_route(HelpTree, argc=1)
70
78
def helptree_to_help(help, subhelp_name):
71
79
    try:
72
 
        sub = help.tree[subhelp_name]
73
 
        if type(sub) is dict:
74
 
            return HelpTree(sub)
75
 
        else:
76
 
            return HelpEntry(sub)
 
80
        return help[subhelp_name]
77
81
    except KeyError:
78
82
        # No help entry of that name
79
83
        return None
80
84
 
 
85
@reverse_route(HelpTree)
 
86
def helptree_url(helptree):
 
87
    if helptree.parent is None:
 
88
        return (ROOT, '+help')
 
89
    return (helptree.parent, helptree.name)
 
90
 
 
91
@reverse_route(HelpEntry)
 
92
def helpentry_url(helpentry):
 
93
    return (helpentry.parent, helpentry.name)
 
94
 
81
95
class Plugin(ViewPlugin, MediaPlugin):
82
96
    """The plugin for viewing help files."""
83
97
    forward_routes = (root_to_helptree, helptree_to_help)
 
98
    reverse_routes = (helptree_url, helpentry_url)
 
99
 
84
100
    views = [(HelpTree, '+index', HelpTreeView),
85
101
             (HelpEntry, '+index', HelpEntryView)]
86
102