From 75ef54697f119db4d2c42e1a8665dc65230610e3 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sun, 4 Feb 2024 14:40:05 -0800 Subject: [PATCH] preserve name, author, version, and logger in case cli.milc_options() is called multiple times --- docs/metadata.md | 10 ++++------ milc/milc_interface.py | 10 +++++++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/metadata.md b/docs/metadata.md index 09933f7..5ce0879 100644 --- a/docs/metadata.md +++ b/docs/metadata.md @@ -12,21 +12,19 @@ cli.milc_options(name='Florzelbop', version='1.0.0', author='Jane Doe') You should only do this once, and you should do it as early in your program's execution as possible. -!!! danger - Do not import `set_metadata` and `cli` at the same time! When you run `set_metadata` the `cli` object will be replaced, but your existing import will continue to reference the old `cli` object. +!!! warning + If you have spread your program among several files, or you are using `milc.subcommand.config`, you need to use `cli.milc_options()` before you import those modules. ## Custom Loggers You can also use this to pass in custom loggers. ```python -from milc import set_metadata +from milc import cli from my_program import custom_logger -set_metadata(logger=custom_logger) - -from milc import cli +cli.milc_options(logger=custom_logger) ``` ## Deprecated: set_metadata() diff --git a/milc/milc_interface.py b/milc/milc_interface.py index 8c98053..96f0131 100644 --- a/milc/milc_interface.py +++ b/milc/milc_interface.py @@ -18,12 +18,20 @@ class MILCInterface: def __init__(self) -> None: self._milc: Optional[MILC] = None + self._name = None + self._author = None + self._version = None + self._logger = None def milc_options(self, *, name: Optional[str] = None, author: Optional[str] = None, version: Optional[str] = None, logger: Optional[Logger] = None) -> None: if self._milc and self._milc._inside_context_manager: raise RuntimeError('You must run set_metadata() before cli()!') - self._milc = MILC(name, author, version, logger) + self._name = name or self._name + self._author = author or self._author + self._version = version or self._version + self._logger = logger or self._logger + self._milc = MILC(self._name, self._author, self._version, self._logger) @property def milc(self) -> MILC: