
    Z'i                         d dl Z d dlZd dlmZ d dlmZ d dlm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 d d
lmZ d dlmZ  G d d      Zd Zd Zd Zy)    N)sax)BytesIO)Mimetype)list_zip_file_infos)EPub)FB2FB2Zip)FB2sax)Dummy)
Mobipocket)configc                   ,    e Zd Zed        Zed        Zy)mime_detectorc                    | j                         dk(  rt        j                  S | j                         dk(  rt        j                  S | j                         dk(  rt        j                  S | j                         dk(  rt        j
                  S | j                         dk(  rt        j                  S | j                         dk(  rt        j                  S | j                         dk(  s| j                         dk(  rt        j                  S | j                         d	k(  rt        j                  S | j                         d
k(  rt        j                  S | j                         dk(  rt        j                  S t        j                  S )Nxmlfb2epubmobizippdfdocdocxdjvutxtrtf)lowerr   XMLr   EPUBMOBIZIPPDFMSWORDDJVUTEXTRTFOCTET_STREAM)fmts    (/opt/sopds/book_tools/format/__init__.pyr'   zmime_detector.fmt   s   99;%<<YY[E!<<YY[6!== YY[6!== YY[E!<<YY[5 <<YY[5 CIIK$7??"YY[6!== YY[5 == YY[5 <<(((    c                 v    t         j                  j                  |       \  }}t        j	                  |dd        S )N   )ospathsplitextr   r'   )filenamenes      r(   filezmime_detector.file,   s1    !!(+A  12''r)   N)__name__
__module____qualname__staticmethodr'   r2    r)   r(   r   r      s(    ) )0 ( (r)   r   c                    d}t         j                  |      }	 |t        j                  k(  r|t	        |       k(  rnt        j
                  S |t        j                  k(  rt        j                  |       5 }|j                         st        |      }t        |      dk(  r9|t	        |j                  |d               k(  rt        j                  cd d d        S 	 |j                  d      5 }|j                  d      j                         j!                  d      t        j"                  k(  r"t        j"                  cd d d        cd d d        S 	 d d d        d d d        |S |t        j&                  k(  r9| j                  d      }|dd  }|j                         d	k(  rt        j(                  S |S # 1 sw Y   axY w# t$        $ r
}Y d }~rd }~ww xY w# 1 sw Y   |S xY w#  Y |S xY w)
NFictionBookr+   r   mimetype   z
D   <   BOOKMOBI)r   r2   r   r   __xml_root_tagr   r    zipfileZipFiletestzipr   lenopenFB2_ZIPreaddecoderstripr   	Exceptionr&   r   )	r2   original_filenameFB2_ROOTmimezip_fileinfolistmimetype_filer1   mobiflags	            r(   detect_mimerQ   1   s   H/0D8<<>$//||#X\\!& ('')28<H8})#~hmmHQK6P'QQ#+#3#3 %]]:6 5-,11"5<<>EEfMQYQ^Q^^'/}}5 5   _5( K X***		"H}H J.}}$ K5 5 % ( KKs   1G (G 3AG	G F7(AF+7	F7 	G F7G AG +F4	0F77	G
 GG

GGG G Gc                    t        | t              rt        | d      } t        | j	                               } t        | |      }|t        j                  k(  rt        | |      S |t        j                  k(  r(t        j                  rt        | |      S t        | |      S |t        j                  k(  rt        | |      S |t        j                  k(  rt!        | |      S |t        j"                  t        j$                  t        j&                  t        j(                  t        j*                  fv rt-        | ||      S t/        d|z        )Nrbz&File type '%s' is not supported, sorry)
isinstancestrrD   r   rF   rQ   r   r   r   r   r   SOPDS_FB2SAXr
   rE   r	   r   r   r$   r!   r"   r%   r#   r   rI   )r2   rJ   r:   s      r(   create_bookfilerW   P   s    $D$499;D4 12H8== D+,,	X\\	!282E2Evd-.g3tUfKgg	X%%	%d-..	X]]	"$ 122	hmmX\\8??HLLRZR_R_`	`D+X66BXMNNr)   c                      G d dt                G fddt        j                  j                        }	 t        j                  |  |              y # $ r}|j
                  cY d }~S d }~ww xY w)Nc                       e Zd Zd Zy)$__xml_root_tag.<locals>.XMLRootFoundc                     || _         y N)name)selfr]   s     r(   __init__z-__xml_root_tag.<locals>.XMLRootFound.__init__d   s	    DIr)   N)r3   r4   r5   r_   r7   r)   r(   XMLRootFoundrZ   c   s    	r)   r`   c                       e Zd Z fdZy)%__xml_root_tag.<locals>.RootTagFinderc                      |      r\   r7   )r^   r]   
attributesr`   s      r(   startElementz2__xml_root_tag.<locals>.RootTagFinder.startElementh   s    t$$r)   N)r3   r4   r5   re   )r`   s   r(   RootTagFinderrb   g   s    	%r)   rf   )rI   r   handlerContentHandlerparser]   )r2   rf   r1   r`   s      @r(   r?   r?   b   sZ    y %22 %		$(   vvs   A A.A)#A.)A.)r,   r@   r   r   ior   book_tools.format.mimetyper   book_tools.format.utilr   book_tools.format.epubr   book_tools.format.fb2r   r	   book_tools.format.fb2saxr
   book_tools.format.otherr   book_tools.format.mobir   	constancer   r   rQ   rW   r?   r7   r)   r(   <module>rs      sC    	    / 6 ' - + ) - ( (>>O$r)   