A unicode subclass used to identify anonymously generated names.
| 5966 | |
| 5967 | |
| 5968 | class _anonymous_label(_truncated_label): |
| 5969 | """A unicode subclass used to identify anonymously |
| 5970 | generated names.""" |
| 5971 | |
| 5972 | __slots__ = () |
| 5973 | |
| 5974 | @classmethod |
| 5975 | def safe_construct_with_key( |
| 5976 | cls, seed: int | str, body: str, sanitize_key: bool = False |
| 5977 | ) -> typing_Tuple[_anonymous_label, str]: |
| 5978 | # need to escape chars that interfere with format |
| 5979 | # strings in any case, issue #8724 |
| 5980 | body = _anonymous_label_escape.sub("_", body) |
| 5981 | |
| 5982 | if sanitize_key: |
| 5983 | # sanitize_key is then an extra step used by BindParameter |
| 5984 | body = body.strip("_") |
| 5985 | |
| 5986 | key = f"{seed} {body.replace('%', '%%')}" |
| 5987 | label = _anonymous_label(f"%({key})s") |
| 5988 | return label, key |
| 5989 | |
| 5990 | @classmethod |
| 5991 | def safe_construct( |
| 5992 | cls, seed: int | str, body: str, sanitize_key: bool = False |
| 5993 | ) -> _anonymous_label: |
| 5994 | # need to escape chars that interfere with format |
| 5995 | # strings in any case, issue #8724 |
| 5996 | body = _anonymous_label_escape.sub("_", body) |
| 5997 | |
| 5998 | if sanitize_key: |
| 5999 | # sanitize_key is then an extra step used by BindParameter |
| 6000 | body = body.strip("_") |
| 6001 | |
| 6002 | return _anonymous_label(f"%({seed} {body.replace('%', '%%')})s") |
| 6003 | |
| 6004 | def __add__(self, other: str) -> _anonymous_label: |
| 6005 | if "%" in other and not isinstance(other, _anonymous_label): |
| 6006 | other = str(other).replace("%", "%%") |
| 6007 | else: |
| 6008 | other = str(other) |
| 6009 | |
| 6010 | return _anonymous_label( |
| 6011 | quoted_name( |
| 6012 | str.__add__(self, other), |
| 6013 | self.quote, |
| 6014 | ) |
| 6015 | ) |
| 6016 | |
| 6017 | def __radd__(self, other: str) -> _anonymous_label: |
| 6018 | if "%" in other and not isinstance(other, _anonymous_label): |
| 6019 | other = str(other).replace("%", "%%") |
| 6020 | else: |
| 6021 | other = str(other) |
| 6022 | |
| 6023 | return _anonymous_label( |
| 6024 | quoted_name( |
| 6025 | str.__add__(other, self), |
no outgoing calls