source: tailor/vcpx/changes.py @ 22

Revision 22, 3.4 KB checked in by lele@…, 9 years ago (diff)

Apply the refill to every changelog

Line 
1#! /usr/bin/python
2# -*- mode: python; coding: utf-8 -*-
3# :Progetto: vcpx -- Changesets
4# :Creato:   ven 11 giu 2004 15:31:18 CEST
5# :Autore:   Lele Gaifax <lele@nautilus.homeip.net>
6#
7
8"""
9Changesets are an object representation of a set of changes to some files.
10"""
11
12__docformat__ = 'reStructuredText'
13
14class ChangesetEntry(object):
15    """
16    Represent a changed entry in a Changeset.
17
18    For our scope, this simply means an entry `name`, the original
19    `old_revision`, the `new_revision` after this change, an
20    `action_kind` to denote the kind of change, and finally a `status`
21    to indicate possible conflicts.
22    """
23   
24    ADDED = 'A'
25    DELETED = 'D'
26    UPDATED = 'U'
27    RENAMED = 'R'
28
29    APPLIED = 'A'
30    CONFLICT = 'C'
31   
32    __slots__ = ('name', 'old_name',
33                 'old_revision', 'new_revision',
34                 'action_kind', 'status')
35
36    def __init__(self, name):
37        self.name = name
38        self.old_name = None
39        self.old_revision = None
40        self.new_revision = None
41        self.action_kind = None
42        self.status = None
43
44    def __str__(self):
45        return "%s %s->%s" % (self.name, self.old_revision, self.new_revision)
46
47
48from textwrap import TextWrapper
49from re import compile, MULTILINE
50   
51itemize_re = compile('^[ ]*[-*] ', MULTILINE)
52
53def refill(msg):
54    wrapper = TextWrapper()
55    s = []
56    items = itemize_re.split(msg)
57    if len(items)>1:
58        if len(items)>2:
59            if items[0]:
60                wrapper.initial_indent = ' - '
61                wrapper.subsequent_indent = ' '*3
62            else:
63                del items[0]
64               
65    for m in items:
66        if m:
67            s.append(wrapper.fill(' '.join(filter(None, m.split(' ')))))
68            s.append('')
69
70    return '\n'.join(s)
71
72
73class Changeset(object):
74    """
75    Represent a single upstream Changeset.
76
77    This is a container of each file affected by this revision of the tree.
78    """
79
80    def __init__(self, revision, date, author, log, entries, **other):
81        """
82        Initialize a new ChangeSet.
83        """
84       
85        self.revision = revision
86        self.date = date
87        self.author = author
88        self.log = refill(log)
89        self.entries = entries
90
91    def __str__(self):
92        s = []
93        s.append('Revision: %s' % self.revision)
94        s.append('Date: %s' % self.date)
95        s.append('Author: %s' % self.author)
96        for ak in ['Added', 'Modified', 'Removed', 'Renamed']:
97            entries = getattr(self, ak.lower()+'Entries')()
98            if entries:
99                s.append('%s: %s' % (ak, ','.join([e.name
100                                                   for e in self.entries])))
101        s.append('Log: %s' % self.log)
102
103    def addedEntries(self):
104        """
105        Filter the changesets and extract the added entries.
106        """
107       
108        return [e for e in self.entries if e.action_kind == e.ADDED]
109
110    def modifiedEntries(self):
111        """
112        Filter the changesets and extract the modified entries.
113        """
114
115        return [e for e in self.entries if e.action_kind == e.UPDATE]
116
117    def removedEntries(self):
118        """
119        Filter the changesets and extract the deleted entries.
120        """
121
122        return [e for e in self.entries if e.action_kind == e.DELETED]
123
124    def renamedEntries(self):
125        """
126        Filter the changesets and extract the renamed entries.
127        """
128
129        return [e for e in self.entries if e.action_kind == e.RENAMED]
Note: See TracBrowser for help on using the repository browser.