From a4647d345db8a3dd04c63547ad30b5763b52fabe Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Sun, 27 Oct 2024 14:19:41 +0100 Subject: [PATCH] feat: improve logging (#746) --- memium/__main__.py | 6 +++++- memium/source/document_source.py | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/memium/__main__.py b/memium/__main__.py index de47776..c694280 100644 --- a/memium/__main__.py +++ b/memium/__main__.py @@ -1,6 +1,7 @@ import logging import sys import time +from datetime import datetime from functools import partial from pathlib import Path from typing import Annotated, Optional @@ -53,6 +54,7 @@ def cli( bool, typer.Option(help="Skip all syncing, useful for smoketesting of the interface") ] = False, ): + start_time = datetime.now() config_dir = input_dir / ".memium" config_dir.mkdir(exist_ok=True) @@ -81,7 +83,9 @@ def cli( main_fn() if watch_seconds: - log.info(f"Sync complete, sleeping for {watch_seconds} seconds") + log.info( + f"Sync complete in {(datetime.now() - start_time).total_seconds()} seconds, sleeping for {watch_seconds} seconds" + ) time.sleep(watch_seconds) main_fn() diff --git a/memium/source/document_source.py b/memium/source/document_source.py index 3e1eecf..c1c90b2 100644 --- a/memium/source/document_source.py +++ b/memium/source/document_source.py @@ -17,6 +17,9 @@ class FileNotRetrievedError(Exception): path: Path error: Exception + def __repr__(self) -> str: + return f"{self.path}: Could not retrieve due to '{self.error}'" + class BaseDocumentSource(Protocol): def get_documents(self) -> Sequence[Document]: ... @@ -59,10 +62,17 @@ def _sanitize_to_valid_markdown(self, input_str: str) -> str: def _get_document_from_file(self, file_path: Path) -> Document | FileNotRetrievedError: try: - with file_path.open("r", encoding="utf8") as f: - return Document( - content=self._sanitize_to_valid_markdown(f.read()), source_path=file_path - ) + try: + contents = file_path.read_text(encoding="utf8") + except Exception as e: + raise Exception(f"Could not read file {file_path}") from e + + try: + sanitized = self._sanitize_to_valid_markdown(contents) + except Exception as e: + raise Exception(f"Could not sanitize file {file_path}") from e + + return Document(content=sanitized, source_path=file_path) except Exception as e: log.warning(f"Could not retrieve {file_path}: {e}") return FileNotRetrievedError(file_path, e)