~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/filters/eol.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-30 16:43:12 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060730164312-b025fd3ff0cee59e
rename  gpl.txt => COPYING.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 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
 
"""End of Line Conversion filters.
18
 
 
19
 
See bzr help eol for details.
20
 
"""
21
 
 
22
 
 
23
 
import re, sys
24
 
 
25
 
from bzrlib.errors import BzrError
26
 
 
27
 
 
28
 
# Real Unix newline - \n without \r before it
29
 
_UNIX_NL_RE = re.compile(r'(?<!\r)\n')
30
 
 
31
 
 
32
 
def _to_lf_converter(chunks, context=None):
33
 
    """A content file that converts crlf to lf."""
34
 
    content = ''.join(chunks)
35
 
    if '\x00' in content:
36
 
        return [content]
37
 
    else:
38
 
        return [content.replace('\r\n', '\n')]
39
 
 
40
 
 
41
 
def _to_crlf_converter(chunks, context=None):
42
 
    """A content file that converts lf to crlf."""
43
 
    content = ''.join(chunks)
44
 
    if '\x00' in content:
45
 
        return [content]
46
 
    else:
47
 
        return [_UNIX_NL_RE.sub('\r\n', content)]
48
 
 
49
 
 
50
 
# Register the eol content filter.
51
 
def register_eol_content_filter():
52
 
    from bzrlib.filters import ContentFilter, register_filter_stack_map
53
 
 
54
 
    if sys.platform == 'win32':
55
 
        _native_output = _to_crlf_converter
56
 
    else:
57
 
        _native_output = _to_lf_converter
58
 
    _eol_filter_stack_map = {
59
 
        'exact': [],
60
 
        'native': [ContentFilter(_to_lf_converter, _native_output)],
61
 
        'lf':     [ContentFilter(_to_lf_converter, _to_lf_converter)],
62
 
        'crlf':   [ContentFilter(_to_lf_converter, _to_crlf_converter)],
63
 
        'native-with-crlf-in-repo':
64
 
            [ContentFilter(_to_crlf_converter, _native_output)],
65
 
        'lf-with-crlf-in-repo':
66
 
            [ContentFilter(_to_crlf_converter, _to_lf_converter)],
67
 
        'crlf-with-crlf-in-repo':
68
 
            [ContentFilter(_to_crlf_converter, _to_crlf_converter)],
69
 
        }
70
 
    def eol_lookup(key):
71
 
        filter = _eol_filter_stack_map.get(key)
72
 
        if filter is None:
73
 
            raise BzrError("Unknown eol value '%s'" % key)
74
 
        return filter
75
 
    register_filter_stack_map('eol', eol_lookup)