/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

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
from __future__ import absolute_import
 
28
 
 
29
 
 
30
def simple_parse_lines(lines):
 
31
    """Same as simple_parse, but takes an iterable of strs rather than a single
 
32
    str.
 
33
    """
 
34
    return simple_parse(''.join(lines))
 
35
 
 
36
 
 
37
def simple_parse(content):
 
38
    """Returns blocks, where each block is a 2-tuple (kind, text).
 
39
    
 
40
    :kind: one of 'heading', 'release', 'section', 'empty' or 'text'.
 
41
    :text: a str, including newlines.
 
42
    """
 
43
    blocks = content.split('\n\n')
 
44
    for block in blocks:
 
45
        if block.startswith('###'):
 
46
            # First line is ###...: Top heading
 
47
            yield 'heading', block
 
48
            continue
 
49
        last_line = block.rsplit('\n', 1)[-1]
 
50
        if last_line.startswith('###'):
 
51
            # last line is ###...: 2nd-level heading
 
52
            yield 'release', block
 
53
        elif last_line.startswith('***'):
 
54
            # last line is ***...: 3rd-level heading
 
55
            yield 'section', block
 
56
        elif block.startswith('* '):
 
57
            # bullet
 
58
            yield 'bullet', block
 
59
        elif block.strip() == '':
 
60
            # empty
 
61
            yield 'empty', block
 
62
        else:
 
63
            # plain text
 
64
            yield 'text', block
 
65
 
 
66
 
 
67
if __name__ == '__main__':
 
68
    import sys
 
69
    content = open(sys.argv[1], 'rb').read()
 
70
    for result in simple_parse(content):
 
71
        print result