"""Non-interactive interface that stores output... versionadded:: 6.4"""## (C) Pywikibot team, 2021-2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportloggingimportqueuefromtypingimportAny,Sequencefrompywikibotimportconfigfrompywikibot.loggingimportINFO,VERBOSEfrompywikibot.userinterfaces._interface_baseimportABUIC
[docs]classUI(ABUIC):"""Collects output into an unseen buffer. .. versionadded:: 6.4 """def__init__(self)->None:"""Initialize the UI."""super().__init__()self._buffer=queue.Queue()self.log_handler=logging.handlers.QueueHandler(self._buffer)self.log_handler.setLevel(VERBOSEifconfig.verbose_outputelseINFO)
[docs]definit_handlers(self,root_logger,*args,**kwargs)->None:"""Initialize the handlers for user output."""root_logger.addHandler(self.log_handler)
[docs]definput(self,question:str,password:bool=False,default:str='',force:bool=False)->str:"""Ask the user a question and return the answer."""returndefault
[docs]definput_choice(self,question:str,options,default:str|None=None,return_shortcut:bool=True,automatic_quit:bool=True,force:bool=False):"""Ask the user and returns a value from the options."""returndefault
[docs]definput_list_choice(self,question:str,answers:Sequence[Any],default:int|str|None=None,force:bool=False)->Any:"""Ask the user to select one entry from a list of entries."""returndefault
[docs]defoutput(self,text,*args,**kwargs)->None:"""Output text that would usually go to a stream."""self._buffer.put(text)
[docs]defpop_output(self):"""Provide and clear any buffered output."""output=[]whilenotself._buffer.empty():record=self._buffer.get_nowait()ifisinstance(record,str):output.append(record)elifisinstance(record,logging.LogRecord):output.append(record.getMessage())else:raiseValueError('Buffer can only contain logs and strings, 'f'had {type(record).__name__}')returnoutput
[docs]defclear(self)->None:"""Removes any buffered output."""self.pop_output()