~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
562 by dcoles
Added new app: Diff (SVN diff application)
33
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
34
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.
35
    '''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
36
    template = 'template.html'
37
1099.1.57 by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place.
38
    def __init__(self, req, path):
39
        self.path = path
40
1099.1.110 by William Grant
Implement an authorization system in the new framework. This breaks the REST
41
    def authorize(self, req):
42
        return req.user is not None
43
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
44
    def populate(self, req, ctx):
1099.1.67 by William Grant
Port ivle.webapp.filesystem.{diff,svnlog}'s media to the new framework.
45
        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
46
47
        revfields = req.get_fieldstorage().getlist("r")
48
        if len(revfields) > 2:
49
            raise BadRequest('A maximum of two revisions can be given.')
50
51
        revs = [revfield.value for revfield in revfields]
52
1220 by William Grant
Remove ivle.conf dependency from ivle.webapp.filesystem.diff.
53
        jail_dir = os.path.join(req.config['paths']['jails']['mounts'],
54
                                req.user.login)
1099.1.57 by William Grant
ivle.webapp.filesystem.diff: Add some docstrings, and remove a constant which was only used in one place.
55
        (out, err) = ivle.interpret.execute_raw(req.user, jail_dir, '/home',
1220 by William Grant
Remove ivle.conf dependency from ivle.webapp.filesystem.diff.
56
                                    os.path.join(req.config['paths']['share'],
57
                                                 'services/diffservice'),
58
                                    [self.path] + revs)
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
59
        assert not err
60
61
        response = cjson.decode(out)
62
        if 'error' in response:
63
            if response['error'] == 'notfound':
64
                raise NotFound()
65
            else:
66
                raise AssertionError('Unknown error from diffservice: %s' %
67
                                     response['error'])
68
69
        # No error. We must be safe.
70
        diff = response['diff']
71
72
        # Split up the udiff into individual files
73
        diff_matcher = re.compile(
74
            r'^Index: (.*)\n\=+\n((?:[^I].*\n)*)',re.MULTILINE
75
        )
76
1173 by William Grant
Show the filename in a diff view.
77
        ctx['title'] = self.path.rsplit('/', 1)[-1]
78
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
79
        # Create a dict with (name, HTMLdiff) pairs for each non-empty diff.
80
        ctx['files'] = dict([(fd[0], genshi.XML(htmlfy_diff(fd[1])))
81
                             for fd in diff_matcher.findall(diff)
82
                             if fd[1]])
83
84
85
def htmlfy_diff(difftext):
86
    """Adds HTML markup to a udiff string"""
87
    output = cgi.escape(difftext)
88
    subs = {
89
     r'^([\+\-]{3})\s(\S+)\s\((.+)\)$':
90
         r'<span class="diff-files">\1 \2 <em>(\3)</em></span>',
91
     r'^\@\@ (.*) \@\@$':
92
         r'<span class="diff-range">@@ \1 @@</span>',
93
     r'^\+(.*)$':
94
         r'<span class="diff-add">+\1</span>',
95
     r'^\-(.*)$':
96
         r'<span class="diff-sub">-\1</span>',
97
     r'^\\(.*)$':
98
         r'<span class="diff-special">\\\1</span>'
99
    }
100
101
    for match in subs:
102
        output = re.compile(match, re.MULTILINE).sub(subs[match], output)
103
104
    return '<pre class="diff">%s</pre>' % output
105
1099.1.99 by William Grant
Require that plugins providing media subclass MediaPlugin.
106
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.
107
    '''Registration class for diff components.'''
1099.1.48 by William Grant
ivle.webapp.filesystem.diff: Port www/apps/diff to new framework. This moves
108
    urls = [
109
        ('diff/', DiffView, {'path': ''}),
110
        ('diff/*(path)', DiffView),
111
    ]
1099.1.67 by William Grant
Port ivle.webapp.filesystem.{diff,svnlog}'s media to the new framework.
112
113
    media = 'media'