1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
"""
ReleaseLog class
Most software packages keep a history of last releases in some form. This
module provides an abstraction for handling such information.
Release history is represented by a ReleaseLog class. Each log contains a
list of history entries in reverse chronological order.
"""
from __future__ import print_function
from __future__ import unicode_literals
import re
import dateparser
class Release(object):
"""Release - a single release history entry object
Attributes:
date -- datetime of the release
version -- release version number
descr -- textual description of the release
"""
date = None
version = None
descr = None
def __init__(self, version, date, descr):
self.version = version
self.date = date
self.descr = descr
def __str__(self):
return "Version %s, released at %s" % (self.version, self.date)
class ReleaseHistory(object):
"""ReleaseHistory - base class for ReleaseLog implementations
Attributes:
history - a list of Release objects
header - a compiled regular expression that returns a match for
history entry heading lines
"""
history = []
def __len__(self):
return len(self.history)
def __getitem__(self, i):
return self.history[i]
def append(self, arg):
"""Appends new release to the end of release history list"""
if isinstance(arg, Release):
self.history.append(arg)
else:
raise TypeError()
def parse_header(self, line):
"""Matches line against the history header regexp. On match, returns
a tuple (date, version). On failure, returns (None, None).
"""
date = None
version = None
m = self.header.match(line)
if m:
version = m.group('version')
date = dateparser.parse(m.group('date'))
return date, version
def __init__(self, lines, **kwargs):
"""Create a new history object from the list of lines. The list is
split into history entries by lines that match the header compiled
regexp. Matches should contain at least two groups:
version - part of line containing the release version
date - part of line containing the release date
The line is matching only if both groups are not None.
Additionally, a line starting with form-feed character (\f) or
containing a line of contiguous equals signs, optionally surrounded
by whitespace, is considered to terminate the current history entry.
Keyword arguments:
start=N
Start from the entry N
stop=N
Stop parsing on Nth entry
count=N
Collect at most N entries
If all three keywords are given, the actual range of history entries
is computed as
[start, min(start+count, stop)]
Entries are numbered from 0.
"""
date = None
version = None
descr = []
start = None
stop = None
count = None
for kw, val in kwargs.items():
if kw == 'start':
start = val
elif kw == 'stop':
stop = val
elif kw == 'count':
count = val
else:
raise TypeError() # FIXME
if count:
if start:
if not (stop and stop < start + count):
stop = start + count
elif stop:
start = stop - count
if start < 0:
start = 0
else:
start = 0
stop = start + count
i = 0
for line in lines:
(d, v) = self.parse_header(line)
if d:
if date:
self.append(Release(version, date, descr))
date = None
version = None
descr = []
i += 1
if start and i <= start:
continue
if stop and i > stop:
break
date = d
version = v
descr = []
elif re.match('^(\f|^\s*=+\s*$)', line):
if date:
self.append(Release(version, date, descr))
date = None
version = None
descr = []
elif date:
descr.append(line)
if date:
self.append(Release(version, date, descr))
def __iter__(self):
for r in self.history:
yield(r)
class ReleaseLog(object):
"""A release log class.
It is a fabric returning actual release history implementation, depending
on the first argument to constructor. Typical usage
cl = ReleaseLog('GNU', lines, count=1)
"""
formatdb = {}
def __new__(cls, fmt, *args, **kwargs):
"""Object constructor:
ReleaseLog(fmt, lines, [start=N], [stop=N], [count=N]
Arguments:
fmt
History log format. E.g. 'GNU' for GNU-style NEWS file, or
'Changes', for CPAN-style Changes file.
lines
List of history lines.
Keyword arguments are the same as in ReleaseHistory.
"""
return cls.formatdb[fmt](*args, **kwargs)
@classmethod
def regformat(cls, fmt, impl):
"""Register a new history format implementation. Typical usage:
ReleaseLog.deftype(format, class)
Arguments:
format
Format name. It will subsequently be used as the format argument
to ReleaseLog constructor in order to require this particular
implementation.
class
Name of the class implementing the format.
"""
if isinstance(fmt, list):
for f in fmt:
cls.regformat(f, impl)
else:
cls.formatdb[fmt] = impl
# Initialize the ReleaseLog implementations
import pkgutil
import importlib
for (loader, name, ispkg) in pkgutil.iter_modules([dir for dir in
map(lambda x: x + '/format',
__path__)]):
importlib.import_module('.' + name, __package__ + '.format')
for cls in ReleaseHistory.__subclasses__():
try:
ReleaseLog.regformat(cls.format, cls)
except AttributeError:
pass
|