[docs]defconvert_api_errors(func:Callable[P,T])->Callable[P,T]:""" Decorator that catches a MaestralApiError and prints a formatted error message to stdout before exiting. Calls ``sys.exit(1)`` after printing the error to stdout. """from..exceptionsimportMaestralApiError@functools.wraps(func)defwrapper(*args:P.args,**kwargs:P.kwargs)->T:try:returnfunc(*args,**kwargs)exceptMaestralApiErrorasexc:warn(f"{exc.title}. {exc.message}")sys.exit(1)returnwrapper
[docs]defcheck_for_fatal_errors(m:MaestralProxy|Maestral)->bool:""" Checks the given Maestral instance for fatal errors such as revoked Dropbox access, deleted Dropbox folder etc. Prints a nice representation to the command line. :param m: Proxy to Maestral daemon or Maestral instance. :returns: True in case of fatal errors, False otherwise. """importtextwrapmaestral_err_list=m.fatal_errorsiflen(maestral_err_list)>0:size=get_term_size()err=maestral_err_list[0]wrapped_msg=textwrap.fill(err.message,width=size.columns)click.echo("")click.secho(err.title,fg="red")click.secho(wrapped_msg,fg="red")click.echo("")returnTrueelse:returnFalse
[docs]config_option=click.option("-c","--config-name",default=DEFAULT_CONFIG_NAME,type=ConfigName(existing=False),is_eager=True,expose_value=True,help="Run command with the given configuration.",)
[docs]existing_config_option=click.option("-c","--config-name",default=DEFAULT_CONFIG_NAME,type=ConfigName(),is_eager=True,expose_value=True,help="Run command with the given configuration.",)
[docs]definject_proxy(fallback:bool,existing_config:bool)->Callable[[Callable[P,T]],Callable[P,Any]]:defdecorator(f:Callable[P,T])->Callable[P,Any]:defwrapper(*args:P.args,**kwargs:P.kwargs)->Any:from..daemonimportCommunicationError,MaestralProxyctx=click.get_current_context()config_name=ctx.params.pop("config_name","maestral")kwargs.pop("config_name",None)try:proxy=ctx.with_resource(MaestralProxy(config_name,fallback=fallback))exceptCommunicationError:click.echo("Maestral daemon is not running.")ctx.exit(1)else:returnctx.invoke(f,proxy,*args,**kwargs)ifexisting_config:f=existing_config_option(f)else:f=config_option(f)returnfunctools.update_wrapper(wrapper,f)returndecorator