Skip to content

Commit c08b591

Browse files
committed
DRY inbox doc loading
1 parent 1aed56a commit c08b591

File tree

1 file changed

+31
-49
lines changed

1 file changed

+31
-49
lines changed

beanhub_cli/inbox/main.py

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,34 @@ def compute_missing_emails(
143143
raise ValueError(f"Unexpected action type {type(action)}")
144144

145145

146+
def load_inbox_doc(config: str) -> InboxDoc:
147+
config_path = pathlib.Path(config)
148+
if config_path.exists():
149+
doc_payload = yaml.safe_load(config_path.read_bytes())
150+
inbox_doc = InboxDoc.model_validate(doc_payload)
151+
if inbox_doc.inbox is None:
152+
inbox_doc.inbox = DEFAULT_INBOX_RULES
153+
logger.info("Use default inbox rules")
154+
logger.info(
155+
"Loaded inbox doc from [green]%s[/]",
156+
config,
157+
extra={"markup": True, "highlighter": None},
158+
)
159+
else:
160+
inbox_doc = DEFAULT_INBOX_DOC
161+
logger.info(
162+
"The inbox doc at [green]%s[/] does not exist, use default config",
163+
config,
164+
extra={"markup": True, "highlighter": None},
165+
)
166+
logger.debug(
167+
f"Inbox doc: %s",
168+
inbox_doc.model_dump_json(indent=2),
169+
extra={"markup": True, "highlighter": JSONHighlighter()},
170+
)
171+
return inbox_doc
172+
173+
146174
@cli.command(
147175
help="Extract data from archived email files based on rules defined inbox config file"
148176
)
@@ -188,31 +216,8 @@ def extract(
188216
keep_thinking_log: bool,
189217
debug_output_folder: str,
190218
):
191-
config_path = pathlib.Path(config)
192219
workdir_path = pathlib.Path(workdir)
193-
if config_path.exists():
194-
doc_payload = yaml.safe_load(config_path.read_bytes())
195-
inbox_doc = InboxDoc.model_validate(doc_payload)
196-
if inbox_doc.inbox is None:
197-
inbox_doc.inbox = DEFAULT_INBOX_RULES
198-
logger.info("Use default inbox rules")
199-
logger.info(
200-
"Loaded inbox doc from [green]%s[/]",
201-
config,
202-
extra={"markup": True, "highlighter": None},
203-
)
204-
else:
205-
inbox_doc = DEFAULT_INBOX_DOC
206-
logger.info(
207-
"The inbox doc at [green]%s[/] does not exist, use default config",
208-
config,
209-
extra={"markup": True, "highlighter": None},
210-
)
211-
logger.debug(
212-
f"Inbox doc: %s",
213-
inbox_doc.model_dump_json(indent=2),
214-
extra={"markup": True, "highlighter": JSONHighlighter()},
215-
)
220+
inbox_doc = load_inbox_doc(config)
216221

217222
logger.info(
218223
"Extracting data with Ollama model [green]%s[/]",
@@ -354,6 +359,7 @@ def extract(
354359
"-c",
355360
"--config",
356361
type=click.Path(dir_okay=False),
362+
default=".beanhub/inbox.yaml",
357363
help="The path to inbox config file",
358364
)
359365
@click.option(
@@ -378,32 +384,8 @@ def dump(
378384
workdir: str,
379385
unsafe_tar_extract: bool,
380386
):
381-
default_config_path = pathlib.Path(DEFAULT_INBOX_CONFIG_FILE)
382-
if config is not None or default_config_path.exists():
383-
if config is not None:
384-
config_path = pathlib.Path(config)
385-
if not config_path.exists():
386-
logger.error("Inbox config file does not exist at %s", config_path)
387-
sys.exit(-1)
388-
else:
389-
config_path = default_config_path
390-
with config_path.open("rt") as fo:
391-
doc_payload = yaml.safe_load(fo)
392-
inbox_doc = InboxDoc.model_validate(doc_payload)
393-
env.logger.info(
394-
"Loaded inbox doc from [green]%s[/]",
395-
config,
396-
extra={"markup": True, "highlighter": None},
397-
)
398-
else:
399-
env.logger.info(
400-
"Loaded default inbox doc as config file is not provided by argument and does exists at the default location %s",
401-
default_config_path,
402-
)
403-
# TODO: load default
404-
inbox_doc = None
405-
406387
workdir_path = pathlib.Path(workdir)
388+
inbox_doc = load_inbox_doc(config)
407389

408390
if not hasattr(tarfile, "data_filter") and not unsafe_tar_extract:
409391
logger.error(

0 commit comments

Comments
 (0)