U
    Zwe                     @   s   d dl Z d dl mZ d dlmZmZmZ d dlmZ ddlmZ ddl	m
Z
 G dd	 d	ZG d
d dee
ZG dd dee
ZdS )    N)PathLike)UnionBinaryIOOptional)xopen   )SequenceRecord)SingleEndWriterc                   @   sl   e Zd ZU dZeed< eddeee	ef e
e dddZe	dd	d
ZddddZdd Zdd ZdS )
FileWriterzR
    A mix-in that manages opening and closing and provides a context manager
    _fileNopener_close_file)filer   c                C   sJ   zt | W n$ tk
r2   || _t|| _Y nX ||d| _d| _d S )NwbT)osfspath	TypeErrorr   bool_close_on_exit)selfr   r   r    r   ,lib/python3.8/site-packages/dnaio/writers.py__init__   s    zFileWriter.__init__returnc                 C   s    | j j dt| jd| j dS )Nz('name'))	__class____name__getattrr   r   r   r   r   __repr__#   s    zFileWriter.__repr__c                 C   s   | j r| j  d S N)r   r   closer!   r   r   r   r$   &   s    zFileWriter.closec                 C   s   | j jrtd| S )NzI/O operation on closed file)r   closed
ValueErrorr!   r   r   r   	__enter__*   s    zFileWriter.__enter__c                 G   s   |    d S r#   )r$   )r   argsr   r   r   __exit__/   s    zFileWriter.__exit__)r   
__module____qualname____doc__r   __annotations__r   r   r   strr   r   r   r"   r$   r'   r)   r   r   r   r   r
      s   
r
   c                       sh   e Zd ZdZdeddeeeef e	e
 e	e d fddZeddd	Zde	e d
ddZ  ZS )FastaWriteraS  
    Write FASTA-formatted sequences to a file

    While this class can be instantiated directly, the recommended way is to
    use `dnaio.open` with appropriate arguments unless you need to set the
    line_length argument.

    Arguments:
        line_length: Wrap sequence lines after this many characters (None disables wrapping)
    N)line_lengthr   r   )r   r0   r   c                   s(   t  j|||d |dkr|nd | _d S )Nr   r   )superr   r0   )r   r   r0   r   r   r   r   r   r   ?   s    zFastaWriter.__init__r   c                 C   s   dt | jd| j dS )NzFastaWriter('r   r   r    r   r!   r   r   r   r"   J   s    zFastaWriter.__repr__)sequencec                 C   s   |dkr|j }|j}n|}| jdk	r| jd| d d g }tdt|| jD ] }||||| j  d  qT| jd	|d n&d| d | d }| j|d dS )a  Write a record to the FASTA file.

        If only one parameter (name_or_record) is given, it must have
        attributes .name and .sequence, which are then used.
        Otherwise, the first parameter must be the name and the second
        the sequence.

        The effect is that you can write this:
        writer.write("name", "ACCAT")
        or
        writer.write(SequenceRecord("name", "ACCAT"))
        N>
asciir    )
r   r4   r0   r   writeencoderangelenappendjoin)r   Zname_or_recordr4   r   sitextr   r   r   r9   M   s    
zFastaWriter.write)N)r   r*   r+   r,   r   r   r   r.   r   r   intr   r   r"   r9   __classcell__r   r   r2   r   r/   3   s   r/   c                       s   e Zd ZdZdZdeddeeee	f e
ee
 d fddZed	d
dZeddddZeddddZeddddZeeeddddZ  ZS )FastqWriteraV  
    Write records in FASTQ format

    While this class can be instantiated directly, the recommended way is to
    use `dnaio.open` with appropriate arguments unless you need to set
    two_headers to True.

    Arguments:
        two_headers: If True, the header is repeated on the third line
            of each record after the "+".
    r   FN)two_headersr   r   )r   rE   r   c                   s6   t  j|||d || _t| d| jr*| jn| j d S )Nr   r9   )r1   r   Z_two_headerssetattr_write_two_headers_write)r   r   rE   r   r   r2   r   r   r   z   s      zFastqWriter.__init__r   c                 C   s   dt | jd| j dS )NzFastqWriter('r   r   r3   r!   r   r   r   r"      s    zFastqWriter.__repr__)recordr   c                 C   s   dst dS )3
        Write a record to the FASTQ file.
        FN)AssertionErrorr   rI   r   r   r   r9      s    zFastqWriter.writec                 C   s   | j |  dS )rJ   Nr   r9   Zfastq_bytesrL   r   r   r   rH      s    zFastqWriter._writec                 C   s   | j |jdd dS )zr
        Write a record to the FASTQ file, repeating the header
        in the third line after the "+" .
        T)rE   NrM   rL   r   r   r   rG      s    zFastqWriter._write_two_headers)r   r4   	qualitiesr   c              	   C   s0   | j d|dd|dd|ddd d S )N@r?   r6   z
+
r7   )r   r9   r:   )r   r   r4   rN   r   r   r   writeseq   s    zFastqWriter.writeseq)r   r*   r+   r,   Z	file_moder   r   r   r.   r   r   r   r   r"   r   r9   rH   rG   rP   rC   r   r   r2   r   rD   k   s   rD   )r   r   typingr   r   r   r   r8   r   Z
interfacesr	   r
   r/   rD   r   r   r   r   <module>   s   (8