diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2022-01-23 21:47:11 -0500 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2022-01-23 21:47:11 -0500 |
| commit | 2de9ab95cea180f1e24ff98988fc6abbb02b0b8a (patch) | |
| tree | eef4b1698625d9018af73fe25a027d94e7666c99 | |
| parent | 42ce1398e1576537966a9dbfbf63e5757dbb95e8 (diff) | |
| download | python-setuptools-git-2de9ab95cea180f1e24ff98988fc6abbb02b0b8a.tar.gz | |
Extract function for joining continuations in lines.
| -rw-r--r-- | pkg_resources/__init__.py | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index c8dad54b..7f8cc93d 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -3107,23 +3107,46 @@ def drop_comment(line): return line.partition(' #')[0] -def parse_requirements(strs): - """Yield ``Requirement`` objects for each specification in `strs` +def join_continuation(lines): + r""" + Join lines continued by a trailing backslash. - `strs` must be a string, or a (possibly-nested) iterable thereof. - """ - # create a steppable iterator, so we can handle \-continuations - lines = iter(map(drop_comment, yield_lines(strs))) + >>> list(join_continuation(['foo \\', 'bar', 'baz'])) + ['foobar', 'baz'] + >>> list(join_continuation(['foo \\', 'bar', 'baz'])) + ['foobar', 'baz'] + >>> list(join_continuation(['foo \\', 'bar \\', 'baz'])) + ['foobarbaz'] + + Not sure why, but... + The character preceeding the backslash is also elided. + + >>> list(join_continuation(['goo\\', 'dly'])) + ['godly'] + + A terrible idea, but... + If no line is available to continue, suppress the lines. - for line in lines: - # If there is a line continuation, drop it, and append the next line. - if line.endswith('\\'): - line = line[:-2].strip() + >>> list(join_continuation(['foo', 'bar\\', 'baz\\'])) + ['foo'] + """ + lines = iter(lines) + for item in lines: + while item.endswith('\\'): try: - line += next(lines) + item = item[:-2].strip() + next(lines) except StopIteration: return - yield Requirement(line) + yield item + + +def parse_requirements(strs): + """Yield ``Requirement`` objects for each specification in `strs` + + `strs` must be a string, or a (possibly-nested) iterable thereof. + """ + lines = map(drop_comment, yield_lines(strs)) + return map(Requirement, join_continuation(lines)) class RequirementParseError(packaging.requirements.InvalidRequirement): |
