|
| 1 | +"""Functions brought over from jaraco.text. |
| 2 | +
|
| 3 | +These functions are not supposed to be used within `pip._internal`. These are |
| 4 | +helper functions brought over from `jaraco.text` to enable vendoring newer |
| 5 | +copies of `pkg_resources` without having to vendor `jaraco.text` and its entire |
| 6 | +dependency cone; something that our vendoring setup is not currently capable of |
| 7 | +handling. |
| 8 | +
|
| 9 | +License reproduced from original source below: |
| 10 | +
|
| 11 | +Copyright Jason R. Coombs |
| 12 | +
|
| 13 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +of this software and associated documentation files (the "Software"), to |
| 15 | +deal in the Software without restriction, including without limitation the |
| 16 | +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 17 | +sell copies of the Software, and to permit persons to whom the Software is |
| 18 | +furnished to do so, subject to the following conditions: |
| 19 | +
|
| 20 | +The above copyright notice and this permission notice shall be included in |
| 21 | +all copies or substantial portions of the Software. |
| 22 | +
|
| 23 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 28 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 29 | +IN THE SOFTWARE. |
| 30 | +""" |
| 31 | + |
| 32 | +import functools |
| 33 | +import itertools |
| 34 | + |
| 35 | + |
| 36 | +def _nonblank(str): |
| 37 | + return str and not str.startswith("#") |
| 38 | + |
| 39 | + |
| 40 | +@functools.singledispatch |
| 41 | +def yield_lines(iterable): |
| 42 | + r""" |
| 43 | + Yield valid lines of a string or iterable. |
| 44 | +
|
| 45 | + >>> list(yield_lines('')) |
| 46 | + [] |
| 47 | + >>> list(yield_lines(['foo', 'bar'])) |
| 48 | + ['foo', 'bar'] |
| 49 | + >>> list(yield_lines('foo\nbar')) |
| 50 | + ['foo', 'bar'] |
| 51 | + >>> list(yield_lines('\nfoo\n#bar\nbaz #comment')) |
| 52 | + ['foo', 'baz #comment'] |
| 53 | + >>> list(yield_lines(['foo\nbar', 'baz', 'bing\n\n\n'])) |
| 54 | + ['foo', 'bar', 'baz', 'bing'] |
| 55 | + """ |
| 56 | + return itertools.chain.from_iterable(map(yield_lines, iterable)) |
| 57 | + |
| 58 | + |
| 59 | +@yield_lines.register(str) |
| 60 | +def _(text): |
| 61 | + return filter(_nonblank, map(str.strip, text.splitlines())) |
| 62 | + |
| 63 | + |
| 64 | +def drop_comment(line): |
| 65 | + """ |
| 66 | + Drop comments. |
| 67 | +
|
| 68 | + >>> drop_comment('foo # bar') |
| 69 | + 'foo' |
| 70 | +
|
| 71 | + A hash without a space may be in a URL. |
| 72 | +
|
| 73 | + >>> drop_comment('http://example.com/foo#bar') |
| 74 | + 'http://example.com/foo#bar' |
| 75 | + """ |
| 76 | + return line.partition(" #")[0] |
| 77 | + |
| 78 | + |
| 79 | +def join_continuation(lines): |
| 80 | + r""" |
| 81 | + Join lines continued by a trailing backslash. |
| 82 | +
|
| 83 | + >>> list(join_continuation(['foo \\', 'bar', 'baz'])) |
| 84 | + ['foobar', 'baz'] |
| 85 | + >>> list(join_continuation(['foo \\', 'bar', 'baz'])) |
| 86 | + ['foobar', 'baz'] |
| 87 | + >>> list(join_continuation(['foo \\', 'bar \\', 'baz'])) |
| 88 | + ['foobarbaz'] |
| 89 | +
|
| 90 | + Not sure why, but... |
| 91 | + The character preceeding the backslash is also elided. |
| 92 | +
|
| 93 | + >>> list(join_continuation(['goo\\', 'dly'])) |
| 94 | + ['godly'] |
| 95 | +
|
| 96 | + A terrible idea, but... |
| 97 | + If no line is available to continue, suppress the lines. |
| 98 | +
|
| 99 | + >>> list(join_continuation(['foo', 'bar\\', 'baz\\'])) |
| 100 | + ['foo'] |
| 101 | + """ |
| 102 | + lines = iter(lines) |
| 103 | + for item in lines: |
| 104 | + while item.endswith("\\"): |
| 105 | + try: |
| 106 | + item = item[:-2].strip() + next(lines) |
| 107 | + except StopIteration: |
| 108 | + return |
| 109 | + yield item |
0 commit comments