Skip to content

Commit

Permalink
fix some regex
Browse files Browse the repository at this point in the history
  • Loading branch information
carderne committed Mar 20, 2024
1 parent 459bfe7 commit 241eabb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
23 changes: 20 additions & 3 deletions sigexport/merge.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import re
import shutil
from pathlib import Path

from sigexport import files, models, utils
from sigexport.logging import log


def lines_to_msgs(lines: list[str]) -> list[models.MergeMessage]:
"""Extract messages from lines of Markdown."""
p = re.compile(r"^\[(\d{4}-\d{2}-\d{2},{0,1} \d{2}:\d{2})\] (.*?): (.*\n)")
msgs: list[models.MergeMessage] = []
for li in lines:
m = p.match(li)
if m:
date_str, sender, body = m.groups()
date = utils.parse_datetime(date_str)
msg = models.MergeMessage(date=date, sender=sender, body=body)
msgs.append(msg)
else:
msgs[-1].body += li
return msgs


def merge_chat(new: list[models.Message], path_old: Path) -> list[models.Message]:
"""Merge new and old chat markdowns."""
with path_old.open(encoding="utf-8") as f:
old_raw = f.readlines()

old = utils.lines_to_msgs(old_raw)
old = lines_to_msgs(old_raw)
old_msgs = [o.to_message() for o in old]

try:
Expand Down Expand Up @@ -48,12 +65,12 @@ def merge_with_old(
try:
path_old = dir_old / "chat.md"
msgs_new = merge_chat(msgs, path_old)
new_chat_dict[name] = msgs_new
new_chat_dict[key] = msgs_new
except FileNotFoundError:
try:
path_old = dir_old / "index.md" # old name
msgs_new = merge_chat(msgs, path_old)
new_chat_dict[name] = msgs_new
new_chat_dict[key] = msgs_new
except FileNotFoundError:
log(f"\tNo old for {name}")
else:
Expand Down
2 changes: 1 addition & 1 deletion sigexport/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def to_message(self: "MergeMessage") -> Message:
reactions = [Reaction(*r.split(":")) for r in m_reactions[0].split(", ")]
body = re.sub(p_reactions, "", body)

p_stickers = r"\n(\(\( (.*) ))"
p_stickers = r"\n\(\( (.*) \)\)"
stickers = re.findall(p_stickers, self.body)
sticker = stickers[0] if stickers else ""
body = re.sub(p_stickers, "", body)
Expand Down
17 changes: 0 additions & 17 deletions sigexport/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
import sys
from datetime import datetime
from pathlib import Path
Expand All @@ -20,22 +19,6 @@ def parse_datetime(input_str: str) -> datetime:
return datetime.strptime(input_str, "%Y-%m-%d, %H:%M")


def lines_to_msgs(lines: list[str]) -> list[models.MergeMessage]:
"""Extract messages from lines of Markdown."""
p = re.compile(r"^\[(\d{4}-\d{2}-\d{2},{0,1} \d{2}:\d{2})\](.*?:)(.*\n)")
msgs: list[models.MergeMessage] = []
for li in lines:
m = p.match(li)
if m:
date_str, sender, body = m.groups()
date = parse_datetime(date_str)
msg = models.MergeMessage(date=date, sender=sender, body=body)
msgs.append(msg)
else:
msgs[-1].body += li
return msgs


def version_callback(value: bool) -> None:
"""Get sigexport version."""
if value:
Expand Down

0 comments on commit 241eabb

Please sign in to comment.