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

1099.1.54 by William Grant
ivle.webapp.filesystem.svnlog: Port www/apps/svnlog to new framework. As with
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
562 by dcoles
Added new app: Diff (SVN diff application)
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
18
# Author: David Coles, Will Grant
19
1099.1.57 by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place.
20
'''Components of the webapp for diffing user files.'''
21
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
22
import os
23
import re
24
import cgi
25
26
import cjson
27
import genshi
562 by dcoles
Added new app: Diff (SVN diff application)
28
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
29
import ivle.interpret
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
30
from ivle.webapp.base.xhtml import XHTMLView
1099.1.99 by William Grant
Require that plugins providing media subclass MediaPlugin.
31
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
1099.1.50 by William Grant
ivle.webapp.filesystem.diff: Import BadRequest; it was used.
32
from ivle.webapp.errors import NotFound, BadRequest
1254 by William Grant
Replace DiffView's titles too.
33
from ivle.webapp.filesystem import make_path_segments
562 by dcoles
Added new app: Diff (SVN diff application)
34
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
35
class DiffView(XHTMLView):
1099.1.57 by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place.
36
    '''A view to present a nice XHTML Subversion diff from a user's jail.'''
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
37
    template = 'template.html'
1251 by William Grant
Put Subversion diffs and logs in the Files tab.
38
    tab = 'files'
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
39
1099.1.57 by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place.
40
    def __init__(self, req, path):
41
        self.path = path
42
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
43
    def authorize(self, req):
44
        return req.user is not None
45
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
46
    def populate(self, req, ctx):
1099.1.67 by William Grant
Port ivle.webapp.filesystem.{diff,svnlog}'s media to the new framework.
47
        self.plugin_styles[Plugin] = ['diff.css']
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
48
49
        revfields = req.get_fieldstorage().getlist("r")
50
        if len(revfields) > 2:
51
            raise BadRequest('A maximum of two revisions can be given.')
52
53
        revs = [revfield.value for revfield in revfields]
54
1220 by William Grant
Remove ivle.conf dependency from ivle.webapp.filesystem.diff.
55
        jail_dir = os.path.join(req.config['paths']['jails']['mounts'],
56
                                req.user.login)
1276 by William Grant
Drop ivle.conf usage from ivle.interpret.
57
        (out, err) = ivle.interpret.execute_raw(req.config, req.user, jail_dir,
58
                            '/home', os.path.join(req.config['paths']['share'],
59
                                                  'services/diffservice'),
60
                            [self.path] + revs
61
                            )
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
62
        assert not err
63
64
        response = cjson.decode(out)
65
        if 'error' in response:
66
            if response['error'] == 'notfound':
67
                raise NotFound()
68
            else:
69
                raise AssertionError('Unknown error from diffservice: %s' %
70
                                     response['error'])
71
72
        # No error. We must be safe.
73
        diff = response['diff']
74
75
        # Split up the udiff into individual files
76
        diff_matcher = re.compile(
77
            r'^Index: (.*)\n\=+\n((?:[^I].*\n)*)',re.MULTILINE
78
        )
79
1291 by William Grant
Fix browser/diff/svnlog titles for paths with trailing slashes.
80
        ctx['title'] = os.path.normpath(self.path).rsplit('/', 1)[-1]
1254 by William Grant
Replace DiffView's titles too.
81
        ctx['paths'] = make_path_segments(self.path)
1173 by William Grant
Show the filename in a diff view.
82
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
83
        # Create a dict with (name, HTMLdiff) pairs for each non-empty diff.
84
        ctx['files'] = dict([(fd[0], genshi.XML(htmlfy_diff(fd[1])))
85
                             for fd in diff_matcher.findall(diff)
86
                             if fd[1]])
87
88
89
def htmlfy_diff(difftext):
90
    """Adds HTML markup to a udiff string"""
91
    output = cgi.escape(difftext)
92
    subs = {
93
     r'^([\+\-]{3})\s(\S+)\s\((.+)\)$':
94
         r'<span class="diff-files">\1 \2 <em>(\3)</em></span>',
95
     r'^\@\@ (.*) \@\@$':
96
         r'<span class="diff-range">@@ \1 @@</span>',
97
     r'^\+(.*)$':
98
         r'<span class="diff-add">+\1</span>',
99
     r'^\-(.*)$':
100
         r'<span class="diff-sub">-\1</span>',
101
     r'^\\(.*)$':
102
         r'<span class="diff-special">\\\1</span>'
103
    }
104
105
    for match in subs:
106
        output = re.compile(match, re.MULTILINE).sub(subs[match], output)
107
108
    return '<pre class="diff">%s</pre>' % output
109
1099.1.99 by William Grant
Require that plugins providing media subclass MediaPlugin.
110
class Plugin(ViewPlugin, MediaPlugin):
1099.1.57 by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place.
111
    '''Registration class for diff components.'''
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
112
    urls = [
113
        ('diff/', DiffView, {'path': ''}),
114
        ('diff/*(path)', DiffView),
115
    ]
1099.1.67 by William Grant
Port ivle.webapp.filesystem.{diff,svnlog}'s media to the new framework.
116
117
    media = 'media'