~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/news_merge/parser.py

  • Committer: Robert Collins
  • Date: 2010-06-25 20:34:05 UTC
  • mto: This revision was merged to the branch mainline in revision 5324.
  • Revision ID: robertc@robertcollins.net-20100625203405-c74lxd3enklhaqf9
``bzrlib.osutils.get_terminal_encoding`` will now only mutter its
selection when explicitly requested; this avoids many duplicate calls
being logged when helpers, wrappers and older code that manually calls
it are executed it is now logged deliberately by the ui setup code.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Simple parser for bzr's NEWS file.
 
18
 
 
19
Simple as this is, it's a bit over-powered for news_merge's needs, which only
 
20
cares about 'bullet' and 'everything else'.
 
21
 
 
22
This module can be run as a standalone Python program; pass it a filename and
 
23
it will print the parsed form of a file (a series of 2-tuples, see
 
24
simple_parse's docstring).
 
25
"""
 
26
 
 
27
 
 
28
def simple_parse_lines(lines):
 
29
    """Same as simple_parse, but takes an iterable of strs rather than a single
 
30
    str.
 
31
    """
 
32
    return simple_parse(''.join(lines))
 
33
 
 
34
 
 
35
def simple_parse(content):
 
36
    """Returns blocks, where each block is a 2-tuple (kind, text).
 
37
    
 
38
    :kind: one of 'heading', 'release', 'section', 'empty' or 'text'.
 
39
    :text: a str, including newlines.
 
40
    """
 
41
    blocks = content.split('\n\n')
 
42
    for block in blocks:
 
43
        if block.startswith('###'):
 
44
            # First line is ###...: Top heading
 
45
            yield 'heading', block
 
46
            continue
 
47
        last_line = block.rsplit('\n', 1)[-1]
 
48
        if last_line.startswith('###'):
 
49
            # last line is ###...: 2nd-level heading
 
50
            yield 'release', block
 
51
        elif last_line.startswith('***'):
 
52
            # last line is ***...: 3rd-level heading
 
53
            yield 'section', block
 
54
        elif block.startswith('* '):
 
55
            # bullet
 
56
            yield 'bullet', block
 
57
        elif block.strip() == '':
 
58
            # empty
 
59
            yield 'empty', block
 
60
        else:
 
61
            # plain text
 
62
            yield 'text', block
 
63
 
 
64
 
 
65
if __name__ == '__main__':
 
66
    import sys
 
67
    content = open(sys.argv[1], 'rb').read()
 
68
    for result in simple_parse(content):
 
69
        print result