A unicode subclass used to identify anonymously generated names.
| 5530 | |
| 5531 | |
| 5532 | class _anonymous_label(_truncated_label): |
| 5533 | """A unicode subclass used to identify anonymously |
| 5534 | generated names.""" |
| 5535 | |
| 5536 | __slots__ = () |
| 5537 | |
| 5538 | @classmethod |
| 5539 | def safe_construct( |
| 5540 | cls, |
| 5541 | seed: int, |
| 5542 | body: str, |
| 5543 | enclosing_label: Optional[str] = None, |
| 5544 | sanitize_key: bool = False, |
| 5545 | ) -> _anonymous_label: |
| 5546 | # need to escape chars that interfere with format |
| 5547 | # strings in any case, issue #8724 |
| 5548 | body = re.sub(r"[%\(\) \$]+", "_", body) |
| 5549 | |
| 5550 | if sanitize_key: |
| 5551 | # sanitize_key is then an extra step used by BindParameter |
| 5552 | body = body.strip("_") |
| 5553 | |
| 5554 | label = "%%(%d %s)s" % (seed, body.replace("%", "%%")) |
| 5555 | if enclosing_label: |
| 5556 | label = "%s%s" % (enclosing_label, label) |
| 5557 | |
| 5558 | return _anonymous_label(label) |
| 5559 | |
| 5560 | def __add__(self, other): |
| 5561 | if "%" in other and not isinstance(other, _anonymous_label): |
| 5562 | other = str(other).replace("%", "%%") |
| 5563 | else: |
| 5564 | other = str(other) |
| 5565 | |
| 5566 | return _anonymous_label( |
| 5567 | quoted_name( |
| 5568 | str.__add__(self, other), |
| 5569 | self.quote, |
| 5570 | ) |
| 5571 | ) |
| 5572 | |
| 5573 | def __radd__(self, other): |
| 5574 | if "%" in other and not isinstance(other, _anonymous_label): |
| 5575 | other = str(other).replace("%", "%%") |
| 5576 | else: |
| 5577 | other = str(other) |
| 5578 | |
| 5579 | return _anonymous_label( |
| 5580 | quoted_name( |
| 5581 | str.__add__(other, self), |
| 5582 | self.quote, |
| 5583 | ) |
| 5584 | ) |
| 5585 | |
| 5586 | def apply_map(self, map_): |
| 5587 | if self.quote is not None: |
| 5588 | # preserve quoting only if necessary |
| 5589 | return quoted_name(self % map_, self.quote) |
no outgoing calls