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