Skip to content

Commit

Permalink
Better catch MTGJSON builds and alert maintainers of new build setups
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeldaZach committed Jun 26, 2020
1 parent a4ec3d8 commit 39d2c82
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ persistent=yes
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
disable=
broad-except,
C0326,
C0330,
duplicate-code,
Expand Down
4 changes: 4 additions & 0 deletions mtgjson.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ version=
date=
use_cache=

[Pushover]
app_token=
user_tokens=

[Scryfall]
name=
client_id=
Expand Down
32 changes: 25 additions & 7 deletions mtgjson5/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

gevent.monkey.patch_all() # isort:skip


import argparse
import logging
from typing import List, Set, Union

from mtgjson5.arg_parser import get_sets_to_build, parse_args
from mtgjson5.compress_generator import compress_mtgjson_contents
from mtgjson5.consts import OUTPUT_PATH
from mtgjson5.consts import MTGJSON_VERSION, OUTPUT_PATH
from mtgjson5.output_generator import (
generate_compiled_output_files,
generate_compiled_prices_output,
Expand All @@ -21,7 +23,7 @@
from mtgjson5.providers import GitHubMTGSqliteProvider, WhatsInStandardProvider
from mtgjson5.referral_builder import build_and_write_referral_map
from mtgjson5.set_builder import build_mtgjson_set
from mtgjson5.utils import init_logger
from mtgjson5.utils import init_logger, send_push_notification


def build_mtgjson_sets(
Expand Down Expand Up @@ -54,13 +56,10 @@ def build_mtgjson_sets(
write_set_file(compiled_set, output_pretty)


def main() -> None:
def dispatcher(args: argparse.Namespace) -> None:
"""
MTGJSON Main Executor
MTGJSON Dispatcher
"""
LOGGER.info("MTGJSON Compiler Starting")
args = parse_args()

# If a price build, simply build prices and exit
if args.price_build:
generate_compiled_prices_output(build_prices(), args.pretty)
Expand All @@ -82,6 +81,25 @@ def main() -> None:
generate_output_file_hashes(OUTPUT_PATH)


def main() -> None:
"""
MTGJSON safe main call
"""
LOGGER.info(f"Starting MTGJSON {MTGJSON_VERSION}")
args = parse_args()

try:
if not args.no_alerts:
send_push_notification(f"Starting build\n{args}")
dispatcher(args)
if not args.no_alerts:
send_push_notification("Build finished")
except Exception as exception:
LOGGER.fatal(f"Exception caught: {exception}")
if not args.no_alerts:
send_push_notification(f"Build failed\n{exception}")


if __name__ == "__main__":
init_logger()
LOGGER = logging.getLogger(__name__)
Expand Down
6 changes: 6 additions & 0 deletions mtgjson5/arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def parse_args() -> argparse.Namespace:
action="store_true",
help="Create and maintain a referral map for referral linkages.",
)
mtgjson_arg_group.add_argument(
"-NA",
"--no-alerts",
action="store_true",
help="Prevent push notifications from sending when property keys are defined.",
)

# Show help menu if no arguments are passed
if len(sys.argv) == 1:
Expand Down
39 changes: 39 additions & 0 deletions mtgjson5/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,42 @@ def get_str_or_none(value: Any) -> Optional[str]:
return None

return str(value)


def send_push_notification(message: str) -> bool:
"""
Send a push notification to project maintainers.
These alerts can be disabled by removing the Pushover
category from the properties file.
:param message: Message to send
:return If the message send successfully to everyone
"""
pushover_app_token = consts.CONFIG.get("Pushover", "app_token")
pushover_app_users = list(
filter(None, consts.CONFIG.get("Pushover", "user_tokens").split(","))
)

if not pushover_app_token:
LOGGER.warning("Unable to send Pushover notification. App token not set.")
return False

if not pushover_app_users:
LOGGER.warning("Unable to send Pushover notification. No user keys set.")
return False

all_succeeded = True
for user in pushover_app_users:
response = requests.post(
"https://api.pushover.net/1/messages.json",
data={
"token": pushover_app_token,
"user": user,
"title": f"MTGJSON {consts.MTGJSON_VERSION}",
"message": message,
},
)
if not response.ok:
LOGGER.warning(f"Error sending Pushover notification: {response.text}")
all_succeeded = False

return all_succeeded

0 comments on commit 39d2c82

Please sign in to comment.