generated from MITLibraries/python-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MITLibraries/in-714-app-structure
Set up initial CLI command and configuration
- Loading branch information
Showing
15 changed files
with
911 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.10.3 | ||
3.11.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,17 @@ | ||
# python-cli-template | ||
# alma-creditcardslips | ||
|
||
A template repository for creating Python CLI applications. | ||
|
||
## App setup (delete this section and above after initial application setup) | ||
|
||
1. Rename "my_app" to the desired app name across the repo. (May be helpful to do a project-wide find-and-replace). | ||
2. Update Python version if needed. | ||
3. Install all dependencies with `make install` to create initial Pipfile.lock with latest dependency versions. | ||
4. Add initial app description to README and update initial required ENV variable documentation as needed. | ||
5. Update license if needed (check app-specific dependencies for licensing terms). | ||
6. Check Github repository settings: | ||
- Confirm repo branch protection settings are correct (see [dev docs](https://mitlibraries.github.io/guides/basics/github.html) for details) | ||
- Confirm that all of the following are enabled in the repo's code security and analysis settings: | ||
- Dependabot alerts | ||
- Dependabot security updates | ||
- Secret scanning | ||
7. Create a Sentry project for the app if needed (we want this for most apps): | ||
- Send initial exceptions to Sentry project for dev, stage, and prod environments to create them. | ||
- Create an alert for the prod environment only, with notifications sent to the appropriate team(s). | ||
- If *not* using Sentry, delete Sentry configuration from config.py and test_config.py, and remove sentry_sdk from project dependencies. | ||
|
||
# my_app | ||
|
||
Description of the app | ||
A CLI application to generate and email credit card slips for Alma invoices via the Alma API. | ||
|
||
## Development | ||
|
||
- To install with dev dependencies: `make install` | ||
- To update dependencies: `make update` | ||
- To run unit tests: `make test` | ||
- To lint the repo: `make lint` | ||
- To run the app: `pipenv run my_app --help` | ||
- To run the app: `pipenv run ccslips --help` | ||
|
||
## Required ENV | ||
## ENV Variables | ||
|
||
- `LOG_LEVEL` = Optional, set to a valid Python logging level (e.g. `DEBUG`, case-insensitive) if desired. Can also be passed as an option directly to the ccslips command. Defaults to `INFO` if not set or passed to the command. | ||
- `SENTRY_DSN` = If set to a valid Sentry DSN, enables Sentry exception monitoring. This is not needed for local development. | ||
- `WORKSPACE` = Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""ccslips package.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import logging | ||
from datetime import datetime, timedelta | ||
from time import perf_counter | ||
from typing import Optional | ||
|
||
import click | ||
|
||
from ccslips.config import configure_logger, configure_sentry | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"-s", | ||
"--source-email", | ||
required=True, | ||
help="The email address sending the credit card slips.", | ||
) | ||
@click.option( | ||
"-r", | ||
"--recipient-email", | ||
required=True, | ||
multiple=True, | ||
help="The email address receiving the credit card slips. Repeatable", | ||
) | ||
@click.option( | ||
"-d", | ||
"--date", | ||
help=( | ||
"Optional date of exports to process, in 'YYYY-MM-DD' format. Defaults to " | ||
"yesterday's date if not provided." | ||
), | ||
) | ||
@click.option( | ||
"-l", | ||
"--log-level", | ||
envvar="LOG_LEVEL", | ||
help="Case-insensitive Python log level to use, e.g. debug or warning. Defaults to " | ||
"INFO if not provided or found in ENV.", | ||
) | ||
@click.pass_context | ||
def main( | ||
ctx: click.Context, | ||
source_email: str, | ||
recipient_email: list[str], | ||
date: Optional[str], | ||
log_level: Optional[str], | ||
) -> None: | ||
start_time = perf_counter() | ||
log_level = log_level or "INFO" | ||
root_logger = logging.getLogger() | ||
logger.info(configure_logger(root_logger, log_level)) | ||
logger.info(configure_sentry()) | ||
logger.debug("Command called with options: %s", ctx.params) | ||
|
||
logger.info("Starting credit card slips process") | ||
|
||
# Do things here! | ||
date = date or (datetime.today() - timedelta(days=1)).strftime("%Y-%m-%d") | ||
click.echo( | ||
f"\nFunctionality to be added here will process the credit card invoices from " | ||
f"date {date} and send the resulting email from {source_email} to " | ||
f"{recipient_email}\n" | ||
) | ||
|
||
elapsed_time = perf_counter() - start_time | ||
logger.info( | ||
"Finished! Total time to complete process: %s", | ||
str(timedelta(seconds=elapsed_time)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,40 @@ | ||
from my_app.cli import main | ||
from freezegun import freeze_time | ||
|
||
from ccslips.cli import main | ||
|
||
def test_cli_no_options(caplog, runner): | ||
result = runner.invoke(main) | ||
|
||
@freeze_time("2023-01-02") | ||
def test_cli_only_required_options(caplog, runner): | ||
result = runner.invoke( | ||
main, ["-s", "source@example.com", "-r", "recipient@example.com"] | ||
) | ||
assert result.exit_code == 0 | ||
assert "Logger 'root' configured with level=INFO" in caplog.text | ||
assert "Running process" in caplog.text | ||
assert "Starting credit card slips process" in caplog.text | ||
assert "Total time to complete process" in caplog.text | ||
|
||
|
||
def test_cli_all_options(caplog, runner): | ||
result = runner.invoke(main, ["--verbose"]) | ||
result = runner.invoke( | ||
main, | ||
[ | ||
"--source-email", | ||
"source@example.com", | ||
"--recipient-email", | ||
"recipient1@example.com", | ||
"--recipient-email", | ||
"recipient2@example.com", | ||
"--date", | ||
"2023-01-02", | ||
"--log-level", | ||
"debug", | ||
], | ||
) | ||
assert result.exit_code == 0 | ||
assert "Logger 'root' configured with level=DEBUG" in caplog.text | ||
assert "Running process" in caplog.text | ||
assert ( | ||
"Command called with options: {'source_email': 'source@example.com', " | ||
"'recipient_email': ('recipient1@example.com', 'recipient2@example.com'), " | ||
"'date': '2023-01-02', 'log_level': 'debug'}" in caplog.text | ||
) | ||
assert "Total time to complete process" in caplog.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters