o
    wi7                     @   s>  d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dlZd dl	Z	d dl
Z
d dlZe
jd  dkZerAd dlmZ efZeZnd dlmZ efZeZdZ	 erWd!ddZnejZG dd deZG dd deZd"ddZedd ZG dd deZG dd deZdd ZG dd deZ dd Z!G dd  d eZ"dS )#    )unicode_literals)OrderedDict)contextmanagerN)
TextIOBase   )MutableMappingzutf-8rc                 C   s   t | tr
| t} |dks|du rtd|dkr/tj| dtj|d}tt	j
|jtd|S |dkrCtj| dtj|d}t|j|S |d	kr\tj| dtj|d
}tt	j
|jtd|S |dkrptj| dtj|d
}t|j|S td|)zOriginated from python os module

        Extend for supporting mode == 'rb' and 'wb'

        Args:
            cmd (str):
            mode (str):
            buffering (int):
        r   Nz+popen() does not support unbuffered streamsr   T)shellstdoutbufsizeencodingrbw)r
   stdinr   wbzUnsupported mode == {})
isinstance	text_typeencodedefault_encoding
ValueError
subprocessPopenPIPE_wrap_closeioTextIOWrapperr   r   	TypeErrorformat)cmdmode	bufferingproc r$   J/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/kaldiio/utils.pymy_popen=   s:   






r&   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )r   z_Originated from python os module

    A proxy for a file whose close waits for the process
    c                 C   s   || _ || _d S N)_stream_proc)selfstreamr#   r$   r$   r%   __init__p      
z_wrap_close.__init__c                 C   s6   | j   | j }|dkrd S tjdkr|S |d> S )Nr   nt   )r(   closer)   waitosname)r*   
returncoder$   r$   r%   r0   t   s   


z_wrap_close.closec                 C   s   | S r'   r$   r*   r$   r$   r%   	__enter__~      z_wrap_close.__enter__c                 G   s   |    d S r'   )r0   r*   argsr$   r$   r%   __exit__      z_wrap_close.__exit__c                 C      t | j|S r'   )getattrr(   r*   r3   r$   r$   r%   __getattr__   r;   z_wrap_close.__getattr__c                 C   
   t | jS r'   )iterr(   r5   r$   r$   r%   __iter__      
z_wrap_close.__iter__N)
__name__
__module____qualname____doc__r,   r0   r6   r:   r?   rB   r$   r$   r$   r%   r   j   s    
r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )_stdstream_wrapc                 C   s
   || _ d S r'   fd)r*   rJ   r$   r$   r%   r,      rC   z_stdstream_wrap.__init__c                 C   s   | j S r'   rI   r5   r$   r$   r%   r6      s   z_stdstream_wrap.__enter__c                 G      d S r'   r$   r8   r$   r$   r%   r:         z_stdstream_wrap.__exit__c                 C   rK   r'   r$   r5   r$   r$   r%   r0      rL   z_stdstream_wrap.closec                 C   r<   r'   )r=   rJ   r>   r$   r$   r%   r?      r;   z_stdstream_wrap.__getattr__c                 C   r@   r'   )rA   rJ   r5   r$   r$   r%   rB      rC   z_stdstream_wrap.__iter__N)	rD   rE   rF   r,   r6   r:   r0   r?   rB   r$   r$   r$   r%   rH      s    rH   c                 C   sF  t | tstrd|v rt | tr| jS | S |  dr,|  dd t}t	||S |  
drC|  dd t}t	||S | dkrgd|v rgtrb|dkrWttjjS ttjtjjtd	S ttjS | dkrd
|v ssd|v rtr|dks}|dkrttjjS ttjtjjtd	S ttjS d|v rdnt}tj| ||d	S )zYOpen a file like kaldi io

    Args:
        name (str or file):
        mode (str):
    b|Nr	      -r   r   r   r   ar   ab)r   string_typesPY3r   bufferstripendswithr   r   r&   
startswithrH   sysr   r   r   r   open)r3   r!   r    r   r$   r$   r%   open_like_kaldi   s8   




r[   c                 c   sp    t | trd|v rd nt}tj| ||d}ntr&d|v r&t | tr&| j}n| }|V  t | tr6|  d S d S )NrM   r   )	r   rS   r   r   rZ   rT   r   rU   r0   )fnamer!   r   fr$   r$   r%   
open_or_fd   s   

r^   c                   @   sF   e Zd ZdZdd ZdddZdd Zd	d
 ZdddZdddZ	dS )MultiFileDescriptora  What is this class?

    First of all, I want to load all format kaldi files
    only by using read_kaldi function, and I want to load it
    from file and file descriptor including standard input stream.
    To judge its file format it is required to make the
    file descriptor read and seek(to return original position).
    However, stdin is not seekable, so I create this clas.
    This class joints multiple file descriptors
    and I assume this class is used as follwoing,

        >>> string = fd.read(size)
        >>> # To check format from string
        >>> _fd = StringIO(string)
        >>> newfd = MultiFileDescriptor(_fd, fd)
    c                 G   s.   || _ |  rdd | j D | _d S d | _d S )Nc                 S   s   g | ]}|  qS r$   )tell.0r]   r$   r$   r%   
<listcomp>   s    z0MultiFileDescriptor.__init__.<locals>.<listcomp>)fdsseekableinit_pos)r*   rd   r$   r$   r%   r,      s   
zMultiFileDescriptor.__init__r   c                 C   s   |   s
trtt|dkrtd||dkr!||  7 }d}|dkrFt| jD ]\}}| j	| }|
|| d || | 8 }q*d S td|)Nr   z	offset={}rO   zfrom_what={})re   rT   OSErrorIOErrorNotImplementedErrorr   r`   	enumeraterd   rf   seek)r*   offset	from_whatidxr]   posr$   r$   r%   rk      s    
zMultiFileDescriptor.seekc                 C   s   t dd | jD S )Nc                 s   s    | ]}t |V  qd S r'   )re   ra   r$   r$   r%   	<genexpr>  s    z/MultiFileDescriptor.seekable.<locals>.<genexpr>)allrd   r5   r$   r$   r%   re     s   zMultiFileDescriptor.seekablec                    s0      s
trttt fddt jD S )Nc                 3   s&    | ]\}}|   j|  V  qd S r'   )r`   rf   )rb   rn   r]   r5   r$   r%   rp     s   $ z+MultiFileDescriptor.tell.<locals>.<genexpr>)re   rT   rg   rh   sumrj   rd   r5   r$   r5   r%   r`     s
   zMultiFileDescriptor.tellr	   c                 C   s`   |}d }| j D ]&}|d u r||}n|||7 }|t| }|dkr' |S |dk r-d}q|S )Nr   r	   )rd   readlenr*   sizeremainstringr]   r$   r$   r%   rs     s   
zMultiFileDescriptor.readc                 C   s   |}d }| j D ]<}|d u r||}n|||7 }t|tr(|dr' |S n|dr0 |S |t| }|dkr= |S |dk rCd}q|S )N
   
r   r	   )rd   readliner   r   rW   rt   ru   r$   r$   r%   r{   ,  s,   


	
zMultiFileDescriptor.readlineNr   r	   )
rD   rE   rF   rG   r,   rk   re   r`   rs   r{   r$   r$   r$   r%   r_      s    

r_   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	d,ddZ
d,ddZd-ddZdd Zdd Zd.ddZdd Zd d! Zd"d# Zd,d$d%Zd&d' Zd(d) Zd*d+ ZdS )/CountFileDescriptorc                 C   s   || _ d| _d S )Nr   )r]   position)r*   r]   r$   r$   r%   r,   C  r-   zCountFileDescriptor.__init__c                 C   
   | j  S r'   )r]   r0   r5   r$   r$   r%   r0   G  rC   zCountFileDescriptor.closec                 C   r   r'   )r]   closedr5   r$   r$   r%   r   J  rC   zCountFileDescriptor.closedc                 C   r   r'   )r]   flienor5   r$   r$   r%   filenoM  rC   zCountFileDescriptor.filenoc                 C   r   r'   )r]   flushr5   r$   r$   r%   r   P  rC   zCountFileDescriptor.flushc                 C   r   r'   )r]   isattyr5   r$   r$   r%   r   S  rC   zCountFileDescriptor.isattyc                 C   r   r'   )r]   readabler5   r$   r$   r%   readbaleV  rC   zCountFileDescriptor.readbaler	   c                 C   "   | j |}|  jt|7  _|S r'   )r]   r{   r   rt   )r*   rv   liner$   r$   r%   r{   Y     zCountFileDescriptor.readlinec                 C   s,   | j |}|D ]}|  jt|7  _q|S r'   )r]   	readlinesr   rt   )r*   hintlinesr   r$   r$   r%   r   ^  s   zCountFileDescriptor.readlinesr   c                 C   s   t d)NzCan't use seek)RuntimeError)r*   rl   whencer$   r$   r%   rk   d     zCountFileDescriptor.seekc                 C   s   dS )NFr$   r5   r$   r$   r%   re   g  r7   zCountFileDescriptor.seekablec                 C   r   r'   )r]   r`   r5   r$   r$   r%   r`   j  rC   zCountFileDescriptor.tellNc                 C   s   | j |S r'   )r]   	trauncate)r*   rv   r$   r$   r%   truncatem  r;   zCountFileDescriptor.truncatec                 C   r   r'   )r]   writabler5   r$   r$   r%   r   p  rC   zCountFileDescriptor.writablec                 C   s(   |D ]}|  j t|7  _ q| j|S r'   )r   rt   r]   
writelines)r*   r   r   r$   r$   r%   r   s  s   zCountFileDescriptor.writelinesc                 C   s   |   S r'   )__del__r5   r$   r$   r%   r   x  r   zCountFileDescriptor.__del__c                 C   r   r'   )r]   rs   r   rt   )r*   rv   datar$   r$   r%   rs   {  r   zCountFileDescriptor.readc                 C   s    | j  }|  jt|7  _|S r'   )r]   readallr   rt   )r*   r   r$   r$   r%   r     s   
zCountFileDescriptor.readallc                 C   s   | j |}|  j|7  _|S r'   )r]   readinfor   )r*   rM   nbyter$   r$   r%   r     s   zCountFileDescriptor.readinfoc                 C   s   |  j |7  _ | | d S r'   )r   write)r*   rM   r$   r$   r%   r     s   zCountFileDescriptor.writer}   r|   r'   )rD   rE   rF   r,   r0   r   r   r   r   r   r{   r   rk   re   r`   r   r   r   r   rs   r   r   r   r$   r$   r$   r%   r~   B  s*    




r~   c              	   C   s  |  dd}t|dkrd| vrtd| |\}}| d}d|vr/d|vr/td| d|v rId|v rId|vrBtd	| | dd}n|g}d
d
ddddddd}|D ]+}||vrgtd|||dv r|| d
urxtd||d||< qYd||< qY|S )a  A utility to parse "specifier"

    Args:
        specifier (str):
    Returns:
        parsed_dict (OrderedDict):
            Like {'ark': 'file.ark', 'scp': 'file.scp'}


    >>> d = parse_specifier('ark,t,scp:file.ark,file.scp')
    >>> print(d['ark,t'])
    file.ark

    :rO      znThe output file must be specified with kaldi-specifier style, e.g. ark,scp:out.ark,out.scp, but you gave as {},arkscpzEOne of/both ark and scp is required: e.g. ark,scp:out.ark,out.scp: {}zZYou specified both ark and scp, but a file path is given: e.g. ark,scp:out.ark,out.scp: {}NF)r   r   topr]   scszUnknown option {}({}))r   r   zYou specified {} twicer   T)splitrt   r   r   pop)	specifiersptypesfiles	spec_dictr   r$   r$   r%   parse_specifier  sP   


r   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )
LazyLoaderzDon't use this class directlyc                 C   s   i | _ || _d S r'   )_dict_loader)r*   loaderr$   r$   r%   r,     r-   zLazyLoader.__init__c                 C   s   d t| S )NzLazyLoader [{} keys])r   rt   r5   r$   r$   r%   __repr__     zLazyLoader.__repr__c                 C   s8   | j | }z| |W S  ty   td|  w )Nz"An error happend when loading "{}")r   r   	Exceptionwarningswarnr   )r*   keyark_namer$   r$   r%   __getitem__  s   
zLazyLoader.__getitem__c                 C   s   || j |< d S r'   r   r*   r   valuer$   r$   r%   __setitem__  r   zLazyLoader.__setitem__c                 C   s   | j |= d S r'   r   r*   r   r$   r$   r%   __delitem__  r;   zLazyLoader.__delitem__c                 C   r   r'   )r   rB   r5   r$   r$   r%   rB     rC   zLazyLoader.__iter__c                 C   r@   r'   )rt   r   r5   r$   r$   r%   __len__  rC   zLazyLoader.__len__c                 C   s
   || j v S r'   r   r*   itemr$   r$   r%   __contains__  rC   zLazyLoader.__contains__N)rD   rE   rF   rG   r,   r   r   r   r   rB   r   r   r$   r$   r$   r%   r     s    r   c              	   C   sH   t | dr	|  S t | dr"z|   W dS  ttfy!   Y dS w dS )Nre   r`   FT)hasattrre   r`   rh   rg   )r]   r$   r$   r%   re     s   


re   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )LimitedSizeDictc                 C   s   || _ t | _d S r'   )_maxsizer   r   )r*   maxsizer$   r$   r%   r,     s   zLimitedSizeDict.__init__c                 C   r@   r'   )reprr   r5   r$   r$   r%   r   
  rC   zLimitedSizeDict.__repr__c                 C   s2   t | | jkr| jtt| j || j|< d S r'   )rt   r   r   r   nextrA   r   r$   r$   r%   r     s   zLimitedSizeDict.__setitem__c                 C   s
   | j | S r'   )r   r   r$   r$   r%   r     rC   zLimitedSizeDict.__getitem__c                 C   s   |  j d8  _ | j|= d S )NrO   )r   r   r   r$   r$   r%   r     s   zLimitedSizeDict.__delitem__c                 C   r@   r'   )rA   r   r5   r$   r$   r%   rB     rC   zLimitedSizeDict.__iter__c                 C   r@   r'   )rt   r   r5   r$   r$   r%   r     rC   zLimitedSizeDict.__len__N)
rD   rE   rF   r,   r   r   r   r   rB   r   r$   r$   r$   r%   r     s    r   )r   r	   )r   )#
__future__r   collectionsr   
contextlibr   r   r   r2   r   rY   r   version_inforT   collections.abcr   strrS   r   
basestringunicoder   r&   popenobjectr   rH   r[   r^   r_   r~   r   r   re   r   r$   r$   r$   r%   <module>   sB    *!
1
^MC"