-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Read Acoustic Newsletters mapping from database (ref #256) * Mention command in docs * Use official return codes * Use os.EX_DATAERR instead of os.EX_NOTFOUND (available in Unix only) * Address code review comments * Address changes on the acoustic_fields command too * Adjust usage info in commands
- Loading branch information
Showing
11 changed files
with
344 additions
and
106 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python3 | ||
"""Manage Acoustic newsletters mapping: add, list, and remove from the db.""" | ||
|
||
import argparse | ||
import os | ||
import sys | ||
|
||
from ctms import config | ||
from ctms.crud import ( | ||
create_acoustic_newsletters_mapping, | ||
delete_acoustic_newsletters_mapping, | ||
get_all_acoustic_newsletters_mapping, | ||
) | ||
from ctms.database import get_db_engine | ||
|
||
|
||
def main(dbsession, test_args=None) -> int: | ||
parser = argparse.ArgumentParser( | ||
description="Manage Acoustic Newsletter mapping", | ||
) | ||
subparsers = parser.add_subparsers(dest="action") | ||
parser_add = subparsers.add_parser( | ||
"add", | ||
usage="""python acoustic_newsletters_mapping.py add "test-pilot:sub_new_test_pilot" """, | ||
) | ||
parser_add.add_argument( | ||
"mapping", help="Add newsletter mapping specified as 'source:destination'" | ||
) | ||
|
||
parser_remove = subparsers.add_parser( | ||
"remove", | ||
usage="""python acoustic_newsletters_mapping.py remove "test-pilot" """, | ||
) | ||
parser_remove.add_argument( | ||
"source", help="Remove newsletter mapping with specified 'source'" | ||
) | ||
|
||
subparsers.add_parser("list") | ||
|
||
args = parser.parse_args(args=test_args) | ||
if args.action == "add": | ||
source, destination = args.mapping.split(":") | ||
# This will fail if mapping already exists. | ||
create_acoustic_newsletters_mapping(dbsession, source, destination) | ||
print(f"Added {source!r} → {destination!r}.") | ||
elif args.action == "remove": | ||
row = delete_acoustic_newsletters_mapping(dbsession, args.source) | ||
if not row: | ||
print(f"Unknown mapping '{args.source}'. Give up.") | ||
return os.EX_DATAERR | ||
print(f"Removed {row.source!r} → {row.destination!r}.") | ||
else: | ||
entries = get_all_acoustic_newsletters_mapping(dbsession) | ||
print("\n".join(sorted(f"- {e.source!r} → {e.destination!r}" for e in entries))) | ||
|
||
return os.EX_OK | ||
|
||
|
||
if __name__ == "__main__": | ||
config_settings = config.Settings() | ||
engine, session_factory = get_db_engine(config_settings) | ||
session = session_factory() | ||
with engine.connect() as connection: | ||
return_code = main(session) # pylint:disable = invalid-name | ||
|
||
sys.exit(return_code) |
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
Oops, something went wrong.