Skip to content

Commit

Permalink
Use an interface so we don't have to replace the milc.cli object
Browse files Browse the repository at this point in the history
  • Loading branch information
skullydazed committed Feb 4, 2024
1 parent 07586cb commit 51ffe3f
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 42 deletions.
6 changes: 2 additions & 4 deletions ci_tests
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ from pathlib import Path
from shutil import rmtree
from subprocess import CalledProcessError, DEVNULL, run

from milc import set_metadata

set_metadata(name='ci_tests', author='MILC', version='1.3.0')

from milc import cli

cli.milc_options(name='ci_tests', author='MILC', version='1.8.0')


@cli.entrypoint('Run CI Tests...')
def main(cli):
Expand Down
4 changes: 2 additions & 2 deletions custom_logger
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ PYTHON_ARGCOMPLETE_OK
"""
import logging

from milc import set_metadata
from milc import cli

# Setup external logger
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('custom_logger')

set_metadata(logger=logging.getLogger('custom_logger'))
cli.milc_options(logger=logging.getLogger('custom_logger'))

# Import milc
from milc import cli
Expand Down
28 changes: 28 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,31 @@ Users have several CLI arguments they can pass to control the output of logs. Th
* Enable or disable ANSI color
* `--unicode` and `--no-unicode`
* Enable or disable unicode icons

# Custom Loggers

You may want to bypass MILC's logging and use your own logger instead. To do this use `cli.milc_options(logger=<MyLogger>)`. This should be done before you call `cli()` or do anything else.

Example:

```python
import logging

from milc import cli


@cli.entrypoint('Hello, World!')
def hello(cli):
cli.log.info('Hello, World!')

if __name__ == '__main__':
my_logger = logging.getLogger('my-program')
# Configure my_logger the way you want/need here

cli.milc_options(logger=my_logger)
cli()

```

!!! warning
You should only call `cli.milc_options()` one time during your program's execution.
14 changes: 9 additions & 5 deletions docs/metadata.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# MILC Metadata

In order to initialize some things, such as the configuration file location and the version number reported by `--version`, MILC needs to know some basic information before you import `cli`. If you need to set the program's name, author name, and/or version number do it like this:
In order to initialize some things, such as the configuration file location and the version number reported by `--version`, MILC needs to know some basic information before the entrypoint is called. You can use `cli.milc_options()` to set this information.

```python
from milc import set_metadata

set_metadata(name='Florzelbop', version='1.0.0', author='Jane Doe')
Example:

```python
from milc import cli

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.
Expand All @@ -28,3 +28,7 @@ set_metadata(logger=custom_logger)

from milc import cli
```

## Deprecated: set_metadata()

Earlier versions of MILC used `milc.set_metadata` instead. This is still supported but will throw a Deprecation warning.
3 changes: 3 additions & 0 deletions docs/subcommand_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Configuration for MILC applications is a key/value system. Each key consists of

`import milc.subcommand.config`

!!! warn
This must be imported after `cli.milc_options()` is used.

Read on to see how users can utilize this subcommand.

## Simple Example
Expand Down
6 changes: 3 additions & 3 deletions example
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ PYTHON_ARGCOMPLETE_OK
"""
import os

from milc import set_metadata
from milc import cli

set_metadata(name='example', author='Milc Milcenson', version='1.8.0')
cli.milc_options(name='example', author='Milc Milcenson', version='1.8.0')

from milc import cli
# This needs to be imported after we use cli.milc_options()
import milc.subcommand.config # noqa


Expand Down
7 changes: 2 additions & 5 deletions generate_docs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import os
from pathlib import Path
from subprocess import CalledProcessError

summary_demarc = '<!-- DO NOT ADD OR CHANGE ANYTHING BELOW THIS LINE -->'

from milc import set_metadata

set_metadata(name='generate_docs', author='MILC', version='1.3.0')
from milc import cli

cli.milc_options(name='generate_docs', author='MILC', version='1.8.0')


@cli.argument('--commit', arg_only=True, action='store_true', help='Commit changes to git.')
@cli.entrypoint('Generate documentation.')
Expand Down
20 changes: 9 additions & 11 deletions milc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
import os
import sys
import warnings
from typing import Optional
from typing import Any, Optional

from .emoji import EMOJI_LOGLEVELS
from .milc import MILC
from .milc_interface import MILCInterface

if 'MILC_IGNORE_DEPRECATED' not in os.environ:
for name in ('MILC_APP_NAME', 'MILC_APP_VERSION', 'MILC_APP_AUTHOR'):
if name in os.environ:
warnings.warn(f'Using environment variable {name} is deprecated and will not be supported in the future, please use set_metadata() instead.', stacklevel=2)
warnings.warn(f'Using environment variable {name} is deprecated and will not be supported in the future, please use cli.milc_options() instead.', stacklevel=2)

cli = MILC()
cli = MILCInterface()


def set_metadata(
Expand All @@ -37,21 +37,19 @@ def set_metadata(
author: Optional[str] = None,
version: Optional[str] = None,
logger: Optional[logging.Logger] = None,
) -> MILC:
) -> Any:
"""Set metadata about your program.
Deprecated: Use `cli.milc_options()` instead.
This allows you to set the application's name, version, and/or author
before executing your entrypoint. You can also pass your own logger here
if you like.
It's best to run this only once, and it must be run before you call `cli()`.
"""
global cli

if cli._inside_context_manager:
raise RuntimeError('You must run set_metadata() before cli()!')

cli = MILC(name, version, author, logger)
warnings.warn("milc.set_metadata has been deprecated, please use cli.milc_options() instead.", stacklevel=2)
cli.milc_options(name=name, author=author, version=version, logger=logger)

return cli

Expand Down
8 changes: 1 addition & 7 deletions milc/milc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@
class MILC(object):
"""MILC - An Opinionated Batteries Included Framework
"""
def __init__(
self,
name: Optional[str] = None,
version: Optional[str] = None,
author: Optional[str] = None,
logger: Optional[logging.Logger] = None,
) -> None:
def __init__(self, name: Optional[str] = None, author: Optional[str] = None, version: Optional[str] = None, logger: Optional[logging.Logger] = None) -> None:
"""Initialize the MILC object.
"""
# Set some defaults
Expand Down
Loading

0 comments on commit 51ffe3f

Please sign in to comment.