Skip to content

Commit

Permalink
preserve name, author, version, and logger in case cli.milc_options()…
Browse files Browse the repository at this point in the history
… is called multiple times
  • Loading branch information
skullydazed committed Feb 4, 2024
1 parent 51ffe3f commit 75ef546
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 4 additions & 6 deletions docs/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 9 additions & 1 deletion milc/milc_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 75ef546

Please sign in to comment.