
    Cc0|                     8   d Z g dZddlZddlmZ ddlmZ ddlZddlm	Z	m
Z
mZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ  G d de
      Z G d de      Z G d dee      Zd Z G d de      Z G d de      Z G d deej<                        Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d  d!e      Z$ G d" d#e      Z% G d$ d%e      Z& G d& d'e      Z' G d( d)ejP                        Z)e)Z* ee+e       ed*d+,      d-               Z,y).a  Read and write FLAC Vorbis comments and stream information.

Read more about FLAC at http://flac.sourceforge.net.

FLAC supports arbitrary metadata blocks. The two most interesting ones
are the FLAC stream information block, and the Vorbis comment block;
these are also the only ones Mutagen can currently read.

This module does not handle Ogg FLAC files.

Based off documentation available at
http://flac.sourceforge.net/format.html
)FLACOpendelete    N)BytesIO   )VCommentDict)resize_bytesMutagenErrorget_sizeloadfileconvert_errorbchrendswith)PaddingInfo)BitPaddedInt)reducec                       e Zd Zy)errorN__name__
__module____qualname__     ./usr/lib/python3/dist-packages/mutagen/flac.pyr   r   $       r   r   c                       e Zd Zy)FLACNoHeaderErrorNr   r   r   r   r   r   (   r   r   r   c                       e Zd Zy)FLACVorbisErrorNr   r   r   r   r    r    ,   r   r   r    c                 0    t        d t        |       d      S )zMConvert an arbitrarily-long string to a long using big-endian
    byte order.c                     | dz  |z   S )N   r   )abs     r   <lambda>zto_int_be.<locals>.<lambda>3   s    Q!| r   r   )r   	bytearraydatas    r   	to_int_ber*   0   s     +Yt_a@@r   c                   $    e Zd ZdZd ZddZd Zy)StrictFileObjectzlWraps a file-like object and raises an exception if the requested
    amount of data to read isn't returned.c           	      h    || _         dD ]&  }t        ||      st        | |t        ||             ( y )N)closetellseekwritenameflushtruncate)_fileobjhasattrsetattrgetattr)selffileobjms      r   __init__zStrictFileObject.__init__:   s7     	6Aw"a!!45	6r   c                     | j                   j                  |      }|dk\  r't        |      |k7  rt        d|t        |      fz        |S )Nr   z!file said %d bytes, read %d bytes)r5   readlenr   )r9   sizer)   s      r   r>   zStrictFileObject.readA   sP    }}!!$'19Td*;c$i?) ) * *r   c                 4     | j                   j                  | S N)r5   r>   )r9   argss     r   tryreadzStrictFileObject.tryreadH   s    !t}}!!4((r   N))r   r   r   __doc__r<   r>   rD   r   r   r   r,   r,   6   s    .6)r   r,   c                   T    e Zd ZdZdZ	 dZ	 dZd Zd Zd Z	e
dd       Ze
d	        Zy
)MetadataBlocka  A generic block of FLAC metadata.

    This class is extended by specific used as an ancestor for more specific
    blocks, and also as a container for data blobs of unknown blocks.

    Attributes:
        data (`bytes`): raw binary data for this block
    FrE   i c                     |`t        |t              s>t        |t              rt        |      }nt	        |d      st        d      t        |      }| j                  |       yy)zqParse the given data string or file-like as a metadata block.
        The metadata header should not be included.Nr>   z.StreamInfo requires string data or a file-like)
isinstancer,   bytesr   r6   	TypeErrorloadr9   r)   s     r   r<   zMetadataBlock.__init__a   s^     d$45dE*"4=D v.#HJ J'-IIdO r   c                 .    |j                         | _        y rB   )r>   r)   rN   s     r   rM   zMetadataBlock.loadn   s    IIK	r   c                     | j                   S rB   r(   r9   s    r   r1   zMetadataBlock.writeq   s    yyr   c                    t               }|r|j                  dz  n|j                  }|j                         }t        |      }|| j                  kD  r3|j
                  r|j                  dk7  r|j                  }nt        d      || j                  kD  rJ t        j                  d|      dd }|j                  |       ||z  }||z  }|S )zCReturns the block content + header.

        Raises error.
           rE   zblock is too long to write>IN)r'   coder1   r?   	_MAX_SIZE_distrust_size_invalid_overflow_sizer   structpackappend)clsblockis_lastr)   rV   datumr@   lengths           r   _writeblockzMetadataBlock._writeblockt   s     {%,

S %**5z#--##(D(D(J 33899#--'''T4(-Dr   c                 t   t               }|D ]'  }t        |t              r|| j                  |      z  }) t	        |      }t               }|t	        | j                  |            z  }t        ||z
  |      }	t        |	j                  |      | j                        |_	        || j                  |d      z  }|S )z'Render metadata block as a byte string.T)r_   )
r'   rJ   Paddingrb   r?   r   min_get_paddingrW   ra   )
r]   blocks	available	cont_sizepadding_funcr)   r^   
blockssizepadding_blockinfos
             r   _writeblockszMetadataBlock._writeblocks   s    
 { 	+E%)COOE**D	+ Y
  	c#//-899
 9z19="4#4#4\#B#&== 2t<<r   NF)r   r   r   rF   rX   rY   rW   r<   rM   r1   classmethodrb   rn   r   r   r   rH   rH   L   s^     N,  - I   6  r   rH   c                   H    e Zd ZdZdZdZd Zej                  Zd Z	d Z
d Zy)
StreamInfoa/  StreamInfo()

    FLAC stream information.

    This contains information about the audio data in the FLAC file.
    Unlike most stream information objects in Mutagen, changes to this
    one will rewritten to the file when it is saved. Unless you are
    actually changing the audio stream itself, don't change any
    attributes of this block.

    Attributes:
        min_blocksize (`int`): minimum audio block size
        max_blocksize (`int`): maximum audio block size
        sample_rate (`int`): audio sample rate in Hz
        channels (`int`): audio channels (1 for mono, 2 for stereo)
        bits_per_sample (`int`): bits per sample
        total_samples (`int`): total samples in file
        length (`float`): audio length in seconds
        bitrate (`int`): bitrate in bits per second, as an int
    r   c                 b   	 | j                   |j                   k(  xr | j                  |j                  k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S # t        $ r Y yw xY wNF)min_blocksizemax_blocksizesample_ratechannelsbits_per_sampletotal_samples	Exceptionr9   others     r   __eq__zStreamInfo.__eq__   s    	&&%*=*== >&&%*=*==>$$(9(99> MMU^^3> ((E,A,AA	>
 &&%*=*==?  		s   BB" "	B.-B.c                 \   t        t        |j                  d                  | _        t        t        |j                  d                  | _        t        t        |j                  d                  | _        t        t        |j                  d                  | _        t        |j                  d            }t        |j                  d            }t        |j                  d            }|dz	  }t        |dz  |z         | _        | j                  st        d      t        |dz	  dz  dz         | _	        |dz	  }|dz  dz  }t        ||z   dz         | _
        |d	z  | _        | j                  t        | j                        z  | _        t        |j                  d
            | _        y )N      r         z#A sample rate value of 0 is invalid   $   l   ?    )intr*   r>   ru   rv   min_framesizemax_framesizerw   r   rx   ry   rz   floatra   md5_signature)r9   r)   sample_firstsample_channels_bps	bps_totalsample_tailbps_tailbps_heads           r   rM   zStreamInfo.load   sa    499Q<!89 499Q<!89 499Q<!89 499Q<!89 1.'		!5diil+	)Q. 1[@A=>>1Q6!;q@A?'!+1"8h#6#:;&4((51A1A+BB&tyy}5r   c           
      f   t               }|j                  t        j                  d| j                        dd         |j                  t        j                  d| j
                        dd         |j                  t        j                  d| j                        dd         |j                  t        j                  d| j                        dd         |j                  t        j                  d| j                  dz	        dd         | j                  dz  dz  }|| j                  dz
  dz  dz  z  }|| j                  dz
  dz	  dz  z  }|j                  t        |             | j                  dz
  dz  dz  }|| j                  dz	  dz  z  }|j                  t        |             |j                  t        j                  d| j                  d	z               | j                  }|j                  t        j                  d
|dz	  d	z  |dz	  d	z  |dz	  d	z  |d	z               |j                         S )NrT   rU   r      r   r       l    z>4I`   @   )r   r1   rZ   r[   ru   rv   r   r   rw   rx   ry   r   rz   r   getvalue)r9   fbytesigs       r   r1   zStreamInfo.write   s   I	D$"4"45bc:;	D$"4"45bc:;	D$"4"45bc:;	D$"4"45bc:; 	
D$"2"2a"78=>  3&1,$--!#q(Q..$&&*q0A55	T
%%)S0Q6##r)S00	T
	D$"4"4z"ABC  	C2I+cRi:-EBY*$cJ&68 	9 zz|r   c                 8    d| j                   | j                  fz  S )NzFLAC, %.2f seconds, %d Hz)ra   rw   rQ   s    r   pprintzStreamInfo.pprint  s    +t{{D<L<L.MMMr   N)r   r   r   rF   rV   bitrater~   rH   __hash__rM   r1   r   r   r   r   rr   rr      s5    * DG	 %%H626Nr   rr   c                   ^     e Zd ZdZ fdZd Z ed       Z ed       Z ed       Z	 xZ
S )	SeekPointa  SeekPoint()

    A single seek point in a FLAC file.

    Placeholder seek points have first_sample of 0xFFFFFFFFFFFFFFFFL,
    and byte_offset and num_samples undefined. Seek points must be
    sorted in ascending order by first_sample number. Seek points must
    be unique by first_sample number, except for placeholder
    points. Placeholder points must occur last in the table and there
    may be any number of them.

    Attributes:
        first_sample (`int`): sample number of first sample in the target frame
        byte_offset (`int`): offset from first frame to target frame
        num_samples (`int`): number of samples in target frame
    c                 2    t         t        |   | |||f      S rB   )superr   __new__)r]   first_samplebyte_offsetnum_samples	__class__s       r   r   zSeekPoint.__new__  s#    Y,,[9; 	;r   c                 H    | j                   | j                  | j                  fS rB   )r   r   r   rQ   s    r   __getnewargs__zSeekPoint.__getnewargs__  s!      $"2"2D4D4DDDr   c                     | d   S Nr   r   rQ   s    r   r&   zSeekPoint.<lambda>!  
    a r   c                     | d   S Nr   r   rQ   s    r   r&   zSeekPoint.<lambda>"  
    Q r   c                     | d   S )Nr   r   rQ   s    r   r&   zSeekPoint.<lambda>#  r   r   )r   r   r   rF   r   r   propertyr   r   r   __classcell__r   s   @r   r   r     s6    ";E 01L/0K/0Kr   r   c                   ~     e Zd ZdZdZ ej                  e      ZdZ fdZ	d Z
ej                  Zd Zd Zd Z xZS )		SeekTablezdRead and write FLAC seek tables.

    Attributes:
        seekpoints: list of SeekPoint objects
    z>QQHr   c                 :    g | _         t        t        |   |       y rB   )
seekpointsr   r   r<   r9   r)   r   s     r   r<   zSeekTable.__init__2  s    i'-r   c                 `    	 | j                   |j                   k(  S # t        t        f$ r Y yw xY wrt   )r   AttributeErrorrL   r|   s     r   r~   zSeekTable.__eq__6  s3    	OOu'7'778	* 		s    --c                 `   g | _         |j                  | j                        }t        |      | j                  k(  rt| j                   j	                  t        t        j                  | j                  |              |j                  | j                        }t        |      | j                  k(  rsy y rB   )	r   rD   _SeekTable__SEEKPOINT_SIZEr?   r\   r   rZ   unpack_SeekTable__SEEKPOINT_FORMAT)r9   r)   sps      r   rM   zSeekTable.load>  s    \\$//0"g...OO""9t66;$= >d334B "g...r   c                     t               }| j                  D ]S  }t        j                  | j                  |j
                  |j                  |j                        }|j                  |       U |j                         S rB   )
r   r   rZ   r[   r   r   r   r   r1   r   )r9   r   	seekpointpackeds       r   r1   zSeekTable.writeF  sh    I 	I[[''&&	(=(=%%'F GGFO	 zz|r   c                 N    dt        |       j                  d| j                  dS )N<z seekpoints=>)typer   r   rQ   s    r   __repr__zSeekTable.__repr__P  s    '+Dz':':DOOLLr   )r   r   r   rF   r   rZ   calcsizer   rV   r<   r~   rH   r   rM   r1   r   r   r   s   @r   r   r   &  sN      &v'9:D. %%H5Mr   r   c                   8     e Zd ZdZdZdZd fd	Zd fd	Z xZS )
VCFLACDictzVCFLACDict()

    Read and write FLAC Vorbis comments.

    FLACs don't use the framing bit at the end of the comment block.
    So this extends VCommentDict to not use the framing bit.
    r   Tc                 2    t         t        |   |||       y )N)errorsframing)r   r   rM   )r9   r)   r   r   r   s       r   rM   zVCFLACDict.load`  s    j$$T&'$Jr   c                 ,    t         t        |   |      S )N)r   )r   r   r1   )r9   r   r   s     r   r1   zVCFLACDict.writec  s    Z,W,==r   )replaceFro   )	r   r   r   rF   rV   rX   rM   r1   r   r   s   @r   r   r   T  s$     DNK> >r   r   c                   F     e Zd ZdZ fdZ ed       Z ed       Z xZS )CueSheetTrackIndexa  CueSheetTrackIndex(index_number, index_offset)

    Index for a track in a cuesheet.

    For CD-DA, an index_number of 0 corresponds to the track
    pre-gap. The first index in a track must have a number of 0 or 1,
    and subsequently, index_numbers must increase by 1. Index_numbers
    must be unique within a track. And index_offset must be evenly
    divisible by 588 samples.

    Attributes:
        index_number (`int`): index point number
        index_offset (`int`): offset in samples from track start
    c                 0    t         t        |   | ||f      S rB   )r   r   r   )r]   index_numberindex_offsetr   s      r   r   zCueSheetTrackIndex.__new__w  s"    '5,-/ 	/r   c                     | d   S r   r   rQ   s    r   r&   zCueSheetTrackIndex.<lambda>{  r   r   c                     | d   S r   r   rQ   s    r   r&   zCueSheetTrackIndex.<lambda>|  r   r   )	r   r   r   rF   r   r   r   r   r   r   s   @r   r   r   g  s%    / 01L01Lr   r   c                   @    e Zd ZdZ	 	 ddZd Zej                  Zd Zy)CueSheetTracka  CueSheetTrack()

    A track in a cuesheet.

    For CD-DA, track_numbers must be 1-99, or 170 for the
    lead-out. Track_numbers must be unique within a cue sheet. There
    must be atleast one index in every track except the lead-out track
    which must have none.

    Attributes:
        track_number (`int`): track number
        start_offset (`int`): track offset in samples from start of FLAC stream
        isrc (`mutagen.text`): ISRC code, exactly 12 characters
        type (`int`): 0 for audio, 1 for digital data
        pre_emphasis (`bool`): true if the track is recorded with pre-emphasis
        indexes (list[CueSheetTrackIndex]):
            list of CueSheetTrackIndex objects
    c                 X    || _         || _        || _        || _        || _        g | _        y rB   )track_numberstart_offsetisrcr   pre_emphasisindexes)r9   r   r   r   type_r   s         r   r<   zCueSheetTrack.__init__  s/    ((		(r   c                 n   	 | j                   |j                   k(  xr | j                  |j                  k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S # t        t        f$ r Y yw xY wrt   )r   r   r   r   r   r   r   rL   r|   s     r   r~   zCueSheetTrack.__eq__  s    	%%););; 2%%););;2II+2 II+2 %%););;	2
 LLEMM13 	* 		s   BB" "B43B4c                     dt        |       j                  | j                  | j                  | j                  | j                   | j
                  | j                  fz  S )NzI<%s number=%r, offset=%d, isrc=%r, type=%r, pre_emphasis=%r, indexes=%r)>)r   r   r   r   r   r   r   rQ   s    r   r   zCueSheetTrack.__repr__  sQ    0d$$d&7&79J9JDIIt'8'8$,,HH 	Ir   N) r   F)	r   r   r   rF   r<   r~   objectr   r   r   r   r   r   r     s,    & CD#	 HIr   r   c                        e Zd ZdZdZ ej                  e      ZdZ ej                  e      Z	dZ
 ej                  e
      ZdZdZdZdZ fd	Zd
 Zej&                  Zd Zd Zd Z xZS )CueSheeta  CueSheet()

    Read and write FLAC embedded cue sheets.

    Number of tracks should be from 1 to 100. There should always be
    exactly one lead-out track and that track must be the last track
    in the cue sheet.

    Attributes:
        media_catalog_number (`mutagen.text`): media catalog number in ASCII,
            up to 128 characters
        lead_in_samples (`int`): number of lead-in samples
        compact_disc (`bool`): true if the cuesheet corresponds to a
            compact disc
        tracks (list[CueSheetTrack]):
            list of CueSheetTrack objects
        lead_out (`CueSheetTrack` or `None`):
            lead-out as CueSheetTrack or None if lead-out was not found
    z>128sQB258xBz>QB12sB13xBz>QB3xr   r   iX Tc                 :    g | _         t        t        |   |       y rB   )tracksr   r   r<   r   s     r   r<   zCueSheet.__init__  s    h&t,r   c                    	 | j                   |j                   k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S # t        t
        f$ r Y yw xY wrt   )media_catalog_numberlead_in_samplescompact_discr   r   rL   r|   s     r   r~   zCueSheet.__eq__  s    	--1K1KK 0((E,A,AA0%%););;0 KK5<</1 	* 		s   A)A, ,A>=A>c                     |j                  | j                        }t        j                  | j                  |      \  }}}}|j                  d      | _        || _        t        |dz        | _	        g | _
        t        |      D ]  }|j                  | j                        }t        j                  | j                  |      \  }	}
}}}|j                  d      }|dz  dz	  }t        |dz        }t        |
|	|||      }t        |      D ]e  }|j                  | j                        }t        j                  | j                   |      \  }}|j"                  j%                  t'        ||             g | j                  j%                  |        y )N    rS   r   r   )r>   _CueSheet__CUESHEET_SIZErZ   r   _CueSheet__CUESHEET_FORMATrstripr   r   boolr   r   range_CueSheet__CUESHEET_TRACK_SIZE _CueSheet__CUESHEET_TRACK_FORMATr   #_CueSheet__CUESHEET_TRACKINDEX_SIZE%_CueSheet__CUESHEET_TRACKINDEX_FORMATr   r\   r   )r9   r)   headerr   r   flags
num_tracksitrackr   r   isrc_paddednum_indexesr   r   r   valjindexr   r   s                        r   rM   zCueSheet.load  ss   4//0MM$00&9 	Aouj$8$?$?$F!. .z" 	$AIId889Ed::EB HL,UK%%e,DT\a'E-LlD%GC;' D		$"A"AB-3]]55u.>*l""&|\BD	D KKs#	$r   c                    t               }d}| j                  r|dz  }t        j                  | j                  | j
                  | j                  |t        | j                              }|j                  |       | j                  D ]  }d}||j                  dz  dz  z  }|j                  r|dz  }t        j                  | j                  |j                  |j                  |j                  |t        |j                               }|j                  |       |j                   D ]H  }t        j                  | j"                  |j$                  |j&                        }|j                  |       J  |j)                         S )Nr   rS   r   r   r   )r   r   rZ   r[   r   r   r   r?   r   r1   r   r   r   r   r   r   r   r   r   r   r   )	r9   r   r   r   r   track_flagstrack_packedr  index_packeds	            r   r1   zCueSheet.write  sE   ITME""D$=$=  %T[[)9; 	
[[ 	&EKEJJNq00K!!t#!;;,,e.@.@""EJJEMM"$L GGL! &%{{55&&(:(: < %	&	& zz|r   c                     dt        |       j                  d| j                  d| j                  d| j                  d| j
                  dS )Nr   z media_catalog_number=z
, lead_in=z, compact_disc=z	, tracks=r   )r   r   r   r   r   r   rQ   s    r   r   zCueSheet.__repr__  s<     d$$d&?&?%%t'8'8$++G 	Hr   )r   r   r   rF   r   rZ   r   r   r   r   r   r   rV   r   r   r   r<   r~   rH   r   rM   r1   r   r   r   s   @r   r   r     s    ( '%foo&78O++FOO,CD#* !01M!NDOL- %%H$24Hr   r   c                   \     e Zd ZdZdZdZd	 fd	Zd Zej                  Z	d Z
d Zd Z xZS )
Picturea  Picture()

    Read and write FLAC embed pictures.

    .. currentmodule:: mutagen

    Attributes:
        type (`id3.PictureType`): picture type
            (same as types for ID3 APIC frames)
        mime (`text`): MIME type of the picture
        desc (`text`): picture's description
        width (`int`): width in pixels
        height (`int`): height in pixels
        depth (`int`): color depth in bits-per-pixel
        colors (`int`): number of colors for indexed palettes (like GIF),
            0 for non-indexed
        data (`bytes`): picture data

    To create a picture from file (in order to add to a FLAC file),
    instantiate this object without passing anything to the constructor and
    then set the properties manually::

        p = Picture()

        with open("Folder.jpg", "rb") as f:
            pic.data = f.read()

        pic.type = id3.PictureType.COVER_FRONT
        pic.mime = u"image/jpeg"
        pic.width = 500
        pic.height = 500
        pic.depth = 16 # color depth
       Tc                     d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        t        t        | +  |       y )Nr   r   r   )r   mimedescwidthheightdepthcolorsr)   r   r  r<   r   s     r   r<   zPicture.__init__A  sJ    			

	gt%d+r   c                    	 | j                   |j                   k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xrj | j                  |j                  k(  xrO | j
                  |j
                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S # t        t        f$ r Y yw xY wrt   )
r   r  r  r  r  r  r  r)   r   rL   r|   s     r   r~   zPicture.__eq__L  s    
	II+ ,II+,II+, JJ%++-, KK5<</	,
 JJ%++-, KK5<</, II+- 	* 		s   CC C*)C*c                    t        j                  d|j                  d            \  | _        }|j                  |      j	                  dd      | _        t        j                  d|j                  d            \  }|j                  |      j	                  dd      | _        t        j                  d|j                  d            \  | _        | _        | _	        | _
        }|j                  |      | _        y )	N>2Ir#   UTF-8r   rT   r   >5I   )rZ   r   r>   r   decoder  r  r  r  r  r  r)   )r9   r)   ra   s      r   rM   zPicture.load[  s    "MM%1>	6IIf%,,Wi@	--diil3IIf%,,Wi@	 &eTYYr] C	T[$*	fIIf%	r   c                    t               }| j                  j                  d      }|j                  t	        j
                  d| j                  t        |                   |j                  |       | j                  j                  d      }|j                  t	        j
                  dt        |                   |j                  |       |j                  t	        j
                  d| j                  | j                  | j                  | j                  t        | j                                     |j                  | j                         |j                         S )Nr  r  rT   r  )r   r  encoder1   rZ   r[   r   r?   r  r  r  r  r  r)   r   )r9   r   r  r  s       r   r1   zPicture.writed  s    Iyy(	E499c$i89	yy(	D#d),-		E4::t{{DJJ KKTYY9 	:			zz|r   c                 r    dt        |       j                  | j                  t        | j                        fz  S )Nz<%s '%s' (%d bytes)>)r   r   r  r?   r)   rQ   s    r   r   zPicture.__repr__q  s1    %d)<)<dii),TYY)9 9 	9r   rB   )r   r   r   rF   rV   rX   r<   r~   rH   r   rM   r1   r   r   r   s   @r   r  r    s;     D DN	, %%H&9r   r  c                   X     e Zd ZdZdZd fd	Zd Zd Zd Ze	j                  Z
d Z xZS )	rd   a2  Padding()

    Empty padding space for metadata blocks.

    To avoid rewriting the entire FLAC file when editing comments,
    metadata is often padded. Padding should occur at the end, and no
    more than one padding block should be in any FLAC file.

    Attributes:
        length (`int`): length
    r   c                 ,    t         t        |   |       y rB   )r   rd   r<   r   s     r   r<   zPadding.__init__  s    gt%d+r   c                 @    t        |j                               | _        y rB   )r?   r>   ra   rN   s     r   rM   zPadding.load  s    $))+&r   c                     	 d| j                   z  S # t        t        t        f$ r t	        d| j                   z        w xY w)Nr   zcannot write %d bytes)ra   OverflowError
ValueErrorMemoryErrorr   rQ   s    r   r1   zPadding.write  sC    		?T[[(( z;7 	?/$++=>>	?s    ->c                 X    t        |t              xr | j                  |j                  k(  S rB   )rJ   rd   ra   r|   s     r   r~   zPadding.__eq__  s!    %)IdkkU\\.IIr   c                 J    dt        |       j                  | j                  fz  S )Nz<%s (%d bytes)>)r   r   ra   rQ   s    r   r   zPadding.__repr__  s      DJ$7$7#EEEr   )r   )r   r   r   rF   rV   r<   rM   r1   r~   rH   r   r   r   r   s   @r   rd   rd   v  s7    
 D,'
?J %%HFr   rd   c                   F   e Zd ZdZg dZdZeedee	e
egZ	 ed        Zd Zd ZeZ eee       ed      dd	              Z ed
 d      Z eee       e       d               Zed        Zd Zd Zed        Z eee       ed      dd              Zd Zd Z d Z!y)r   aN  FLAC(filething)

    A FLAC audio file.

    Args:
        filething (filething)

    Attributes:
        cuesheet (`CueSheet`): if any or `None`
        seektable (`SeekTable`): if any or `None`
        pictures (list[Picture]): list of embedded pictures
        info (`StreamInfo`)
        tags (`mutagen._vorbis.VCommentDict`)
    )z
audio/flaczaudio/x-flaczapplication/x-flacNc                 `    |j                  d      t        | j                         d      dz  z   S )N   fLaCz.flacr   )
startswithr   lower)filenamer:   header_datas      r   scorez
FLAC.score  s0    &&w/)73a78 	9r   c                 `   t        |j                  d            }t        |j                  d            }|dz  }t        |dz        }	 | j                  |   xs t
        }|j                  rF|j                         } ||      }|j                         |z
  }	|	t
        j                  kD  r!||_
        n|j                  |      }
 ||
      }||_        |j                  t        j                  k(  r| j                  || _        nynx|j                  t        j                  k(  r| j                  || _        nGt!        d      |j                  t"        j                  k(  r| j$                  || _        nt!        d      | j&                  j)                  |       | S # t        $ r
 t
        }Y Lw xY w)Nr   r      rS   z> 1 CueSheet block foundz> 1 SeekTable block found)ordr>   r*   r   METADATA_BLOCKSrH   
IndexErrorrX   r/   rW   rY   rV   r   tagsr   cuesheetr   r   	seektablemetadata_blocksr\   )r9   r:   r   r@   rV   
last_block
block_typestartr^   	real_sizer)   s              r   __read_metadata_blockzFLAC.__read_metadata_block  sq   7<<?#a)d{$+&
	'--d3D}J $$ LLNEw'E.I=222/3,<<%Dt$E
::(yy !	 ZZ8==(}}$ %677ZZ9>>)~~%!&788##E*~S  	'&J	's   	F F-,F-c                     | j                   5t               | _         | j                  j                  | j                          yt	        d      )z'Add a Vorbis comment block to the file.Nza Vorbis comment already exists)r1  r   r4  r\   r    rQ   s    r   add_tagszFLAC.add_tags  s9    99"DI  ''		2!"CDDr   T)writablec                    | j                   | j                  D cg c]"  }|j                  t        j                  k7  s!|$ }}| j	                  ||dd        | j                  D cg c]/  }|j                  t        j                  k7  s|| j                   u r|1 c}| j                  dd | j                   j                          yyc c}w c c}w )ztRemove Vorbis comments from a file.

        If no filename is given, the one most recently loaded is used.
        NFc                      yr   r   )xs    r   r&   zFLAC.delete.<locals>.<lambda>  s    r   )padding)r1  r4  rV   r   _saveclear)r9   	filethingr%   temp_blockss       r   r   zFLAC.delete  s     99 //N166Z__3LNK NJJy+ukJJ//'@66Z__,TYY '@D  # IIOO !N'@s   "CC(4Cc                     | j                   S rB   )r1  )ss    r   r&   zFLAC.<lambda>  s
    AFF r   zAlias for tags; don't use this.)docc                    |j                   }g | _        d| _        d| _        d| _        t        |      }| j                  ||j                         | j                  |      r	 | j                  |      r	 | j                  j                   | j                  j                  rq|j                         }|j                  dd       t!        t#        |j                         |z
        dz  | j                  j                  z        | j                  _        yd| j                  _        y# t        t        f$ r t        d      w xY w)z&Load file information from a filename.NzStream info block not foundr   r   r#   )r:   r4  r1  r2  r3  r,   _FLAC__check_headerr2   _FLAC__read_metadata_blockrm   ra   r   r0  r   r/   r0   r   r   r   )r9   rC  r:   r7  s       r   rM   z	FLAC.load  s   
 ##!	"7+GY^^4((1 ((1	CII 99LLNELLA #gllnu,-1DII4D4DD!FDII !"DII 
+ 	C#$ABB	Cs   5D$ $D?c                     | j                   D cg c]!  }|j                  t        j                  k(  r|# }}|d   S c c}w r   )r4  rV   rr   )r9   r^   streaminfo_blockss      r   rm   z	FLAC.info+  sJ      $33
zzZ__, 
 
 !##	
s   &<c                 :    | j                   j                  |       y)zTAdd a new picture to the file.

        Args:
            picture (Picture)
        N)r4  r\   )r9   pictures     r   add_picturezFLAC.add_picture3  s     	##G,r   c                     | j                   D cg c]"  }|j                  t        j                  k7  s!|$ }}|| _         yc c}w )z"Delete all pictures from the file.Nr4  rV   r  )r9   r%   rg   s      r   clear_pictureszFLAC.clear_pictures;  s:     "11LQVVw||5K!LL% Ms
   "A A c                 |    | j                   D cg c]"  }|j                  t        j                  k(  s!|$ c}S c c}w rB   rQ  )r9   r%   s     r   pictureszFLAC.picturesA  s+    //Ja166W\\3IJJJs   "99c                 @    | j                  || j                  ||       y)a  Save metadata blocks to a file.

        Args:
            filething (filething)
            deleteid3 (bool): delete id3 tags while at it
            padding (:obj:`mutagen.PaddingFunction`)

        If no filename is given, the one most recently loaded is used.
        N)rA  r4  )r9   rC  	deleteid3r@  s       r   savez	FLAC.saveE  s     	

9d22IwGr   c                 x   t        |j                        }| j                  ||j                        }| j	                  |      }||z
  }|r|dkD  r
||dz
  z  }d}t        |      |z
  }	|	dk\  sJ t        j                  |||	|      }
t        |
      }t        |j                  |||       |j                  |dz
         |j                  d       |j                  |
       |rK	 |j                  dd       |j                  d      dk(  r#|j                  dd       |j                          y y y # t        $ r Y y w xY w)Nr   r   r&  ir   r   s   TAG)r,   r:   rI  r2   _FLAC__find_audio_offsetr   rH   rn   r?   r	   r0   r1   r>   r4   IOError)r9   rC  r4  rV  r@  r   r   audio_offsetrh   content_sizer)   	data_sizes               r   rA  z
FLAC._saveT  s6   Y../$$Q	7//2 6)	 !!#IF{\1q   ))Yg?I	Y&&	9fE	vz		 !tQ 66!9&FF4OJJL '   s   "D- -	D98D9c                 <   d}|dz  sut        |j                  d            }t        |j                  d            }	 | j                  |dz     }|r|j
                  r	 ||       n|j                  |       |dz  su|j                         S # t        $ r d }Y Kw xY w)Nr   rS   r   r   r-  )r.  r>   r*   r/  r0  rX   r/   )r9   r:   r   r@   r6  s        r   __find_audio_offsetzFLAC.__find_audio_offsetv  s    $;w||A'DW\\!_-D"!11$+>
 j77 7#T" $; ||~  "!
"s   B BBc                     d}|j                  d      }|dk7  rTd}|dd dk(  rJdt        |j                  d      dd       z   }|j                  |dz
         |j                  d      dk7  rd}|t        d	|z        |S )
zReturns the offset of the flac block start
        (skipping id3 tags if found). The passed fileobj will be advanced to
        that offset as well.
        r   r&  Nr   s   ID3   r	  r   z%r is not a valid FLAC file)r>   r   r0   r   )r9   r:   r2   r@   r   s        r   __check_headerzFLAC.__check_header  s     aWDbqzV#La)<==TAX&<<?g-D<#-46 6r   rB   )NFN)"r   r   r   rF   _mimesr1  rr   rd   r   r   r   r  r/  staticmethodr+  rJ  r;  add_vorbiscommentr   rZ  r   r   r   r   vcrM   rm   rO  rR  rT  rW  rA  rY  rI  r   r   r   r   r     s    BFD!7D)Z*O49 91fE !7E"t  # 
"(I	JB7E"Z"  #"8 $ $-& K K 7E"tH  #H !D$r   r   FT)methodr<  c                 r    t        |       }| j                  j                  d       |j                  |        y)zoRemove tags from a file.

    Args:
        filething (filething)
    Raises:
        mutagen.MutagenError
    r   N)r   r:   r0   r   )rC  r   s     r   r   r     s-     	YA1HHYr   )-rF   __all__rZ   ior   _vorbisr   mutagenmutagen._utilr	   r
   r   r   r   r   r   mutagen._tagsr   mutagen.id3._utilr   	functoolsr   r   r   r   r    r*   r   r,   rH   rr   tupler   r   r   r   r   r   r  rd   FileTyper   r   rZ  r   r   r   r   <module>rs     sL   %   ! " " " % * 	L 		 		j% 	A)v ),[F [|[N 2 2 [N|1 1<+M +M\> >&2 20.IF .IbhH} hHVX9m X9v'Fm 'FTz7 zz  w	& ' r   