Skip to content

Commit

Permalink
slob.py: remove KeyTooLongException
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Mar 16, 2024
1 parent b674a0f commit 2f582c4
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions pyglossary/slob.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,12 +1011,6 @@ class WriterEvent(NamedTuple):
data: Any


class KeyTooLongException(Exception):
@property
def key(self) -> str:
return self.args[0]


class Writer:
def __init__( # noqa: PLR0913
self,
Expand Down Expand Up @@ -1117,6 +1111,10 @@ def tag(self, name: str, value: str = "") -> None:

self._tags[name] = value

@staticmethod
def key_is_too_long(actual_key, fragment) -> bool:
return len(actual_key) > MAX_TEXT_LEN or len(fragment) > MAX_TINY_TEXT_LEN

@staticmethod
def _split_key(
key: str | tuple[str, str],
Expand All @@ -1126,8 +1124,6 @@ def _split_key(
fragment = ""
else:
actual_key, fragment = key
if len(actual_key) > MAX_TEXT_LEN or len(fragment) > MAX_TINY_TEXT_LEN:
raise KeyTooLongException(key)
return actual_key, fragment

def add(
Expand All @@ -1147,10 +1143,9 @@ def add(
actual_keys = []

for key in keys:
try:
actual_key, fragment = self._split_key(key)
except KeyTooLongException as e:
self._fire_event("key_too_long", e.key)
actual_key, fragment = self._split_key(key)
if self.key_is_too_long(actual_key, fragment):
self._fire_event("key_too_long", key)
else:
actual_keys.append((actual_key, fragment))

Expand Down Expand Up @@ -1183,16 +1178,11 @@ def add(
def add_alias(self, key: str, target_key: str) -> None:
if not self.max_redirects:
raise NotImplementedError

try:
self._split_key(key)
except KeyTooLongException as e:
self._fire_event("alias_too_long", e.key)
if self.key_is_too_long(*self._split_key(key)):
self._fire_event("alias_too_long", key)
return
try:
self._split_key(target_key)
except KeyTooLongException as e:
self._fire_event("alias_target_too_long", e.key)
if self.key_is_too_long(*self._split_key(target_key)):
self._fire_event("alias_target_too_long", target_key)
return
self.f_aliases.add(pickle.dumps(target_key), key)

Expand Down

0 comments on commit 2f582c4

Please sign in to comment.