Changeset - a455b2c79c85
[Not reviewed]
beta
0 3 0
Marcin Kuzminski - 13 years ago 2012-06-04 18:40:39
marcin@python-works.com
Improved RSS/ATOM feeds
- rewrote using new diff lib
- added shorter commit as title and full commit message in body
- increased feed size from 10 to 20 entries
3 files changed with 67 insertions and 45 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/api/api.py
Show inline comments
 
@@ -541,12 +541,21 @@ class ApiController(JSONRPCController):
 

	
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            raise JSONRPCError('failed to create repository %s' % repo_name)
 

	
 
    @HasPermissionAnyDecorator('hg.admin')
 
    def fork_repo(self, apiuser, repoid):
 
        repo = RepoModel().get_repo(repoid)
 
        if repo is None:
 
            raise JSONRPCError('unknown repository "%s"' % (repo or repoid))
 

	
 
        RepoModel().create_fork(form_data, cur_user)
 

	
 

	
 
    @HasPermissionAnyDecorator('hg.admin')
 
    def delete_repo(self, apiuser, repo_name):
 
        """
 
        Deletes a given repository
 

	
 
        :param repo_name:
 
        """
rhodecode/controllers/feed.py
Show inline comments
 
@@ -25,17 +25,18 @@
 

	
 
import logging
 

	
 
from pylons import url, response, tmpl_context as c
 
from pylons.i18n.translation import _
 

	
 
from rhodecode.lib.utils2 import safe_unicode
 
from webhelpers.feedgenerator import Atom1Feed, Rss201rev2Feed
 

	
 
from rhodecode.lib import helpers as h
 
from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator
 
from rhodecode.lib.base import BaseRepoController
 

	
 
from webhelpers.feedgenerator import Atom1Feed, Rss201rev2Feed
 
from rhodecode.lib.diffs import DiffProcessor
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
class FeedController(BaseRepoController):
 

	
 
@@ -46,37 +47,42 @@ class FeedController(BaseRepoController)
 
        super(FeedController, self).__before__()
 
        #common values for feeds
 
        self.description = _('Changes on %s repository')
 
        self.title = self.title = _('%s %s feed') % (c.rhodecode_name, '%s')
 
        self.language = 'en-us'
 
        self.ttl = "5"
 
        self.feed_nr = 10
 
        self.feed_nr = 20
 

	
 
    def _get_title(self, cs):
 
        return "R%s:%s - %s" % (
 
            cs.revision, cs.short_id, cs.message
 
        return "%s" % (
 
            h.shorter(cs.message, 160)
 
        )
 

	
 
    def __changes(self, cs):
 
        changes = []
 

	
 
        a = [safe_unicode(n.path) for n in cs.added]
 
        if a:
 
            changes.append('\nA ' + '\nA '.join(a))
 

	
 
        m = [safe_unicode(n.path) for n in cs.changed]
 
        if m:
 
            changes.append('\nM ' + '\nM '.join(m))
 
        diffprocessor = DiffProcessor(cs.diff())
 
        stats = diffprocessor.prepare(inline_diff=False)
 
        for st in stats:
 
            st.update({'added': st['stats'][0],
 
                       'removed': st['stats'][1]})
 
            changes.append('\n %(operation)s %(filename)s '
 
                           '(%(added)s lines added, %(removed)s lines removed)'
 
                            % st)
 
        return changes
 

	
 
        d = [safe_unicode(n.path) for n in cs.removed]
 
        if d:
 
            changes.append('\nD ' + '\nD '.join(d))
 

	
 
        changes.append('</pre>')
 

	
 
        return ''.join(changes)
 
    def __get_desc(self, cs):
 
        desc_msg = []
 
        desc_msg.append('%s %s %s:<br/>' % (cs.author, _('commited on'),
 
                                           cs.date))
 
        desc_msg.append('<pre>')
 
        desc_msg.append(cs.message)
 
        desc_msg.append('\n')
 
        desc_msg.extend(self.__changes(cs))
 
        desc_msg.append('</pre>')
 
        return desc_msg
 

	
 
    def atom(self, repo_name):
 
        """Produce an atom-1.0 feed via feedgenerator module"""
 
        feed = Atom1Feed(
 
             title=self.title % repo_name,
 
             link=url('summary_home', repo_name=repo_name,
 
@@ -84,21 +90,19 @@ class FeedController(BaseRepoController)
 
             description=self.description % repo_name,
 
             language=self.language,
 
             ttl=self.ttl
 
        )
 

	
 
        for cs in reversed(list(c.rhodecode_repo[-self.feed_nr:])):
 
            desc_msg = []
 
            desc_msg.append('%s - %s<br/><pre>' % (cs.author, cs.date))
 
            desc_msg.append(self.__changes(cs))
 

	
 
            feed.add_item(title=self._get_title(cs),
 
                          link=url('changeset_home', repo_name=repo_name,
 
                                   revision=cs.raw_id, qualified=True),
 
                          author_name=cs.author,
 
                          description=''.join(desc_msg))
 
                          description=''.join(self.__get_desc(cs)),
 
                          pubdate=cs.date,
 
                          )
 

	
 
        response.content_type = feed.mime_type
 
        return feed.writeString('utf-8')
 

	
 
    def rss(self, repo_name):
 
        """Produce an rss2 feed via feedgenerator module"""
 
@@ -109,19 +113,16 @@ class FeedController(BaseRepoController)
 
            description=self.description % repo_name,
 
            language=self.language,
 
            ttl=self.ttl
 
        )
 

	
 
        for cs in reversed(list(c.rhodecode_repo[-self.feed_nr:])):
 
            desc_msg = []
 
            desc_msg.append('%s - %s<br/><pre>' % (cs.author, cs.date))
 
            desc_msg.append(self.__changes(cs))
 

	
 
            feed.add_item(title=self._get_title(cs),
 
                          link=url('changeset_home', repo_name=repo_name,
 
                                   revision=cs.raw_id, qualified=True),
 
                          author_name=cs.author,
 
                          description=''.join(desc_msg),
 
                          description=''.join(self.__get_desc(cs)),
 
                          pubdate=cs.date,
 
                         )
 

	
 
        response.content_type = feed.mime_type
 
        return feed.writeString('utf-8')
rhodecode/lib/diffs.py
Show inline comments
 
@@ -125,13 +125,13 @@ class DiffProcessor(object):
 
    Give it a unified diff and it returns a list of the files that were
 
    mentioned in the diff together with a dict of meta information that
 
    can be used to render it in a HTML template.
 
    """
 
    _chunk_re = re.compile(r'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)')
 

	
 
    def __init__(self, diff, differ='diff', format='udiff'):
 
    def __init__(self, diff, differ='diff', format='gitdiff'):
 
        """
 
        :param diff:   a text in diff format or generator
 
        :param format: format of diff passed, `udiff` or `gitdiff`
 
        """
 
        if isinstance(diff, basestring):
 
            diff = [diff]
 
@@ -168,13 +168,13 @@ class DiffProcessor(object):
 
        """
 
        self.__udiff, iterator_copy = tee(self.__udiff)
 
        return iterator_copy
 

	
 
    def _extract_rev(self, line1, line2):
 
        """
 
        Extract the filename and revision hint from a line.
 
        Extract the operation (A/M/D), filename and revision hint from a line.
 
        """
 

	
 
        try:
 
            if line1.startswith('--- ') and line2.startswith('+++ '):
 
                l1 = line1[4:].split(None, 1)
 
                old_filename = (l1[0].replace('a/', '', 1)
 
@@ -186,17 +186,21 @@ class DiffProcessor(object):
 
                                if len(l1) >= 1 else None)
 
                new_rev = l2[1] if len(l2) == 2 else 'new'
 

	
 
                filename = (old_filename
 
                            if old_filename != '/dev/null' else new_filename)
 

	
 
                return filename, new_rev, old_rev
 
                operation = 'D' if new_filename == '/dev/null' else None
 
                if not operation:
 
                    operation = 'M' if old_filename != '/dev/null' else 'A'
 

	
 
                return operation, filename, new_rev, old_rev
 
        except (ValueError, IndexError):
 
            pass
 

	
 
        return None, None, None
 
        return None, None, None, None
 

	
 
    def _parse_gitdiff(self, diffiterator):
 
        def line_decoder(l):
 
            if l.startswith('+') and not l.startswith('+++'):
 
                self.adds += 1
 
            elif l.startswith('-') and not l.startswith('---'):
 
@@ -275,13 +279,13 @@ class DiffProcessor(object):
 
                    tag,
 
                    l['line'][last:]
 
                )
 
            do(line)
 
            do(next_)
 

	
 
    def _parse_udiff(self):
 
    def _parse_udiff(self, inline_diff=True):
 
        """
 
        Parse the diff an return data for the template.
 
        """
 
        lineiter = self.lines
 
        files = []
 
        try:
 
@@ -290,19 +294,22 @@ class DiffProcessor(object):
 
                # continue until we found the old file
 
                if not line.startswith('--- '):
 
                    line = lineiter.next()
 
                    continue
 

	
 
                chunks = []
 
                filename, old_rev, new_rev = \
 
                stats = [0, 0]
 
                operation, filename, old_rev, new_rev = \
 
                    self._extract_rev(line, lineiter.next())
 
                files.append({
 
                    'filename':         filename,
 
                    'old_revision':     old_rev,
 
                    'new_revision':     new_rev,
 
                    'chunks':           chunks
 
                    'chunks':           chunks,
 
                    'operation':        operation,
 
                    'stats':            stats,
 
                })
 

	
 
                line = lineiter.next()
 
                while line:
 
                    match = self._chunk_re.match(line)
 
                    if not match:
 
@@ -328,13 +335,12 @@ class DiffProcessor(object):
 
                                'new_lineno': '...',
 
                                'action':     'context',
 
                                'line':       line,
 
                            })
 

	
 
                    line = lineiter.next()
 

	
 
                    while old_line < old_end or new_line < new_end:
 
                        if line:
 
                            command, line = line[0], line[1:]
 
                        else:
 
                            command = ' '
 
                        affects_old = affects_new = False
 
@@ -342,15 +348,17 @@ class DiffProcessor(object):
 
                        # ignore those if we don't expect them
 
                        if command in '#@':
 
                            continue
 
                        elif command == '+':
 
                            affects_new = True
 
                            action = 'add'
 
                            stats[0] += 1
 
                        elif command == '-':
 
                            affects_old = True
 
                            action = 'del'
 
                            stats[1] += 1
 
                        else:
 
                            affects_old = affects_new = True
 
                            action = 'unmod'
 

	
 
                        if line.find('No newline at end of file') != -1:
 
                            lines.append({
 
@@ -368,19 +376,22 @@ class DiffProcessor(object):
 
                                'new_lineno':   affects_new and new_line or '',
 
                                'action':       action,
 
                                'line':         line
 
                            })
 

	
 
                        line = lineiter.next()
 

	
 
        except StopIteration:
 
            pass
 

	
 
        sorter = lambda info: {'A': 0, 'M': 1, 'D': 2}.get(info['operation'])
 
        if inline_diff is False:
 
            return sorted(files, key=sorter)
 

	
 
        # highlight inline changes
 
        for _ in files:
 
            for chunk in chunks:
 
        for diff_data in files:
 
            for chunk in diff_data['chunks']:
 
                lineiter = iter(chunk)
 
                try:
 
                    while 1:
 
                        line = lineiter.next()
 
                        if line['action'] != 'unmod':
 
                            nextline = lineiter.next()
 
@@ -388,20 +399,20 @@ class DiffProcessor(object):
 
                               nextline['action'] == line['action']:
 
                                continue
 
                            self.differ(line, nextline)
 
                except StopIteration:
 
                    pass
 

	
 
        return files
 
        return sorted(files, key=sorter)
 

	
 
    def prepare(self):
 
    def prepare(self, inline_diff=True):
 
        """
 
        Prepare the passed udiff for HTML rendering. It'l return a list
 
        of dicts
 
        """
 
        return self._parse_udiff()
 
        return self._parse_udiff(inline_diff=inline_diff)
 

	
 
    def _safe_id(self, idstring):
 
        """Make a string safe for including in an id attribute.
 

	
 
        The HTML spec says that id attributes 'must begin with
 
        a letter ([A-Za-z]) and may be followed by any number
 
@@ -429,13 +440,13 @@ class DiffProcessor(object):
 
        if self.__format == 'gitdiff':
 
            udiff_copy = self._parse_gitdiff(udiff_copy)
 
        return u''.join(udiff_copy)
 

	
 
    def as_html(self, table_class='code-difftable', line_class='line',
 
                new_lineno_class='lineno old', old_lineno_class='lineno new',
 
                code_class='code', enable_comments=False):
 
                code_class='code', enable_comments=False, diff_lines=None):
 
        """
 
        Return udiff as html table with customized css classes
 
        """
 
        def _link_to_if(condition, label, url):
 
            """
 
            Generates a link if condition is meet or just the label if not.
 
@@ -445,12 +456,13 @@ class DiffProcessor(object):
 
                return '''<a href="%(url)s">%(label)s</a>''' % {
 
                    'url': url,
 
                    'label': label
 
                }
 
            else:
 
                return label
 
        if diff_lines is None:
 
        diff_lines = self.prepare()
 
        _html_empty = True
 
        _html = []
 
        _html.append('''<table class="%(table_class)s">\n''' % {
 
            'table_class': table_class
 
        })
0 comments (0 inline, 0 general)