File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/filebuffer.cpython-310.pyc
o
�N�g� � @ sB d Z ddlZddlZddlmZ G dd� de�ZG dd� d�ZdS )z�
:codeauthor: Pedro Algarvio (pedro@algarvio.me)
salt.utils.filebuffer
~~~~~~~~~~~~~~~~~~~~~
This utility allows parsing a file in chunks.
� N)�
SaltExceptionc @ s e Zd ZdZdS )�InvalidFileModezM
An invalid file mode was used to open the file passed to the buffer
N)�__name__�
__module__�__qualname__�__doc__� r r �I/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/filebuffer.pyr s r c @ sJ e Zd ZdZddd�Zedd� �Zd d
� Zdd� ZeZ d
d� Z
dd� ZdS )�BufferedReadera�
This object allows iterating through the contents of a file keeping
X configurable bytes in memory which can be used to, for example,
do regex search/matching on more than a single line.
So, **an imaginary, non accurate**, example could be:
1 - Initiate the BufferedReader filling it to max_in_men:
br = [1, 2, 3]
2 - next chunk(pop chunk_size from the left, append chunk_size to the
right):
br = [2, 3, 4]
:type path: str
:param path: The file path to be read
:type max_in_mem: int
:param max_in_mem: The maximum bytes kept in memory while iterating through
the file. Default 256KB.
:type chunk_size: int
:param chunk_size: The size of each consequent read chunk. Default 32KB.
:type mode: str
:param mode: The mode the file should be opened. **Only read modes**.
� � � �rc C sH d|v sd|v rt d��|| _tjj�| j|�| _|| _|| _d | _ d S )N�a�wz(Cannot open file in write or append mode)
r Z_BufferedReader__path�salt�utils�filesZfopen�_BufferedReader__file�_BufferedReader__max_in_mem�_BufferedReader__chunk_size�_BufferedReader__buffered)�self�pathZ
max_in_memZ
chunk_size�moder r r �__init__5 s
zBufferedReader.__init__c C s | j S �N)r �r r r r �bufferedA s zBufferedReader.bufferedc C � | S r r r r r r �__iter__F � zBufferedReader.__iter__c C sz | j du r| j| j }d| _ nd}| j | jd� | _ | j�| j| �}tjj�|�}|s3| j� � t
�| j |7 _ | j S )z�
Return the next iteration by popping `chunk_size` from the left and
appending `chunk_size` to the right if there's info on the file left
to be read.
N� � )r r r r �readr r ZstringutilsZto_str�close�
StopIteration)r Z
multiplier�datar r r �nextI s
zBufferedReader.nextc C r r r r r r r � __enter__h r zBufferedReader.__enter__c C s | j jdu r
| j �� d S d S )NF)r �closedr$ )r �exc_type� exc_value� tracebackr r r �__exit__k s �zBufferedReader.__exit__N)r r r
)r r r r r �propertyr r r'