o
    +ke&                     @   s   d dl Z d dlZd dlZd dlZd dlmZ ddlmZ e Zddlm	Z	 dd Z
G d	d
 d
ZG dd deZG dd deZG dd dZdS )    N)get_terminal_size   )create_logger   )ellipsis_truncatec                 C   s$   t ddd }|dkr| |S | S )Nr   fallbackr   r   )r   ljust)messageterminal_space r   9usr/lib/python3.10/site-packages/borg/helpers/progress.pyjustify_to_terminal_size   s   
r   c                   @   sP   e Zd ZdZdZdZdZedd ZdddZ	d	d
 Z
ddddZdd ZdS )ProgressIndicatorBasezborg.output.progressNFr   c                 C   s   |  j d7  _ | j S )zYUnique number, can be used by receiving applications to distinguish different operations.r   )operation_id_counter)clsr   r   r   operation_id   s   z"ProgressIndicatorBase.operation_idc                 C   s   d | _ t| j| _|  | _|| _| jjsitj	t
jd| _ | j tj td}z|j}|jr3dnd}|j| _W n tyE   d}Y nw | j | || j _| j| j  | jjtjkre| jtj d| j_| j tjk| _d S )N)streamborg
F)handlerlogging	getLoggerLOGGERloggerr   idmsgidhandlersStreamHandlersysstderrsetLevelINFO	formatterjsonAttributeErrorsetFormatter
terminator
addHandlerlevelNOTSETWARN	propagategetEffectiveLevelemit)selfr   r   r&   r*   r   r   r   __init__"   s,   

	zProgressIndicatorBase.__init__c                 C   s*   | j d ur| j| j  | j   d S d S N)r   r   removeHandlercloser2   r   r   r   __del__I   s   
zProgressIndicatorBase.__del__finishedc             	   K   sP   | j sJ | js
d S |t| j| j| j|t d tt 	|t
jdd d S )N)Z	operationr   typer:   timeT)fileflush)r'   r1   updatedictr   r   	JSON_TYPEr<   printdumpsr"   r#   )r2   r:   kwargsr   r   r   output_jsonN   s   
z!ProgressIndicatorBase.output_jsonc                 C   s$   | j r| jdd d S | d d S )NTr9    )r'   rE   outputr7   r   r   r   finish[   s   zProgressIndicatorBase.finishr4   )__name__
__module____qualname__r   rA   r'   r   classmethodr   r3   r8   rE   rH   r   r   r   r   r      s    

'r   c                   @   s   e Zd ZdZdd ZdS )ProgressIndicatorMessageZprogress_messagec                 C   s*   | j r| j|d d S | jt| d S )N)r   )r'   rE   r   infor   )r2   msgr   r   r   rG   e   s   zProgressIndicatorMessage.outputN)rI   rJ   rK   rA   rG   r   r   r   r   rM   b   s    rM   c                       s@   e Zd ZdZd fdd	Zdd	d
ZdddZdddZ  ZS )ProgressIndicatorPercentZprogress_percentr      %3.0f%%Nc                    s0   d| _ || _|| _|| _|| _t j|d dS )a  
        Percentage-based progress indicator

        :param total: total amount of items
        :param step: step size in percent
        :param start: at which percent value to start
        :param msg: output message, must contain one %f placeholder for the percentage
        r   )r   N)countertotal
trigger_atsteprO   superr3   )r2   rT   rV   startrO   r   	__class__r   r   r3   o   s   	z!ProgressIndicatorPercent.__init__r   c                 C   sN   |d ur|| _ | j d | j }|  j |7  _ || jkr%|  j| j7  _|S d S )Nd   )rS   rT   rU   rV   )r2   currentincreasepctr   r   r   progress   s   
z!ProgressIndicatorPercent.progressc                 C   s   |  ||}|durX|durP| js@ddlm} tddd }|dkr@||| jt|g|dd  dg   }t|d ||d< | j| jt|g|  d	|d
S | | j| S dS )z
        Show and output the progress message

        :param current: set the current percentage [None]
        :param increase: increase the current percentage [None]
        :param info: array of strings to be formatted with msg [None]
        Nr   )swidthr   r	   r   r   rF   F)justifyrN   )	r_   r'   platformr`   r   rO   tupler   rG   )r2   r\   r]   rN   r^   r`   r   spacer   r   r   show   s   * zProgressIndicatorPercent.showTc                 C   s<   | j r| j|| j| j|d d S |rt|}| j| d S )N)r   r\   rT   rN   )r'   rE   rS   rT   r   r   rN   )r2   r   ra   rN   r   r   r   rG      s
   zProgressIndicatorPercent.output)r   rQ   r   rR   N)Nr   )Nr   N)TN)	rI   rJ   rK   rA   r3   r_   re   rG   __classcell__r   r   rY   r   rP   l   s    

	rP   c                   @   s6   e Zd ZdddZdd Zdd Zd	d
 Zdd ZdS )ProgressIndicatorEndless
   Nc                 C   s*   d| _ d| _|| _|du rtj}|| _dS )z
        Progress indicator (long row of dots)

        :param step: every Nth call, call the func
        :param file: output file, default: sys.stderr
        r   N)rS   	triggeredrV   r"   r#   r=   )r2   rV   r=   r   r   r   r3      s   
z!ProgressIndicatorEndless.__init__c                 C   s4   |  j d7  _ | j | j dk}|r|  jd7  _|S )Nr   r   )rS   rV   ri   r2   Ztriggerr   r   r   r_      s
   z!ProgressIndicatorEndless.progressc                 C   s   |   }|r| | jS d S r4   )r_   rG   ri   rj   r   r   r   re      s   zProgressIndicatorEndless.showc                 C   s   t dd| jdd d S )N.rF   T)endr=   r>   rB   r=   )r2   ri   r   r   r   rG      s   zProgressIndicatorEndless.outputc                 C   s   t | jd d S )N)r=   rm   r7   r   r   r   rH      s   zProgressIndicatorEndless.finish)rh   N)rI   rJ   rK   r3   r_   re   rG   rH   r   r   r   r   rg      s    
rg   )r   r'   r"   r<   shutilr   r   r   Zparseformatr   r   r   rM   rP   rg   r   r   r   r   <module>   s    M
>