Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/utils/_jaraco_text.py: 81%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 06:33 +0000

1"""Functions brought over from jaraco.text. 

2 

3These functions are not supposed to be used within `pip._internal`. These are 

4helper functions brought over from `jaraco.text` to enable vendoring newer 

5copies of `pkg_resources` without having to vendor `jaraco.text` and its entire 

6dependency cone; something that our vendoring setup is not currently capable of 

7handling. 

8 

9License reproduced from original source below: 

10 

11Copyright Jason R. Coombs 

12 

13Permission is hereby granted, free of charge, to any person obtaining a copy 

14of this software and associated documentation files (the "Software"), to 

15deal in the Software without restriction, including without limitation the 

16rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 

17sell copies of the Software, and to permit persons to whom the Software is 

18furnished to do so, subject to the following conditions: 

19 

20The above copyright notice and this permission notice shall be included in 

21all copies or substantial portions of the Software. 

22 

23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

24IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

25FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

26AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

27LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 

28FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 

29IN THE SOFTWARE. 

30""" 

31 

32import functools 

33import itertools 

34 

35 

36def _nonblank(str): 

37 return str and not str.startswith("#") 

38 

39 

40@functools.singledispatch 

41def 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) 

60def _(text): 

61 return filter(_nonblank, map(str.strip, text.splitlines())) 

62 

63 

64def 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 

79def 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