o
    i:(                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZ e	r.ddlmZ eee dB edB edB f Zedd	d
ZG dd deZG dd deZG dd	 d	ZG dd deZdededefddZG dd deZdS )z<Hold DicomFile class, which does basic I/O for a dicom file.    )BytesION)Struct)TracebackType)TYPE_CHECKINGcastAnyTypeVarProtocol)CallableSelfDicomIO)boundc                   @   sF   e Zd ZddedefddZddededefdd	Zdefd
dZdS )ReadableBuffer.sizereturnc                C      d S N selfr   r   r   F/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/filebase.pyread       zReadableBuffer.readoffsetwhencec                C   r   r   r   r   r   r   r   r   r   seek   r   zReadableBuffer.seekc                 C   r   r   r   r   r   r   r   tell   r   zReadableBuffer.tellN.)__name__
__module____qualname__intbytesr   r   r   r   r   r   r   r      s    r   c                   @   sL   e Zd ZddededefddZdefddZd	eeB eB defd
dZ	dS )WriteableBuffer.r   r   r   c                C   r   r   r   r   r   r   r   r      r   zWriteableBuffer.seekc                 C   r   r   r   r   r   r   r   r      r   zWriteableBuffer.tellbc                C   r   r   r   r   r&   r   r   r   write!      zWriteableBuffer.writeNr   )
r    r!   r"   r#   r   r   r$   	bytearray
memoryviewr(   r   r   r   r   r%      s    
r%   c                   @   s  e Zd ZdZdeeB ddfddZdededefd	d
Zde	de	fddZ
deddfddZedefddZejdeddfddZedefddZejdeddfddZededB fddZejdeddfddZedeeB fddZd>dedefd d!Zd?d#ed$edefd%d&Zdeeef fd'd(Zdefd)d*Zdefd+d,Zejfd-ed.edefd/d0Zdefd1d2Z d3ee!B e"B defd4d5Z#d6eddfd7d8Z$d9eddfd:d;Z%d9eddfd<d=Z&dS )@r   z^Wrapper for managing buffer-like objects used when reading or writing
    DICOM datasets.
    bufferr   Nc                 C   s   |  |  |  |  |  |  |  |  || _ t| j dd| _t|dr$|j| _t|dr-|j| _t|dr6|j| _|j| _|j| _dS )a  Create a new ``DicomIO`` instance.

        Parameters
        ----------
        buffer : buffer-like object
            A buffer-like object that implements:

            * ``seek()`` and ``tell()`` methods with the same signatures as
              :meth:`io.IOBase.seek` and :meth:`io.IOBase.tell`
            * a ``read()`` method with the same signature as
              :meth:`io.RawIOBase.read` if it supports reading data from
              itself, and/or
            * a ``write()`` method with the same signature as
              :meth:`io.RawIOBase.write` if it supports writing data to itself

            If `buffer` supports reading it can be used with
            :func:`~pydicom.filereader.dcmread` as the source to decode a DICOM
            dataset from, and if it supports writing it can be used with
            :func:`~pydicom.filewriter.dcmwrite` as the destination for the
            encoded DICOM dataset.
        nameNr   r(   close)	_buffergetattr_namehasattrr   r(   r.   r   r   )r   r,   r   r   r   __init__+   s$   


zDicomIO.__init__argskwargsc                 O   s   dS )zClose the buffer (if possible)Nr   )r   r4   r5   r   r   r   r.   b   r)   zDicomIO.closer   c                 C   s   | S r   r   r   r   r   r   	__enter__f   s   zDicomIO.__enter__exc_infoc                 G   s   |    d S r   )r.   )r   r7   r   r   r   __exit__i   s   zDicomIO.__exit__c                 C   $   t | dstt| j d| jS )zwGet/set the endianness for encoding/decoding, ``True`` for little
        endian and ``False`` for big endian.
        _little_endianz#.is_little_endian' has not been set)r2   AttributeErrortyper    r:   r   r   r   r   is_little_endianl   
   
zDicomIO.is_little_endianvaluec                 C   s   t |tstdt| j d|| _d| }t| dj| _t| dj	| _
t| dj| _t| dj	| _t| dj| _t| dj	| _d S )N'z.is_little_endian' must be boolz><HL2H)
isinstancebool	TypeErrorr<   r    r:   r   pack
_us_packerunpack_us_unpacker
_ul_packer_ul_unpacker_tag_packer_tag_unpacker)r   r?   
endiannessr   r   r   r=   x   s   
c                 C   r9   )zsGet/set the VR mode for encoding/decoding. ``True`` for implicit VR
        and ``False`` for explicit VR.
        _implicit_vrz!.is_implicit_VR' has not been set)r2   r;   r<   r    rP   r   r   r   r   is_implicit_VR   r>   zDicomIO.is_implicit_VRc                 C   s*   t |tstdt| j d|| _d S )Nr@   z.is_implicit_VR' must be bool)rD   rE   rF   r<   r    rP   )r   r?   r   r   r   rQ      s   

c                 C      | j S )zReturn the value of the :attr:`~pydicom.filebase.DicomIO.parent`'s
        ``name`` attribute, or ``None`` if no such attribute.
        r1   r   r   r   r   r-      s   zDicomIO.namer-   c                 C   s
   || _ d S r   rS   )r   r-   r   r   r   r-      s   
c                 C   rR   )z'Return the buffer object being wrapped.)r/   r   r   r   r   parent   s   zDicomIO.parentr   c                C   $   t dt| j dt| jj d)zRead up to `size` bytes from the buffer and return them. If `size`
        is unspecified, all bytes until EOF are returned.

        Fewer than `size` bytes may be returned if the operating system call
        returns fewer than `size` bytes.
        r@   ' cannot be used with 'z': object has no read() methodrF   r<   r    r/   r   r   r   r   r      s
   
zDicomIO.read   length
nr_retriesc                 C   s   |  |}t||kr|S t|}d}t| }|k r8||k r8||  || 7 }|d7 }t| }|k r8||k s||kr@t|S td| d| d|  | d)a9  Return `length` bytes read from the buffer.

        Parameters
        ----------
        length : int
            The number of bytes to be read. If ``None`` (default) then read all
            the bytes available.
        nr_retries : int, optional
            The number of tries to read data when the number of bytes read
            from the buffer is less than `length`. Default ``3``.

        Returns
        -------
        bytes
            The read data.

        Raises
        ------
        EOFError
            If unable to read `length` bytes.
        r      zUnexpected end of file. Read z
 bytes of z! expected starting at position 0xx)r   lenr*   r$   EOFErrorr   )r   rZ   r[   
bytes_readattempts	num_bytesr   r   r   
read_exact   s    
zDicomIO.read_exactc                 C   s   t tttf | | dS )z.Return a DICOM tag value read from the buffer.   )r   tupler#   rN   rc   r   r   r   r   read_tag   s   
zDicomIO.read_tagc                 C      t t| | dd S )z'Return a UL value read from the buffer.rd   r   )r   r#   rL   r   r   r   r   r   read_UL      zDicomIO.read_ULc                 C   rg   )z'Return a US value read from the buffer.   r   )r   r#   rJ   r   r   r   r   r   read_US   ri   zDicomIO.read_USr   r   c                C      t  )zChange the buffer position to the given byte `offset`, relative to
        the position indicated by `whence` and return the new absolute position.
        NotImplementedErrorr   r   r   r   r      s   zDicomIO.seekc                 C   rl   )z0Return the current stream position of the bufferrm   r   r   r   r   r      s   zDicomIO.tellr&   c                C   rV   )zfWrite the bytes-like object `b` to the buffer and return the number
        of bytes written.
        r@   rW   z': object has no write() methodrX   r'   r   r   r   r(      s
   
zDicomIO.writetagc                 C   s   |  | |d? |d@  dS )z Write a DICOM tag to the buffer.   i  N)r(   rM   )r   ro   r   r   r   	write_tag  s   zDicomIO.write_tagvalc                 C      |  | | dS )zWrite a UL value to the buffer.N)r(   rK   r   rr   r   r   r   write_UL     zDicomIO.write_ULc                 C   rs   )zWrite a US value to the buffer.N)r(   rH   rt   r   r   r   write_US	  rv   zDicomIO.write_US)rU   )rY   )'r    r!   r"   __doc__r   r%   r3   r   r.   r   r6   ExitExceptionr8   propertyrE   r=   setterrQ   strr-   rT   r#   r$   r   rc   re   rf   rh   rk   osSEEK_SETr   r   r*   r+   r(   rq   ru   rw   r   r   r   r   r   &   s>    7)	c                   @   s   e Zd ZdZdS )DicomFileLikezWrapper for file-likes to simplify encoding/decoding DICOM datasets.

    See Also
    --------
    :class:`~pydicom.filebase.DicomIO`
    :class:`~pydicom.filebase.DicomBytesIO`
    N)r    r!   r"   rx   r   r   r   r   r     s    r   r4   r5   r   c                  O   s   t t| i |S )zKReturn an opened :class:`~pydicom.filebase.DicomFileLike` from a file-like.)r   open)r4   r5   r   r   r   	DicomFile  s   r   c                       s4   e Zd ZdZddeeB eB ddf fddZ  ZS )	DicomBytesIOzWrapper for :class:`io.BytesIO` to simplify encoding/decoding DICOM datasets.

    See Also
    --------
    :class:`~pydicom.filebase.DicomIO`
    :class:`~pydicom.filebase.DicomFileLike`
        initial_bytesr   Nc                    s    t |}t | |j| _dS )zCreate a new DicomBytesIO instance.

        Parameters
        ----------
        buffer : bytes | bytearray | memoryview, optional
            The buffer to write to or read from, default is an empty buffer.
        N)r   superr3   getvalue)r   r   r,   	__class__r   r   r3   (  s   zDicomBytesIO.__init__)r   )	r    r!   r"   rx   r$   r*   r+   r3   __classcell__r   r   r   r   r     s    (r   )rx   ior   r}   structr   typesr   typingr   r   r   r   r	   collections.abcr
   re   r<   BaseExceptionry   r   r   r%   r   r   r   r   r   r   r   r   <module>   s&   
 i