_jaraco_text.py 3.3 KB

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