File size: 20,947 Bytes
b5e8702 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 | r"""
Backrefs for the 'regex' module.
Add the ability to use the following backrefs with re:
- `\Q` and `\Q...\E` - Escape/quote chars (search)
- `\c` and `\C...\E` - Uppercase char or chars (replace)
- `\l` and `\L...\E` - Lowercase char or chars (replace)
- `\N{Black Club Suit}` - Unicode character by name (replace)
- `\u0000` and `\U00000000` - Unicode characters (replace)
- `\R` - Generic line breaks (search)
Licensed under MIT
Copyright (c) 2015 - 2020 Isaac Muse <isaacmuse@gmail.com>
"""
from __future__ import annotations
import regex as _regex # type: ignore[import]
import copyreg as _copyreg
from functools import lru_cache as _lru_cache
from . import util as _util
from . import _bregex_parse
from ._bregex_parse import ReplaceTemplate
from typing import AnyStr, Callable, Any, Generic, Mapping, Iterator, cast
from ._bregex_typing import Pattern, Match
__all__ = (
"expand", "expandf", "match", "fullmatch", "search", "sub", "subf", "subn", "subfn", "split", "splititer",
"findall", "finditer", "purge", "escape", "D", "DEBUG", "A", "ASCII", "B", "BESTMATCH",
"E", "ENHANCEMATCH", "F", "FULLCASE", "I", "IGNORECASE", "L", "LOCALE", "M", "MULTILINE", "R", "REVERSE",
"S", "DOTALL", "U", "UNICODE", "X", "VERBOSE", "V0", "VERSION0", "V1", "VERSION1", "W", "WORD",
"P", "POSIX", "DEFAULT_VERSION", "FORMAT", "compile", "compile_search", "compile_replace", "Bregex",
"ReplaceTemplate"
)
# Expose some common re flags and methods to
# save having to import re and backrefs libraries
D = _regex.D
DEBUG = _regex.DEBUG
A = _regex.A
ASCII = _regex.ASCII
B = _regex.B
BESTMATCH = _regex.BESTMATCH
E = _regex.E
ENHANCEMATCH = _regex.ENHANCEMATCH
F = _regex.F
FULLCASE = _regex.FULLCASE
I = _regex.I
IGNORECASE = _regex.IGNORECASE
L = _regex.L
LOCALE = _regex.LOCALE
M = _regex.M
MULTILINE = _regex.MULTILINE
R = _regex.R
REVERSE = _regex.REVERSE
S = _regex.S
DOTALL = _regex.DOTALL
U = _regex.U
UNICODE = _regex.UNICODE
X = _regex.X
VERBOSE = _regex.VERBOSE
V0 = _regex.V0
VERSION0 = _regex.VERSION0
V1 = _regex.V1
VERSION1 = _regex.VERSION1
W = _regex.W
WORD = _regex.WORD
P = _regex.P
POSIX = _regex.POSIX
DEFAULT_VERSION = _regex.DEFAULT_VERSION
escape = _regex.escape
# Replace flags
FORMAT = 1
# Case upper or lower
_UPPER = 1
_LOWER = 2
# Maximum size of the cache.
_MAXCACHE = 500
_REGEX_TYPE = type(_regex.compile('', 0))
@_lru_cache(maxsize=_MAXCACHE)
def _cached_search_compile(
pattern: AnyStr,
re_verbose: bool,
re_version: bool,
pattern_type: type[AnyStr]
) -> AnyStr:
"""Cached search compile."""
return _bregex_parse._SearchParser(pattern, re_verbose, re_version).parse()
@_lru_cache(maxsize=_MAXCACHE)
def _cached_replace_compile(
pattern: Pattern[AnyStr],
repl: AnyStr,
flags: int,
pattern_type: type[AnyStr]
) -> ReplaceTemplate[AnyStr]:
"""Cached replace compile."""
return _bregex_parse._ReplaceParser(pattern, repl, bool(flags & FORMAT)).parse()
def _get_cache_size(replace: bool = False) -> int:
"""Get size of cache."""
if not replace:
size = _cached_search_compile.cache_info().currsize
else:
size = _cached_replace_compile.cache_info().currsize
return size
def _purge_cache() -> None:
"""Purge the cache."""
_cached_replace_compile.cache_clear()
_cached_search_compile.cache_clear()
def _is_replace(obj: Any) -> bool:
"""Check if object is a replace object."""
return isinstance(obj, ReplaceTemplate)
def _apply_replace_backrefs(
m: Match[AnyStr] | None,
repl: ReplaceTemplate[AnyStr] | AnyStr,
flags: int = 0
) -> AnyStr:
"""Expand with either the `ReplaceTemplate` or compile on the fly, or return None."""
if m is None:
raise ValueError("Match is None!")
if isinstance(repl, ReplaceTemplate):
return repl.expand(m)
return _bregex_parse._ReplaceParser(m.re, repl, bool(flags & FORMAT)).parse().expand(m)
def _apply_search_backrefs(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
flags: int = 0
) -> AnyStr | Pattern[AnyStr]:
"""Apply the search backrefs to the search pattern."""
if isinstance(pattern, (str, bytes)):
re_verbose = VERBOSE & flags
if flags & V0:
re_version = V0
elif flags & V1:
re_version = V1
else:
re_version = 0
if not (flags & DEBUG):
p = _cached_search_compile(
pattern, re_verbose, re_version, type(pattern)
) # type: AnyStr | Pattern[AnyStr]
else: # pragma: no cover
p = _bregex_parse._SearchParser(cast(AnyStr, pattern), re_verbose, re_version).parse()
elif isinstance(pattern, Bregex):
if flags:
raise ValueError("Cannot process flags argument with a compiled pattern")
p = pattern._pattern
elif isinstance(pattern, _REGEX_TYPE):
if flags:
raise ValueError("Cannot process flags argument with a compiled pattern!")
p = pattern
else:
raise TypeError("Not a string or compiled pattern!")
return p
def _assert_expandable(repl: Any, use_format: bool = False) -> None:
"""Check if replace template is expandable."""
if isinstance(repl, ReplaceTemplate):
if repl.use_format != use_format:
if use_format:
raise ValueError("Replace not compiled as a format replace")
else:
raise ValueError("Replace should not be compiled as a format replace!")
elif not isinstance(repl, (str, bytes)):
raise TypeError("Expected string, buffer, or compiled replace!")
###########################
# API
##########################
class Bregex(_util.Immutable, Generic[AnyStr]):
"""Bregex object."""
_pattern: Pattern[AnyStr]
auto_compile: bool
_hash: int
__slots__ = ("_pattern", "auto_compile", "_hash")
def __init__(self, pattern: Pattern[AnyStr], auto_compile: bool = True) -> None:
"""Initialization."""
super().__init__(
_pattern=pattern,
auto_compile=auto_compile,
_hash=hash((type(self), type(pattern), pattern, auto_compile))
)
@property
def pattern(self) -> AnyStr:
"""Return pattern."""
return cast(AnyStr, self._pattern.pattern)
@property
def flags(self) -> int:
"""Return flags."""
return cast(int, self._pattern.flags)
@property
def groupindex(self) -> Mapping[str, int]:
"""Return group index."""
return cast(Mapping[str, int], self._pattern.groupindex)
@property
def groups(self) -> tuple[AnyStr | None, ...]:
"""Return groups."""
return cast('tuple[AnyStr | None, ...]', self._pattern.groups)
@property
def scanner(self) -> Any:
"""Return scanner."""
return self._pattern.scanner
def __hash__(self) -> int:
"""Hash."""
return self._hash
def __eq__(self, other: Any) -> bool:
"""Equal."""
return (
isinstance(other, Bregex) and
self._pattern == other._pattern and
self.auto_compile == other.auto_compile
)
def __ne__(self, other: Any) -> bool:
"""Equal."""
return (
not isinstance(other, Bregex) or
self._pattern != other._pattern or
self.auto_compile != other.auto_compile
)
def __repr__(self) -> str: # pragma: no cover
"""Representation."""
return '{}.{}({!r}, auto_compile={!r})'.format(
self.__module__, self.__class__.__name__, self._pattern, self.auto_compile
)
def _auto_compile(
self,
template: AnyStr | Callable[..., AnyStr],
use_format: bool = False
) -> AnyStr | Callable[..., AnyStr]:
"""Compile replacements."""
if isinstance(template, ReplaceTemplate):
if use_format != template.use_format:
raise ValueError("Compiled replace cannot be a format object!")
elif isinstance(template, ReplaceTemplate) or (isinstance(template, (str, bytes)) and self.auto_compile):
return self.compile(template, (FORMAT if use_format and not isinstance(template, ReplaceTemplate) else 0))
elif isinstance(template, (str, bytes)) and use_format:
# Reject an attempt to run format replace when auto-compiling
# of template strings has been disabled and we are using a
# template string.
raise AttributeError('Format replaces cannot be called without compiling replace template!')
return template
def compile( # noqa A001
self,
repl: AnyStr | Callable[..., AnyStr],
flags: int = 0
) -> Callable[..., AnyStr]:
"""Compile replace."""
return compile_replace(self._pattern, repl, flags)
@property
def named_lists(self) -> Mapping[str, set[str | bytes]]:
"""Returned named lists."""
return cast('Mapping[str, set[str | bytes]]', self._pattern.named_lists)
def search(
self,
string: AnyStr,
*args: Any,
**kwargs: Any
) -> Match[AnyStr] | None:
"""Apply `search`."""
return self._pattern.search(string, *args, **kwargs)
def match(
self,
string: AnyStr,
*args: Any,
**kwargs: Any
) -> Match[AnyStr] | None:
"""Apply `match`."""
return cast('Match[AnyStr] | None', self._pattern.match(string, *args, **kwargs))
def fullmatch(
self,
string: AnyStr,
*args: Any,
**kwargs: Any
) -> Match[AnyStr] | None:
"""Apply `fullmatch`."""
return cast('Match[AnyStr] | None', self._pattern.fullmatch(string, *args, **kwargs))
def split(
self,
string: AnyStr,
*args: Any,
**kwargs: Any
) -> list[AnyStr]:
"""Apply `split`."""
return cast('list[AnyStr]', self._pattern.split(string, *args, **kwargs))
def splititer(
self,
string: AnyStr,
*args: Any,
**kwargs: Any
) -> Iterator[AnyStr]:
"""Apply `splititer`."""
return cast(Iterator[AnyStr], self._pattern.splititer(string, *args, **kwargs))
def findall(
self,
string: AnyStr,
*args: Any,
**kwargs: Any
) -> list[AnyStr] | list[tuple[AnyStr, ...]]:
"""Apply `findall`."""
return cast('list[AnyStr] | list[tuple[AnyStr, ...]]', self._pattern.findall(string, *args, **kwargs))
def finditer(
self,
string: AnyStr,
*args: Any,
**kwargs: Any
) -> Iterator[Match[AnyStr]]:
"""Apply `finditer`."""
return cast(Iterator[Match[AnyStr]], self._pattern.finditer(string, *args, **kwargs))
def sub(
self,
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
*args: Any,
**kwargs: Any
) -> AnyStr:
"""Apply `sub`."""
return cast(AnyStr, self._pattern.sub(self._auto_compile(repl), string, *args, **kwargs))
def subf(
self,
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
*args: Any,
**kwargs: Any
) -> AnyStr: # noqa A002
"""Apply `sub` with format style replace."""
return cast(AnyStr, self._pattern.subf(self._auto_compile(repl, True), string, *args, **kwargs))
def subn(
self,
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
*args: Any,
**kwargs: Any
) -> tuple[AnyStr, int]:
"""Apply `subn` with format style replace."""
return cast('tuple[AnyStr, int]', self._pattern.subn(self._auto_compile(repl), string, *args, **kwargs))
def subfn(
self,
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
*args: Any,
**kwargs: Any
) -> tuple[AnyStr, int]: # noqa A002
"""Apply `subn` after applying backrefs."""
return cast('tuple[AnyStr, int]', self._pattern.subfn(self._auto_compile(repl, True), string, *args, **kwargs))
def compile( # noqa A001
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
flags: int = 0,
auto_compile: bool | None = None,
**kwargs: Any
) -> Bregex[AnyStr]:
"""Compile both the search or search and replace into one object."""
if isinstance(pattern, Bregex):
if auto_compile is not None:
raise ValueError("Cannot compile Bregex with a different auto_compile!")
elif flags != 0:
raise ValueError("Cannot process flags argument with a compiled pattern")
return pattern
else:
if auto_compile is None:
auto_compile = True
return Bregex(compile_search(pattern, flags, **kwargs), auto_compile)
def compile_search(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
flags: int = 0,
**kwargs: Any
) -> Pattern[AnyStr]:
"""Compile with extended search references."""
return cast(Pattern[AnyStr], _regex.compile(_apply_search_backrefs(pattern, flags), flags, **kwargs))
def compile_replace(
pattern: Pattern[AnyStr],
repl: AnyStr | Callable[..., AnyStr],
flags: int = 0
) -> Callable[..., AnyStr]:
"""Construct a method that can be used as a replace method for `sub`, `subn`, etc."""
if pattern is not None and isinstance(pattern, _REGEX_TYPE):
if isinstance(repl, (str, bytes)):
if not (pattern.flags & DEBUG):
call = _cached_replace_compile(pattern, repl, flags, type(repl))
else: # pragma: no cover
call = _bregex_parse._ReplaceParser(pattern, repl, bool(flags & FORMAT)).parse()
elif isinstance(repl, ReplaceTemplate):
if flags:
raise ValueError("Cannot process flags argument with a ReplaceTemplate!")
if repl.pattern_hash != hash(pattern):
raise ValueError("Pattern hash doesn't match hash in compiled replace!")
call = repl
else:
raise TypeError("Not a valid type!")
else:
raise TypeError("Pattern must be a compiled regular expression!")
return call
def purge() -> None:
"""Purge caches."""
_purge_cache()
_regex.purge()
def expand(m: Match[AnyStr] | None, repl: ReplaceTemplate[AnyStr] | AnyStr) -> AnyStr:
"""Expand the string using the replace pattern or function."""
_assert_expandable(repl)
return _apply_replace_backrefs(m, repl)
def expandf(m: Match[AnyStr] | None, repl: ReplaceTemplate[AnyStr] | AnyStr) -> AnyStr:
"""Expand the string using the format replace pattern or function."""
_assert_expandable(repl, True)
return _apply_replace_backrefs(m, repl, flags=FORMAT)
def prefixmatch(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
string: AnyStr,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> Match[AnyStr] | None:
"""Wrapper for `match`."""
return cast(
'Match[AnyStr] | None',
_regex.match(_apply_search_backrefs(pattern, flags), string, flags, *args, **kwargs)
)
match = prefixmatch
def fullmatch(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
string: AnyStr,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> Match[AnyStr] | None:
"""Wrapper for `fullmatch`."""
return cast(
'Match[AnyStr] | None',
_regex.fullmatch(_apply_search_backrefs(pattern, flags), string, flags, *args, **kwargs)
)
def search(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
string: AnyStr,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> Match[AnyStr] | None:
"""Wrapper for `search`."""
return cast(
'Match[AnyStr] | None',
_regex.search(_apply_search_backrefs(pattern, flags), string, flags, *args, **kwargs)
)
def sub(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
count: int = 0,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> AnyStr:
"""Wrapper for `sub`."""
is_replace = _is_replace(repl)
is_string = isinstance(repl, (str, bytes))
if is_replace and cast(ReplaceTemplate[AnyStr], repl).use_format:
raise ValueError("Compiled replace cannot be a format object!")
pattern = compile_search(pattern, flags)
return cast(
AnyStr,
_regex.sub(
pattern, (compile_replace(pattern, repl) if is_replace or is_string else repl), string,
count,
0,
*args, **kwargs
)
)
def subf(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
count: int = 0,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> AnyStr:
"""Wrapper for `subf`."""
is_replace = _is_replace(repl)
is_string = isinstance(repl, (str, bytes))
if is_replace and not cast(ReplaceTemplate[AnyStr], repl).use_format:
raise ValueError("Compiled replace is not a format object!")
pattern = compile_search(pattern, flags)
rflags = FORMAT if is_string else 0
return cast(
AnyStr,
_regex.sub(
pattern, (compile_replace(pattern, repl, flags=rflags) if is_replace or is_string else repl), string,
count,
0,
*args, **kwargs
)
)
def subn(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
count: int = 0,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> tuple[AnyStr, int]:
"""Wrapper for `subn`."""
is_replace = _is_replace(repl)
is_string = isinstance(repl, (str, bytes))
if is_replace and cast(ReplaceTemplate[AnyStr], repl).use_format:
raise ValueError("Compiled replace cannot be a format object!")
pattern = compile_search(pattern, flags)
return cast(
'tuple[AnyStr, int]',
_regex.subn(
pattern, (compile_replace(pattern, repl) if is_replace or is_string else repl), string,
count,
0,
*args, **kwargs
)
)
def subfn(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
repl: AnyStr | Callable[..., AnyStr],
string: AnyStr,
count: int = 0,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> tuple[AnyStr, int]:
"""Wrapper for `subfn`."""
is_replace = _is_replace(repl)
is_string = isinstance(repl, (str, bytes))
if is_replace and not cast(ReplaceTemplate[AnyStr], repl).use_format:
raise ValueError("Compiled replace is not a format object!")
pattern = compile_search(pattern, flags)
rflags = FORMAT if is_string else 0
return cast(
'tuple[AnyStr, int]',
_regex.subn(
pattern, (compile_replace(pattern, repl, flags=rflags) if is_replace or is_string else repl), string,
count,
0,
*args, **kwargs
)
)
def split(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
string: AnyStr,
maxsplit: int = 0,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> list[AnyStr]:
"""Wrapper for `split`."""
return cast(
'list[AnyStr]',
_regex.split(_apply_search_backrefs(pattern, flags), string, maxsplit, flags, *args, **kwargs)
)
def splititer(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
string: AnyStr,
maxsplit: int = 0,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> Iterator[AnyStr]:
"""Wrapper for `splititer`."""
return cast(
Iterator[AnyStr],
_regex.splititer(_apply_search_backrefs(pattern, flags), string, maxsplit, flags, *args, **kwargs)
)
def findall(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
string: AnyStr,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> list[AnyStr] | list[tuple[AnyStr, ...]]:
"""Wrapper for `findall`."""
return cast(
'list[AnyStr] | list[tuple[AnyStr, ...]]',
_regex.findall(_apply_search_backrefs(pattern, flags), string, flags, *args, **kwargs)
)
def finditer(
pattern: AnyStr | Pattern[AnyStr] | Bregex[AnyStr],
string: AnyStr,
flags: int = 0,
*args: Any,
**kwargs: Any
) -> Iterator[Match[AnyStr]]:
"""Wrapper for `finditer`."""
return cast(
Iterator[Match[AnyStr]],
_regex.finditer(_apply_search_backrefs(pattern, flags), string, *args, **kwargs)
)
def _pickle(p): # type: ignore[no-untyped-def]
return Bregex, (p._pattern, p.auto_compile)
_copyreg.pickle(Bregex, _pickle)
|