~zyga/checkbox/fix-1235426

« back to all changes in this revision

Viewing changes to plainbox/plainbox/impl/_textwrap.py

  • Committer: Daniel Manrique
  • Author(s): Zygmunt Krynicki
  • Date: 2014-06-17 00:16:04 UTC
  • mfrom: (3075.1.2 launchpad/deprecated)
  • Revision ID: daniel_manrique-20140617001604-rnvy19kawkii5rsi
"automatic merge by tarmac [r=zkrynicki][bug=][author=zkrynicki]"

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file is part of Checkbox.
 
2
#
 
3
# Copyright 2014 Canonical Ltd.
 
4
# Written by:
 
5
#   Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
 
6
#
 
7
# Checkbox is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License version 3,
 
9
# as published by the Free Software Foundation.
 
10
#
 
11
# Checkbox is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
# Parts copied from Python3.4:
 
20
#   Copyright (C) 1999-2001 Gregory P. Ward.
 
21
#   Copyright (C) 2002, 2003 Python Software Foundation.
 
22
#   Written by Greg Ward <gward@python.net>
 
23
#
 
24
# PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
 
25
# --------------------------------------------
 
26
#
 
27
# 1. This LICENSE AGREEMENT is between the Python Software Foundation ("PSF"),
 
28
#    and the Individual or Organization ("Licensee") accessing and otherwise
 
29
#    using this software ("Python") in source or binary form and its associated
 
30
#    documentation.
 
31
#
 
32
# 2. Subject to the terms and conditions of this License Agreement, PSF hereby
 
33
#    grants Licensee a nonexclusive, royalty-free, world-wide license to
 
34
#    reproduce, analyze, test, perform and/or display publicly, prepare
 
35
#    derivative works, distribute, and otherwise use Python alone or in any
 
36
#    derivative version, provided, however, that PSF's License Agreement and
 
37
#    PSF's notice of copyright, i.e., "Copyright (c) 2001, 2002, 2003, 2004,
 
38
#    2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Python Software
 
39
#    Foundation; All Rights Reserved" are retained in Python alone or in any
 
40
#    derivative version prepared by Licensee.
 
41
#
 
42
# 3. In the event Licensee prepares a derivative work that is based on or
 
43
#    incorporates Python or any part thereof, and wants to make the derivative
 
44
#    work available to others as provided herein, then Licensee hereby agrees
 
45
#    to include in any such work a brief summary of the changes made to Python.
 
46
#
 
47
# 4. PSF is making Python available to Licensee on an "AS IS" basis.  PSF MAKES
 
48
#    NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF EXAMPLE,
 
49
#    BUT NOT LIMITATION, PSF MAKES NO AND DISCLAIMS ANY REPRESENTATION OR
 
50
#    WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT
 
51
#    THE USE OF PYTHON WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.
 
52
#
 
53
# 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON FOR ANY
 
54
#    INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
 
55
#    MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, OR ANY DERIVATIVE
 
56
#    THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
 
57
#
 
58
# 6. This License Agreement will automatically terminate upon a material breach
 
59
#    of its terms and conditions.
 
60
#
 
61
# 7. Nothing in this License Agreement shall be deemed to create any
 
62
#    relationship of agency, partnership, or joint venture between PSF and
 
63
#    Licensee.  This License Agreement does not grant permission to use PSF
 
64
#    trademarks or trade name in a trademark sense to endorse or promote
 
65
#    products or services of Licensee, or any third party.
 
66
#
 
67
# 8. By copying, installing or otherwise using Python, Licensee agrees to be
 
68
#    bound by the terms and conditions of this License Agreement.
 
69
 
 
70
 
 
71
"""
 
72
:mod:`plainbox.impl._textwrap` -- support code for textwrap compatibility
 
73
=========================================================================
 
74
 
 
75
This module contains a copy of textwrap source code from python3.4
 
76
"""
 
77
 
 
78
 
 
79
def _textwrap_indent(text, prefix, predicate=None):
 
80
    """Adds 'prefix' to the beginning of selected lines in 'text'.
 
81
 
 
82
    If 'predicate' is provided, 'prefix' will only be added to the lines
 
83
    where 'predicate(line)' is True. If 'predicate' is not provided,
 
84
    it will default to adding 'prefix' to all non-empty lines that do not
 
85
    consist solely of whitespace characters.
 
86
    """
 
87
    if predicate is None:
 
88
        def predicate(line):
 
89
            return line.strip()
 
90
 
 
91
    def prefixed_lines():
 
92
        for line in text.splitlines(True):
 
93
            yield (prefix + line if predicate(line) else line)
 
94
    return ''.join(prefixed_lines())