Skip to content

Commit

Permalink
Introduce register_teardown_handler + bundle miscellaneous click opti…
Browse files Browse the repository at this point in the history
…ons under @opts_miscellaneous
  • Loading branch information
bergercookie committed Jan 20, 2024
1 parent e30b2d9 commit ecf2ca2
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 140 deletions.
35 changes: 35 additions & 0 deletions syncall/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
`sys.exit()` to avoid dumping stack traces to the user.
"""

import atexit
import inspect
import logging
import os
Expand All @@ -16,6 +17,7 @@
from urllib.parse import quote

from bubop import (
ExitHooks,
PrefsManager,
format_list,
log_to_syslog,
Expand Down Expand Up @@ -347,3 +349,36 @@ def app_log_to_syslog():
calling_file = Path(caller_frame[1])
fname = calling_file.stem
log_to_syslog(name=fname)


def register_teardown_handler(
pdb_on_error: bool, inform_about_config: bool, combination_name: str, verbose: int
) -> ExitHooks:
"""Shortcut for registering the teardown logic in a top-level sync application.
We're explicilty not catching/reporting exceptions if the pdb_on_error argument is set
since the developer is meaning to get a prompt and debug any exception that arises.
"""
hooks: ExitHooks = ExitHooks()

def teardown():
if hooks.exception is not None:
if hooks.exception.__class__ is KeyboardInterrupt:
logger.error("C-c pressed, exiting...")
else:
report_toplevel_exception(is_verbose=verbose >= 1)
return 1

if inform_about_config:
inform_about_combination_name_usage(combination_name)

if pdb_on_error:
logger.warning(
"pdb_on_error is enabled. Disabling exit hooks / not taking actions at the end "
"of the run."
)
else:
hooks.register()
atexit.register(teardown)

return hooks
29 changes: 28 additions & 1 deletion syncall/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import click

from syncall import __version__
from syncall.app_utils import name_to_resolution_strategy_type
from syncall.constants import COMBINATION_FLAGS
from syncall.pdb_cli_utils import run_pdb_on_error as _run_pdb_on_error
Expand Down Expand Up @@ -121,7 +122,7 @@ def opt_tw_all_tasks():
"--taskwarrior-all-tasks",
"tw_sync_all_tasks",
is_flag=True,
help="Sync all taskwarrior tasks [potentially very slow]",
help="Sync all taskwarrior tasks (potentially very slow)",
)


Expand Down Expand Up @@ -370,6 +371,32 @@ def opt_filename_extension():


# general options -----------------------------------------------------------------------------
def opts_miscellaneous(side_A_name: str, side_B_name: str):
def decorator(f):
for d in reversed(
[
(opt_list_resolution_strategies,),
(opt_resolution_strategy,),
(
click.version_option,
__version__,
),
(opt_pdb_on_error,),
(opt_list_combinations, side_A_name, side_B_name),
(opt_combination, side_A_name, side_B_name),
(opt_custom_combination_savename, side_A_name, side_B_name),
]
):
fn = d[0]
fn_args = d[1:]
f = fn(*fn_args)(f) # type: ignore

f = click.option("-v", "--verbose", count=True)(f)
return f

return decorator


def opt_default_duration_event_mins():
return click.option(
"--default-event-duration-mins",
Expand Down
33 changes: 7 additions & 26 deletions syncall/scripts/tw_caldav_sync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import atexit
import datetime
import os
import subprocess
Expand All @@ -7,7 +6,6 @@
import caldav
import click
from bubop import (
ExitHooks,
check_optional_mutually_exclusive,
check_required_mutually_exclusive,
format_dict,
Expand All @@ -16,7 +14,7 @@
)

from syncall import inform_about_app_extras
from syncall.app_utils import app_log_to_syslog, error_and_exit
from syncall.app_utils import app_log_to_syslog, error_and_exit, register_teardown_handler
from syncall.cli import (
opt_caldav_calendar,
opt_caldav_passwd_cmd,
Expand All @@ -42,15 +40,13 @@
fetch_app_configuration,
fetch_from_pass_manager,
get_resolution_strategy,
inform_about_combination_name_usage,
list_named_combinations,
opt_combination,
opt_custom_combination_savename,
opt_list_combinations,
opt_resolution_strategy,
opt_tw_project,
opt_tw_tags,
report_toplevel_exception,
)


Expand Down Expand Up @@ -213,27 +209,12 @@ def main(
caldav_side = CaldavSide(client=client, calendar_name=caldav_calendar)

# teardown function and exception handling ------------------------------------------------
hooks: ExitHooks = ExitHooks()

def teardown():
if hooks.exception is not None:
if hooks.exception.__class__ is KeyboardInterrupt:
logger.error("C-c pressed, exiting...")
else:
report_toplevel_exception(is_verbose=verbose >= 1)
return 1

if inform_about_config:
inform_about_combination_name_usage(combination_name)

if pdb_on_error:
logger.warning(
"pdb_on_error is enabled. Disabling exit hooks / not taking actions at the end "
"of the run."
)
else:
hooks.register()
atexit.register(teardown)
register_teardown_handler(
pdb_on_error=pdb_on_error,
inform_about_config=inform_about_config,
combination_name=combination_name,
verbose=verbose,
)

# sync ------------------------------------------------------------------------------------
with Aggregator(
Expand Down
Loading

0 comments on commit ecf2ca2

Please sign in to comment.