Skip to content

Commit

Permalink
Improvement[Community] Improve methods in IMessageChatLoader (langc…
Browse files Browse the repository at this point in the history
…hain-ai#25746)

- Add @staticmethod to static methods in `IMessageChatLoader`.
- Format args name.
  • Loading branch information
ZhangShenao authored Aug 26, 2024
1 parent 815f59d commit 44e3e23
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions libs/community/langchain_community/chat_loaders/imessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def __init__(self, path: Optional[Union[str, Path]] = None):
"Please install it with `pip install pysqlite3`"
) from e

def _parse_attributedBody(self, attributedBody: bytes) -> str:
@staticmethod
def _parse_attributed_body(attributed_body: bytes) -> str:
"""
Parse the attributedBody field of the message table
for the text content of the message.
Expand All @@ -88,17 +89,18 @@ def _parse_attributedBody(self, attributedBody: bytes) -> str:
that byte.
Args:
attributedBody (bytes): attributedBody field of the message table.
attributed_body (bytes): attributedBody field of the message table.
Return:
str: Text content of the message.
"""
content = attributedBody.split(b"NSString")[1][5:]
content = attributed_body.split(b"NSString")[1][5:]
length, start = content[0], 1
if content[0] == 129:
length, start = int.from_bytes(content[1:3], "little"), 3
return content[start : start + length].decode("utf-8", errors="ignore")

def _get_session_query(self, use_chat_handle_table: bool) -> str:
@staticmethod
def _get_session_query(use_chat_handle_table: bool) -> str:
# Messages sent pre OSX 12 require a join through the chat_handle_join table
# However, the table doesn't exist if database created with OSX 12 or above.

Expand Down Expand Up @@ -151,7 +153,7 @@ def _load_single_chat_session(
if text:
content = text
elif attributedBody:
content = self._parse_attributedBody(attributedBody)
content = self._parse_attributed_body(attributedBody)
else: # Skip messages with no content
continue

Expand Down

0 comments on commit 44e3e23

Please sign in to comment.