o
    7o5`                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ ddlmZmZmZ ddlmZmZmZ G d	d
 d
eZdd ZG dd dZG dd dZG dd dZdS )us   
Safely make I/O operations to files in Python even from multiple threads... and more!

© Anime no Sekai — 2020
    )sqrt)sleep)loadsdumps)Counter)ThreadLock)renameremovereplace)isfilegetsizeexistsc                       s   e Zd Z fddZ  ZS )OverwriteErrorc                    s   t t| | d S N)superr   __init__)selfmessage	__class__ H/var/www/html/env_mimamsha/lib/python3.10/site-packages/safeIO/safeIO.pyr      s   zOverwriteError.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c                    sv   dd }||  || d  d }t fdd|D } d d  }|dkr/dS || dkr7dS || S )z4
    Returns the similarity bewteen two strings
    c                 S   s6   t | }t|}ttdd | D }|||| fS )Nc                 s   s    | ]}|| V  qd S r   r   ).0cr   r   r   	<genexpr>   s    z7stringSimilarity.<locals>.get_vector.<locals>.<genexpr>)r   setr   sumvalues)wordcount_charactersset_characterslengthr   r   r   
get_vector   s   z$stringSimilarity.<locals>.get_vector   c                 3   s(    | ]} d  | d  |  V  qdS r   Nr   )r   	charactervector1vector2r   r   r       s   & z#stringSimilarity.<locals>.<genexpr>   r   )intersectionr!   )string1string2r'   common_charactersproduct_summationr&   r   r+   r   stringSimilarity   s   $r4   c                   @   s   e Zd ZdZd7d8ddZdefdd	Zdefd
dZdd Zdd Z	dd Z
dd Zdd Zdd Zd9ddZd9ddZd:ddZd:dd Zd9d!d"Zd9d#d$Zd;d&d'Zd;d(d)Zd9d*d+Zd;d,d-Zd;d.d/Zd;d0d1Zd9d2d3Zd<d5d6ZdS )=TextFilez
    A Text File object
    utf-8TreturnNc                 C   sT   t || _t || _t | dddv rdnd| _|| _d| _d| _t	 | _
d S N  trueyes1TFr(   r   )strfilepathencodinglowerr   blocking	_blocking_currentOperation_queueLengthr   _Lock)r   r@   rA   rC   r   r   r   r   '   s   

"zTextFile.__init__c                 C   
   d| j  S )C
        Representing the object -> str FileType: filepath
        z
TextFile: r@   r   r   r   r   __repr__0      
zTextFile.__repr__c                 C      | j S zE
        String representation of the object -> str filepath
        rJ   rK   r   r   r   __str__6      zTextFile.__str__c                 C   j   t |ttfr$| j}|j}d | _|_t|  | }|| _||_|S t |tr3t| jt|j S dS ^  
        Substract two safeIO objects

        Returns the Cosine Similarity of the two objects (float [0;1]) if they are both instances of safeIO.TextFile or safeIO.JSONFile

        Returns the difference in size between the two files if one of them is a BinaryFile instance

        Returns None if the other object is not a safeIO object
        TN	
isinstancer5   JSONFilerC   r4   read
BinaryFiler   r@   r   othercurrentFileBlockingotherFileBlockingresultr   r   r   __sub__<      
zTextFile.__sub__c                 C   V   t |tttfr)| j}|j}d | _|_|  | krd}nd}|| _||_|S dS z
        Checks for an equality between two safeIO objects (==)

        Returns True if the contents of the two files are the same

        Returns False else

        Returns None if the other object is not a safeIO object
        TFNrV   rY   r5   rW   rC   rX   rZ   r   r   r   __eq__Q      zTextFile.__eq__c                 C   V   t |tttfr)| j}|j}d | _|_|  | krd}nd}|| _||_|S dS z
        Checks for an inequality between two safeIO objects (!=)

        Returns False if the contents of the two files are the same

        Returns True else

        Returns None if the other object is not a safeIO object
        TFNrc   rZ   r   r   r   __ne__g   re   zTextFile.__ne__c                 C      |    S z3
        Returns self.readlines() iterator
        	readlines__iter__rK   r   r   r   rm   }      zTextFile.__iter__c                 C      | j | _d| _ | S z+
        'with' statement handling
        TrC   rD   rK   r   r   r   	__enter__      zTextFile.__enter__c                 C      | j | _dS z>
        Exit of 'with' statement (deleting the file)
        NrD   rC   r   typevalue	tracebackr   r   r   __exit__   rn   zTextFile.__exit__c                    <    j   d fdd	} jr| S t||gdd  dS );
        Wether the file exists on the disk or not
        Nc                    f    j d  } _ | j krtd | j kst j}  jd7  _ j  | du r-|S | | dS 2
            Internal name() function
            r(   MbP?NrF   r   r   r@   rE   rG   releasecallbackoperationIDdatarK   r   r   _isfile      



z TextFile.isfile.<locals>._isfileTtargetargsdaemonr   rG   acquirerC   r   startr   r   r   r   rK   r   r      
   
zTextFile.isfilec                    r|   )"
        Deletes the file
        Nc                    d    j d  } _ | j krtd | j kst j   jd7  _ j  | d u r-d S |   d S Nr(   r   rF   r   r
   r@   rE   rG   r   r   r   rK   r   r   _delete      




z TextFile.delete.<locals>._deleteTr   r   r   r   r   r   r   rK   r   delete   
   
zTextFile.deleteFc                    B   j   d fdd	}jr||S t|||gdd  dS );
        Renames the file and returns its new path
        Nc                       j d  }_ j}djv r!jd jd d |  _n| _|j kr2td |j ks) sEtjrEj}|_t|d t|j  jd7  _j	  |d u r^jS |j d S Nr(   /r   # already exists and overwrite=False)
rF   r@   rfindr   r   r   r   rE   rG   r   newNamer   r   oldFilepathnewFilepath	overwriter   r   r   _rename   &   
"



z TextFile.rename.<locals>._renameTr   r   r   r   r   r   r   r   r   r   r   r	      
   
zTextFile.renamec                    D    j   d fdd	} jr|||S t||||gdd  dS )9
        Moves the file and returns its new path
        FNc                        j d  } _  j}|  _| j krtd | j ks|s/t jr/ j}| _t|d t| j   jd7  _ j  |d u rH jS | j d S Nr(   r   r   	rF   r@   r   r   r   r   rE   rG   r   newPathr   r   r   r   r   rK   r   r   _move   "   



zTextFile.move.<locals>._moveTr   FNr   r   r   r   r   r   r   rK   r   move   
   

zTextFile.movec                    r|   )'
        Returns the file name
        Nc                        j d  } _ | j krtd | j kst jd jd}|j}W d   n1 s-w   Y    jd7  _ j  | du rD|S | | dS r   r(   r   rrA   N	rF   r   openr@   rA   namerE   rG   r   r   r   readingFiler   rK   r   r   _name     


zTextFile.name.<locals>._nameTr   r   r   r   r   r   r   rK   r   r      
   
zTextFile.namec                    r|   )w
        Returns the file descriptor (int) used by Python to request I/O operations from the operating system.
        Nc                        j d  } _ | j krtd | j kst jd jd}| }W d    n1 s.w   Y    jd7  _ j  | d u rE|S | | d S Nr(   r   r   r   	rF   r   r   r@   rA   filenorE   rG   r   r   rK   r   r   _fileno     



z TextFile.fileno.<locals>._filenoTr   r   r   r   r   r   r   rK   r   r     
   
zTextFile.filenor   c                    @    j   d fdd	} jr||S t|||gdd  dS )?
        Reads the entire file and returns its content
        r   Nc                    s    j d  } _ | j krtd | j kst js#t jd  t jd jd}|t|  |	 }W d    n1 sBw   Y    j
d7  _
 j  |d u rY|S || d S )Nr(   r   wr   r   )rF   r   r   r@   r   closerA   seekintrX   rE   rG   r   positionr   r   r   r   rK   r   r   _read3  s   




zTextFile.read.<locals>._readTr   r)   r   r   r   r   r   r   rK   r   rX   .  
   
zTextFile.readc                    r   )a
        Writes (or overwrites) to the file and returns the number of characters written
        r   Nc                    s    j d  } _ | j krtd | j kst jd jd}|t| |t| } W d    n1 s8w   Y    j	d7  _	 j
  |d u rO| S ||  d S )Nr(   r   r   r   )rF   r   r   r@   rA   r   r   writer?   rE   rG   r   r   r   r   r   writingFilerK   r   r   _writeM  s   


zTextFile.write.<locals>._writeTr   r)   r   r   r   r   r   r   r   rK   r   r   H  
   

zTextFile.writec                    @    j   d fdd	} jr||S t|||gdd  dS )R
        Appends to the file and returns the number of characters written
        Nc                    s    j d  } _ | j krtd | j kst jd jd}|t| } W d    n1 s1w   Y    jd7  _ j	  |d u rH| S ||  d S )Nr(   r   ar   )
rF   r   r   r@   rA   r   r?   rE   rG   r   r   r   r   r   rK   r   r   _appendf  s   


z TextFile.append.<locals>._appendTr   r   r   r   r   r   r   r   rK   r   appenda  
   
zTextFile.appendc                    r   )_
        Returns the line of the current position (from the position to the linebreak)
        r   Nc                    s    j d  } _ | j krtd | j kst jd jd}|t|  | }W d    n1 s5w   Y    jd7  _ j	
  |d u rL|S || d S r   )rF   r   r   r@   rA   r   r   readlinerE   rG   r   r   rK   r   r   	_readline}  s   



z$TextFile.readline.<locals>._readlineTr   r)   r   r   r   r   r   r   rK   r   r   x  
   
zTextFile.readlinec                    r   )X
        Reads the whole file and returns the lines (separated by a line break)
        r   Nc                    s    j d  } _ | j krtd | j kst jd jd}||  | }W d    n1 s3w   Y    jd7  _ j	  |d u rJ|S || d S r   )
rF   r   r   r@   rA   r   rl   rE   rG   r   r   rK   r   r   
_readlines  s   




z&TextFile.readlines.<locals>._readlinesTr   r)   r   r   r   r   r   r   rK   r   rl     r   zTextFile.readlinesc                    r   )L
        Writes (or overwrites) the given list of lines to the file
        r   Nc                    s    j d  } _ | j krtd | j kst jd jd}|| |t| tr1| 	dnt
|  W d    n1 s@w   Y    jd7  _ j  |d u rWd S |  d S )Nr(   r   r   r   
)rF   r   r   r@   rA   r   
writelinesrV   r?   splitlistrE   rG   r   r   rK   r   r   _writelines  s   


$

z(TextFile.writelines.<locals>._writelinesTr   r)   r   r   r   r   r   r   r   rK   r   r     r   zTextFile.writelinesc                    r   )=
        Appends the given list of lines to the file
        Nc                    s    j d  } _ | j krtd | j kst jd jd}|t| tr,| dnt	|  W d    n1 s;w   Y    j
d7  _
 j  |d u rRd S |  d S )Nr(   r   r   r   r   )rF   r   r   r@   rA   r   rV   r?   r   r   rE   rG   r   r   rK   r   r   _appendlines  s   

$

z*TextFile.appendlines.<locals>._appendlinesTr   r   r   r   r   r   r   r   rK   r   appendlines  r   zTextFile.appendlinesr   c                    r   )
        Returns the opened IO (TextIOWrapper)

        Warning: Make sure to close the file correctly after using the file
        r   Nc                    n    j d  } _ | j krtd | j kst j|  jd}  jd7  _ j  |d u r1|S || d S Nr(   r   r   rF   r   r   r@   rA   rE   rG   r   moder   r   fileIOrK   r   r   _detach     


z TextFile.detach.<locals>._detachTr   r   Nr   r   r  r   r  r   rK   r   detach  
   
zTextFile.detach)r6   Tr7   Nr   r   r)   r  r   r   r   __doc__r   r?   rL   rP   r_   rd   rh   rm   rr   r{   r   r   r	   r   r   r   rX   r   r   r   rl   r   r   r
  r   r   r   r   r5   #   s2    	



 








r5   c                   @   s   e Zd ZdZd6d7ddZdefddZdefd	d
Zdd Zdd Z	dd Z
dd Zdd Zdd Zd8ddZd8ddZd9ddZd9ddZd8d d!Zd8d"d#Zd:d%d&Zd:d'd(Zd8d)d*Zd:d+d,Zd:d-d.Zd:d/d0Zd8d1d2Zd;d4d5ZdS )<rY   z
    A Binary File object
    Tr7   Nc                 C   sL   t || _t | dddv rdnd| _| j| _d| _d| _t | _	d S r8   )
r?   r@   rB   r   rC   rD   rE   rF   r   rG   )r   r@   rC   r   r   r   r     s   
"zBinaryFile.__init__c                 C   rH   )rI   zBinaryFile: rJ   rK   r   r   r   rL     rM   zBinaryFile.__repr__c                 C   rN   rO   rJ   rK   r   r   r   rP     rQ   zBinaryFile.__str__c                 C   s(   t |tttfrt| jt|j S dS )rT   N)rV   rY   r5   rW   r   r@   )r   r[   r   r   r   r_     s   zBinaryFile.__sub__c                 C   ra   rb   rc   rZ   r   r   r   rd     re   zBinaryFile.__eq__c                 C   rf   rg   rc   rZ   r   r   r   rh   *  re   zBinaryFile.__ne__c                 C   ri   rj   rk   rK   r   r   r   rm   @  rn   zBinaryFile.__iter__c                 C   ro   rp   rq   rK   r   r   r   rr   G  rs   zBinaryFile.__enter__c                 C   rt   ru   rv   rw   r   r   r   r{   O  rn   zBinaryFile.__exit__c                    r|   )r}   Nc                    r~   r   r   r   rK   r   r   r   Z  r   z"BinaryFile.isfile.<locals>._isfileTr   r   r   r   r   rK   r   r   U  r   zBinaryFile.isfilec                    r|   )r   Nc                    r   r   r   r   rK   r   r   r   s  r   z"BinaryFile.delete.<locals>._deleteTr   r   r   r   r   rK   r   r   n  r   zBinaryFile.deleteFc                    r   )r   Nc                    r   r   
rF   r@   r   r   r   r   r	   rE   rG   r   r   r   r   r   r     r   z"BinaryFile.rename.<locals>._renameTr   r   r   r   r   r   r   r	     r   zBinaryFile.renamec                    r   )r   FNc                    r   r   r   r   rK   r   r   r     r   zBinaryFile.move.<locals>._moveTr   r   r   r   r   rK   r   r     r   zBinaryFile.movec                    r|   )r   Nc                    r   )r   r(   r   rbr   Nr   r   rK   r   r   r     r   zBinaryFile.name.<locals>._nameTr   r   r   r   r   rK   r   r     r   zBinaryFile.namec                    r|   )r   Nc                    s    j d  } _ | j krtd | j kst jd}| }W d    n1 s+w   Y    jd7  _ j  | d u rB|S | | d S Nr(   r   r  )rF   r   r   r@   r   rE   rG   r   r   rK   r   r   r     s   



z"BinaryFile.fileno.<locals>._filenoTr   r   r   r   r   rK   r   r     r   zBinaryFile.filenor   c                    r   )r   r   Nc                    s    j d  } _ | j krtd | j kst js#t jd  t jd}|t|  | }W d    n1 s?w   Y    j	d7  _	 j
  |d u rV|S || d S )Nr(   r   wbr  )rF   r   r   r@   r   r   r   r   rX   rE   rG   r   r   rK   r   r   r     s   




zBinaryFile.read.<locals>._readTr   r)   r   r   r   rK   r   rX     r   zBinaryFile.readc                    r   )z\
        Writes (or overwrites) to the file and returns the number of bytes written
        r   Nc                    s    j d  } _ | j krtd | j kst jd}|t| || } W d    n1 s3w   Y    jd7  _ j	  |d u rJ| S ||  d S Nr(   r   r  )
rF   r   r   r@   r   r   r   rE   rG   r   r   rK   r   r   r     s   


z BinaryFile.write.<locals>._writeTr   r)   r   r   r   rK   r   r     r   zBinaryFile.writec                    r   )zM
        Appends to the file and returns the number of bytes written
        Nc                    s    j d  } _ | j krtd | j kst jd}|| } W d    n1 s,w   Y    jd7  _ j  |d u rC| S ||  d S Nr(   r   ab)rF   r   r   r@   r   rE   rG   r   r   rK   r   r   r   )  s   


z"BinaryFile.append.<locals>._appendTr   r   r   r   r   rK   r   r   $  r   zBinaryFile.appendc                    r   )r   r   Nc                    s    j d  } _ | j krtd | j kst jd}|t|  | }W d    n1 s2w   Y    jd7  _ j	  |d u rI|S || d S r  )
rF   r   r   r@   r   r   r   rE   rG   r   r   rK   r   r   r   @  s   



z&BinaryFile.readline.<locals>._readlineTr   r)   r   r   r   rK   r   r   ;  r   zBinaryFile.readlinec                    r   )r   r   Nc                    s    j d  } _ | j krtd | j kst jd}||  | }W d    n1 s0w   Y    jd7  _ j  |d u rG|S || d S r  )	rF   r   r   r@   r   rl   rE   rG   r   r   rK   r   r   r   X  s   




z(BinaryFile.readlines.<locals>._readlinesTr   r)   r   r   r   rK   r   rl   S  r   zBinaryFile.readlinesc                    r   )r   r   Nc                    s    j d  } _ | j krtd | j kst jd}|| ||  W d    n1 s1w   Y    jd7  _ j  |d u rHd S |  d S r  )	rF   r   r   r@   r   r   rE   rG   r   r   rK   r   r   r   q  s   




z*BinaryFile.writelines.<locals>._writelinesTr   r)   r   r   r   rK   r   r   l  r   zBinaryFile.writelinesc                    r   )r   Nc                    s    j d  } _ | j krtd | j kst jd}||  W d    n1 s,w   Y    jd7  _ j  |d u rCd S |  d S r  )rF   r   r   r@   r   rE   rG   r   r   rK   r   r   r     s   



z,BinaryFile.appendlines.<locals>._appendlinesTr   r   r   r   r   rK   r   r     r   zBinaryFile.appendlinesr  c                    r   )z
        Returns the opened IO (TextIOWrapper)

        Tips: Make sure to include the "b" access mode in the mode

        Warning: Make sure to close the file correctly after using the file
        r  Nc                    sh    j d  } _ | j krtd | j kst j| }  jd7  _ j  |d u r.|S || d S r   )rF   r   r   r@   rE   rG   r   r  rK   r   r   r    s   


z"BinaryFile.detach.<locals>._detachTr   r  Nr   r	  r   rK   r   r
    s
   
zBinaryFile.detach)Tr  r   r   r)   r  r  r   r   r   r   rY     s2    



 








rY   c                   @   s   e Zd ZdZd1d2d	d
ZdefddZdefddZdd Zdd Z	dd Z
dd Zdd Zdd Zd3ddZd3ddZd4dd Zd4d!d"Zd3d#d$Zd3d%d&Zd5d(d)Zd5d*d+Zd3d,d-Zd6d/d0ZdS )7rW   z
    A JSON File object
    F   z, z: r6   Tr7   Nc                 C   s   t || _t || _t | dddv rdnd| _| j| _t | dddv r-dnd| _t | dddv r=nnd | _d| _	n|d urMt
|nd | _|| _	d| _d| _t | _d S )	Nr9   r:   r;   TF),:r(   r   )r?   r@   rA   rB   r   rC   rD   ensure_asciiindent
separatorsr   rE   rF   r   rG   )r   r@   r  minifyr  r  rA   rC   r   r   r   r     s   

""zJSONFile.__init__c                 C   rH   )rI   z
JSONFile: rJ   rK   r   r   r   rL     rM   zJSONFile.__repr__c                 C   rN   rO   rJ   rK   r   r   r   rP     rQ   zJSONFile.__str__c                 C   rR   rS   rU   rZ   r   r   r   r_     r`   zJSONFile.__sub__c                 C   ra   rb   rc   rZ   r   r   r   rd     re   zJSONFile.__eq__c                 C   rf   rg   rc   rZ   r   r   r   rh     re   zJSONFile.__ne__c                 C   ri   rj   rk   rK   r   r   r   rm     rn   zJSONFile.__iter__c                 C   ro   rp   rq   rK   r   r   r   rr     rs   zJSONFile.__enter__c                 C   rt   ru   rv   rw   r   r   r   r{   #  rn   zJSONFile.__exit__c                    r|   )r}   Nc                    r~   r   r   r   rK   r   r   r   .  r   z JSONFile.isfile.<locals>._isfileTr   r   r   r   r   rK   r   r   )  r   zJSONFile.isfilec                    r|   )r   Nc                    r   r   r   r   rK   r   r   r   G  r   z JSONFile.delete.<locals>._deleteTr   r   r   r   r   rK   r   r   B  r   zJSONFile.deletec                    r   )r   Nc                    r   r   r  r   r   r   r   r   ]  r   z JSONFile.rename.<locals>._renameTr   r   r   r   r   r   r   r	   X  r   zJSONFile.renamec                    r   )r   FNc                    r   r   r   r   rK   r   r   r   }  r   zJSONFile.move.<locals>._moveTr   r   r   r   r   rK   r   r   x  r   zJSONFile.movec                    r|   )r   Nc                    r   r   r   r   rK   r   r   r     r   zJSONFile.name.<locals>._nameTr   r   r   r   r   rK   r   r     r   zJSONFile.namec                    r|   )r   Nc                    r   r   r   r   rK   r   r   r     r   z JSONFile.fileno.<locals>._filenoTr   r   r   r   r   rK   r   r     r   zJSONFile.filenor   c                    r   )r   r   Nc                    s    j d  } _ | j krtd | j kst js#t jd  t jd jd"}|t|  |	 }|
dddkrEt|}ni }W d    n1 sQw   Y    jd7  _ j  |d u rh|S || d S )Nr(   r   r   r   r   r9   r:   )rF   r   r   r@   r   r   rA   r   r   rX   r   r   rE   rG   r   r   rK   r   r   r     s&   




zJSONFile.read.<locals>._readTr   r)   r   r   r   rK   r   rX     s
   
zJSONFile.readc                    r   )r   r   Nc              	      s    j d  } _ | j krtd | j kst jd jd}|t| |t|  j	 j
 jd} W d    n1 s?w   Y    jd7  _ j  |d u rV| S ||  d S )Nr(   r   r   r   r  r  r  )rF   r   r   r@   rA   r   r   r   r   r  r  r  rE   rG   r   r   rK   r   r   r     s   


zJSONFile.write.<locals>._writeTr   r)   r   r   r   rK   r   r     r   zJSONFile.writec                    r   )r   Nc              	      s    j d  } _ | j krtd | j kst jd jd}|t|  j j j	d} W d    n1 s8w   Y    j
d7  _
 j  |d u rO| S ||  d S )Nr(   r   r   r   r  )rF   r   r   r@   rA   r   r   r  r  r  rE   rG   r   r   rK   r   r   r     s   


z JSONFile.append.<locals>._appendTr   r   r   r   r   rK   r   r     r   zJSONFile.appendr   c                    r   )r   r   Nc                    r   r  r  r  rK   r   r   r    r  z JSONFile.detach.<locals>._detachTr   r  r   r	  r   rK   r   r
    r  zJSONFile.detach)FFr  r  r6   Tr  r   r   r)   r  )r   r   r   r  r   r?   rL   rP   r_   rd   rh   rm   rr   r{   r   r   r	   r   r   r   rX   r   r   r
  r   r   r   r   rW     s*    



 




rW   N)r  mathr   timer   jsonr   r   collectionsr   	threadingr   r   osr	   r
   r   os.pathr   r   r   	Exceptionr   r4   r5   rY   rW   r   r   r   r   <module>   s&       O   H