"""Decorators for Page objects."""## (C) Pywikibot team, 2017-2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportpywikibotfrompywikibot.exceptionsimport(Error,OtherPageSaveError,PageSaveRelatedError,)frompywikibot.toolsimportSPHINX_RUNNING,add_full_name,manage_wrapping
[docs]defallow_asynchronous(func):"""Decorator to make it possible to run a BasePage method asynchronously. This is done when the method is called with kwarg :code:`asynchronous=True`. Optionally, you can also provide kwarg callback, which, if provided, is a callable that gets the page as the first and a possible exception that occurred during saving in the second thread or None as the second argument. """defhandle(func,self,*args,**kwargs):do_async=kwargs.pop('asynchronous',False)callback=kwargs.pop('callback',None)err=Nonetry:func(self,*args,**kwargs)# TODO: other "expected" error types to catch?exceptErrorasedit_err:err=edit_err# edit_err will be deleted in the end of the scopelink=self.title(as_link=True)ifdo_async:pywikibot.error(f'page {link} not saved due to {err}\n')pywikibot.log(f'Error saving page {link} ({err})\n',exc_info=True)ifnotcallbackandnotdo_async:ifisinstance(err,PageSaveRelatedError):raiseerrraiseOtherPageSaveError(self,err)ifcallback:callback(self,err)defwrapper(self,*args,**kwargs)->None:ifkwargs.get('asynchronous'):pywikibot.async_request(handle,func,self,*args,**kwargs)else:handle(func,self,*args,**kwargs)manage_wrapping(wrapper,func)returnwrapper
ifnotSPHINX_RUNNING:# T365286: decorate allow_asynchronous function with add_full_nameallow_asynchronous=add_full_name(allow_asynchronous)