lint: tidy up code in cli module

This commit is contained in:
Iain Learmonth 2022-06-17 13:21:35 +01:00
parent 5f7733d064
commit 98895e47de
6 changed files with 61 additions and 58 deletions

View file

@ -1,11 +1,11 @@
import argparse
import datetime
import json
import logging
from traceback import TracebackException
from typing import Type, TYPE_CHECKING, Any
from typing import Type
from app import app
from app.cli import _SubparserType, BaseCliHandler
from app.extensions import db
from app.models.activity import Activity
from app.models.automation import Automation, AutomationState, AutomationLogs
@ -30,12 +30,6 @@ from app.terraform.list.s3 import ListS3Automation
from app.terraform.proxy.azure_cdn import ProxyAzureCdnAutomation
from app.terraform.proxy.cloudfront import ProxyCloudfrontAutomation
if TYPE_CHECKING:
_SubparserType = argparse._SubParsersAction[argparse.ArgumentParser]
else:
_SubparserType = Any
jobs = {
x.short_name: x
for x in [
@ -63,6 +57,13 @@ jobs = {
def run_all(**kwargs: bool) -> None:
"""
Run all automation tasks.
:param kwargs: this function takes the same arguments as :func:`run_job` and will pass the same arguments
to every task
:return: None
"""
for job in jobs.values():
run_job(job, **kwargs) # type: ignore
@ -100,10 +101,10 @@ def run_job(job_cls: Type[BaseAutomation], *,
# the error handling process isn't really handling the error, but rather causing it
# to be logged for investigation. Catching more specific exceptions would just mean that
# others go unrecorded and are difficult to debug.
except Exception as e: # pylint: disable=broad-except
tb = TracebackException.from_exception(e)
except Exception as exc: # pylint: disable=broad-except
trace = TracebackException.from_exception(exc)
success = False
logs = "\n".join(tb.format())
logs = "\n".join(trace.format())
if success:
automation.state = AutomationState.IDLE
automation.next_run = datetime.datetime.utcnow() + datetime.timedelta(
@ -128,7 +129,7 @@ def run_job(job_cls: Type[BaseAutomation], *,
db.session.commit()
class AutomateCliHandler:
class AutomateCliHandler(BaseCliHandler):
@classmethod
def add_subparser_to(cls, subparsers: _SubparserType) -> None:
parser = subparsers.add_parser("automate", help="automation operations")
@ -139,9 +140,6 @@ class AutomateCliHandler:
parser.add_argument("--ignore-schedule", help="run job even if it's not time yet", action="store_true")
parser.set_defaults(cls=cls)
def __init__(self, args: argparse.Namespace) -> None:
self.args = args
def run(self) -> None:
with app.app_context():
if self.args.job: