]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pkg_resources/_vendor/jaraco/text/__init__.py
7 from importlib
.resources
import files
# type: ignore
8 except ImportError: # pragma: nocover
9 from pkg_resources
.extern
.importlib_resources
import files
# type: ignore
11 from pkg_resources
.extern
.jaraco
.functools
import compose
, method_cache
12 from pkg_resources
.extern
.jaraco
.context
import ExceptionTrap
15 def substitution(old
, new
):
17 Return a function that will perform a substitution on a string
19 return lambda s
: s
.replace(old
, new
)
22 def multi_substitution(*substitutions
):
24 Take a sequence of pairs specifying substitutions, and create
25 a function that performs those substitutions.
27 >>> multi_substitution(('foo', 'bar'), ('bar', 'baz'))('foo')
30 substitutions
= itertools
.starmap(substitution
, substitutions
)
31 # compose function applies last function first, so reverse the
32 # substitutions to get the expected order.
33 substitutions
= reversed(tuple(substitutions
))
34 return compose(*substitutions
)
37 class FoldedCase(str):
39 A case insensitive string class; behaves just like str
40 except compares equal when the only variation is case.
42 >>> s = FoldedCase('hello world')
44 >>> s == 'Hello World'
47 >>> 'Hello World' == s
50 >>> s != 'Hello World'
59 >>> sorted(map(FoldedCase, ['GAMMA', 'alpha', 'Beta']))
60 ['alpha', 'Beta', 'GAMMA']
62 Sequence membership is straightforward.
64 >>> "Hello World" in [s]
66 >>> s in ["Hello World"]
69 You may test for set inclusion, but candidate and elements
72 >>> FoldedCase("Hello World") in {s}
74 >>> s in {FoldedCase("Hello World")}
77 String inclusion works as long as the FoldedCase object
80 >>> "hello" in FoldedCase("Hello World")
83 But not if the FoldedCase object is on the left:
85 >>> FoldedCase('hello') in 'Hello World'
88 In that case, use ``in_``:
90 >>> FoldedCase('hello').in_('Hello World')
93 >>> FoldedCase('hello') > FoldedCase('Hello')
97 def __lt__(self
, other
):
98 return self
.lower() < other
.lower()
100 def __gt__(self
, other
):
101 return self
.lower() > other
.lower()
103 def __eq__(self
, other
):
104 return self
.lower() == other
.lower()
106 def __ne__(self
, other
):
107 return self
.lower() != other
.lower()
110 return hash(self
.lower())
112 def __contains__(self
, other
):
113 return super().lower().__contains
__(other
.lower())
115 def in_(self
, other
):
116 "Does self appear in other?"
117 return self
in FoldedCase(other
)
119 # cache lower since it's likely to be called frequently.
122 return super().lower()
124 def index(self
, sub
):
125 return self
.lower().index(sub
.lower())
127 def split(self
, splitter
=' ', maxsplit
=0):
128 pattern
= re
.compile(re
.escape(splitter
), re
.I
)
129 return pattern
.split(self
, maxsplit
)
132 # Python 3.8 compatibility
133 _unicode_trap
= ExceptionTrap(UnicodeDecodeError)
136 @_unicode_trap.passes
137 def is_decodable(value
):
139 Return True if the supplied value is decodable (using the default
142 >>> is_decodable(b'\xff')
144 >>> is_decodable(b'\x32')
150 def is_binary(value
):
152 Return True if the value appears to be binary (that is, it's a byte
153 string and isn't decodable).
155 >>> is_binary(b'\xff')
157 >>> is_binary('\xff')
160 return isinstance(value
, bytes) and not is_decodable(value
)
165 Trim something like a docstring to remove the whitespace that
166 is common due to indentation and formatting.
168 >>> trim("\n\tfoo = bar\n\t\tbar = baz\n")
169 'foo = bar\n\tbar = baz'
171 return textwrap
.dedent(s
).strip()
176 Wrap lines of text, retaining existing newlines as
179 >>> print(wrap(lorem_ipsum))
180 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
181 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
182 minim veniam, quis nostrud exercitation ullamco laboris nisi ut
183 aliquip ex ea commodo consequat. Duis aute irure dolor in
184 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
185 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
186 culpa qui officia deserunt mollit anim id est laborum.
188 Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam
189 varius, turpis et commodo pharetra, est eros bibendum elit, nec luctus
190 magna felis sollicitudin mauris. Integer in mauris eu nibh euismod
191 gravida. Duis ac tellus et risus vulputate vehicula. Donec lobortis
192 risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue,
193 eros est euismod turpis, id tincidunt sapien risus a quam. Maecenas
194 fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla
195 a mi. Duis sapien sem, aliquet nec, commodo eget, consequat quis,
196 neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl adipiscing
197 sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque
198 nunc. Nullam arcu. Aliquam consequat. Curabitur augue lorem, dapibus
199 quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis,
200 molestie eu, feugiat in, orci. In hac habitasse platea dictumst.
202 paragraphs
= s
.splitlines()
203 wrapped
= ('\n'.join(textwrap
.wrap(para
)) for para
in paragraphs
)
204 return '\n\n'.join(wrapped
)
209 Given a multi-line string, return an unwrapped version.
211 >>> wrapped = wrap(lorem_ipsum)
212 >>> wrapped.count('\n')
214 >>> unwrapped = unwrap(wrapped)
215 >>> unwrapped.count('\n')
218 Lorem ipsum dolor sit amet, consectetur adipiscing ...
219 Curabitur pretium tincidunt lacus. Nulla gravida orci ...
222 paragraphs
= re
.split(r
'\n\n+', s
)
223 cleaned
= (para
.replace('\n', ' ') for para
in paragraphs
)
224 return '\n'.join(cleaned
)
229 class Splitter(object):
230 """object that will split a string with the given arguments for each call
232 >>> s = Splitter(',')
233 >>> s('hello, world, this is your, master calling')
234 ['hello', ' world', ' this is your', ' master calling']
237 def __init__(self
, *args
):
240 def __call__(self
, s
):
241 return s
.split(*self
.args
)
244 def indent(string
, prefix
=' ' * 4):
249 return prefix
+ string
252 class WordSet(tuple):
254 Given an identifier, return the words that identifier represents,
255 whether in camel case, underscore-separated, etc.
257 >>> WordSet.parse("camelCase")
260 >>> WordSet.parse("under_sep")
263 Acronyms should be retained
265 >>> WordSet.parse("firstSNL")
268 >>> WordSet.parse("you_and_I")
271 >>> WordSet.parse("A simple test")
272 ('A', 'simple', 'test')
274 Multiple caps should not interfere with the first cap of another word.
276 >>> WordSet.parse("myABCClass")
277 ('my', 'ABC', 'Class')
279 The result is a WordSet, so you can get the form you need.
281 >>> WordSet.parse("myABCClass").underscore_separated()
284 >>> WordSet.parse('a-command').camel_case()
287 >>> WordSet.parse('someIdentifier').lowered().space_separated()
290 Slices of the result should return another WordSet.
292 >>> WordSet.parse('taken-out-of-context')[1:].underscore_separated()
295 >>> WordSet.from_class_name(WordSet()).lowered().space_separated()
298 >>> example = WordSet.parse('figured it out')
299 >>> example.headless_camel_case()
301 >>> example.dash_separated()
306 _pattern
= re
.compile('([A-Z]?[a-z]+)|([A-Z]+(?![a-z]))')
308 def capitalized(self
):
309 return WordSet(word
.capitalize() for word
in self
)
312 return WordSet(word
.lower() for word
in self
)
314 def camel_case(self
):
315 return ''.join(self
.capitalized())
317 def headless_camel_case(self
):
319 first
= next(words
).lower()
320 new_words
= itertools
.chain((first
,), WordSet(words
).camel_case())
321 return ''.join(new_words
)
323 def underscore_separated(self
):
324 return '_'.join(self
)
326 def dash_separated(self
):
327 return '-'.join(self
)
329 def space_separated(self
):
330 return ' '.join(self
)
332 def trim_right(self
, item
):
334 Remove the item from the end of the set.
336 >>> WordSet.parse('foo bar').trim_right('foo')
338 >>> WordSet.parse('foo bar').trim_right('bar')
340 >>> WordSet.parse('').trim_right('bar')
343 return self
[:-1] if self
and self
[-1] == item
else self
345 def trim_left(self
, item
):
347 Remove the item from the beginning of the set.
349 >>> WordSet.parse('foo bar').trim_left('foo')
351 >>> WordSet.parse('foo bar').trim_left('bar')
353 >>> WordSet.parse('').trim_left('bar')
356 return self
[1:] if self
and self
[0] == item
else self
358 def trim(self
, item
):
360 >>> WordSet.parse('foo bar').trim('foo')
363 return self
.trim_left(item
).trim_right(item
)
365 def __getitem__(self
, item
):
366 result
= super(WordSet
, self
).__getitem
__(item
)
367 if isinstance(item
, slice):
368 result
= WordSet(result
)
372 def parse(cls
, identifier
):
373 matches
= cls
._pattern
.finditer(identifier
)
374 return WordSet(match
.group(0) for match
in matches
)
377 def from_class_name(cls
, subject
):
378 return cls
.parse(subject
.__class
__.__name
__)
381 # for backward compatibility
382 words
= WordSet
.parse
385 def simple_html_strip(s
):
387 Remove HTML from the string `s`.
389 >>> str(simple_html_strip(''))
392 >>> print(simple_html_strip('A <bold>stormy</bold> day in paradise'))
393 A stormy day in paradise
395 >>> print(simple_html_strip('Somebody <!-- do not --> tell the truth.'))
396 Somebody tell the truth.
398 >>> print(simple_html_strip('What about<br/>\nmultiple lines?'))
402 html_stripper
= re
.compile('(<!--.*?-->)|(<[^>]*>)|([^<]+)', re
.DOTALL
)
403 texts
= (match
.group(3) or '' for match
in html_stripper
.finditer(s
))
404 return ''.join(texts
)
407 class SeparatedValues(str):
409 A string separated by a separator. Overrides __iter__ for getting
412 >>> list(SeparatedValues('a,b,c'))
415 Whitespace is stripped and empty values are discarded.
417 >>> list(SeparatedValues(' a, b , c, '))
424 parts
= self
.split(self
.separator
)
425 return filter(None, (part
.strip() for part
in parts
))
430 Given a series of lines, find the common prefix and strip it from them.
437 >>> res = Stripper.strip_prefix(lines)
441 ['defg\n', '\n', 'de\n']
443 If no prefix is common, nothing should be stripped.
449 >>> res = Stripper.strip_prefix(lines)
455 def __init__(self
, prefix
, lines
):
457 self
.lines
= map(self
, lines
)
460 def strip_prefix(cls
, lines
):
461 prefix_lines
, lines
= itertools
.tee(lines
)
462 prefix
= functools
.reduce(cls
.common_prefix
, prefix_lines
)
463 return cls(prefix
, lines
)
465 def __call__(self
, line
):
468 null
, prefix
, rest
= line
.partition(self
.prefix
)
472 def common_prefix(s1
, s2
):
474 Return the common prefix of two lines.
476 index
= min(len(s1
), len(s2
))
477 while s1
[:index
] != s2
[:index
]:
482 def remove_prefix(text
, prefix
):
484 Remove the prefix from the text if it exists.
486 >>> remove_prefix('underwhelming performance', 'underwhelming ')
489 >>> remove_prefix('something special', 'sample')
492 null
, prefix
, rest
= text
.rpartition(prefix
)
496 def remove_suffix(text
, suffix
):
498 Remove the suffix from the text if it exists.
500 >>> remove_suffix('name.git', '.git')
503 >>> remove_suffix('something special', 'sample')
506 rest
, suffix
, null
= text
.partition(suffix
)
510 def normalize_newlines(text
):
512 Replace alternate newlines with the canonical newline.
514 >>> normalize_newlines('Lorem Ipsum\u2029')
516 >>> normalize_newlines('Lorem Ipsum\r\n')
518 >>> normalize_newlines('Lorem Ipsum\x85')
521 newlines
= ['\r\n', '\r', '\n', '\u0085', '\u2028', '\u2029']
522 pattern
= '|'.join(newlines
)
523 return re
.sub(pattern
, '\n', text
)
527 return str and not str.startswith('#')
530 @functools.singledispatch
531 def yield_lines(iterable
):
533 Yield valid lines of a string or iterable.
535 >>> list(yield_lines(''))
537 >>> list(yield_lines(['foo', 'bar']))
539 >>> list(yield_lines('foo\nbar'))
541 >>> list(yield_lines('\nfoo\n#bar\nbaz #comment'))
542 ['foo', 'baz #comment']
543 >>> list(yield_lines(['foo\nbar', 'baz', 'bing\n\n\n']))
544 ['foo', 'bar', 'baz', 'bing']
546 return itertools
.chain
.from_iterable(map(yield_lines
, iterable
))
549 @yield_lines.register(str)
551 return filter(_nonblank
, map(str.strip
, text
.splitlines()))
554 def drop_comment(line
):
558 >>> drop_comment('foo # bar')
561 A hash without a space may be in a URL.
563 >>> drop_comment('http://example.com/foo#bar')
564 'http://example.com/foo#bar'
566 return line
.partition(' #')[0]
569 def join_continuation(lines
):
571 Join lines continued by a trailing backslash.
573 >>> list(join_continuation(['foo \\', 'bar', 'baz']))
575 >>> list(join_continuation(['foo \\', 'bar', 'baz']))
577 >>> list(join_continuation(['foo \\', 'bar \\', 'baz']))
581 The character preceeding the backslash is also elided.
583 >>> list(join_continuation(['goo\\', 'dly']))
586 A terrible idea, but...
587 If no line is available to continue, suppress the lines.
589 >>> list(join_continuation(['foo', 'bar\\', 'baz\\']))
594 while item
.endswith('\\'):
596 item
= item
[:-2].strip() + next(lines
)
597 except StopIteration: