³ò
OCÖIc           @   sÕ   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z e	 d ƒ \ Z
 Z Z Z Z d a d d d „  ƒ  YZ d d d „  ƒ  YZ d „  Z d	 „  Z d
 „  Z d „  Z d S(   s9   archive.py - Archive handling (extract/create) for Comix.iÿÿÿÿNi   t	   Extractorc           B   sq   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   sÂ  Extractor is a threaded class for extracting different archive formats.

    The Extractor can be loaded with paths to archives (currently ZIP, tar,
    or RAR archives) and a path to a destination directory. Once an archive
    has been set it is possible to filter out the files to be extracted and
    set the order in which they should be extracted. The extraction can
    then be started in a new thread in which files are extracted one by one,
    and a signal is sent on a condition after each extraction, so that it is
    possible for other threads to wait on specific files to be ready.

    Note: Support for gzip/bzip2 compressed tar archives is limited, see
    set_files() for more info.
    c         C   s   t  |  _ d  S(   N(   t   Falset	   _setupped(   t   self(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   __init__#   s    c         C   s÷  | |  _  | |  _ t | ƒ |  _ g  |  _ h  |  _ t |  _ d	 |  _	 t
 i ƒ  |  _ |  i t j o+ t i | d ƒ |  _ |  i i ƒ  |  _ nY|  i t t t f j o+ t i | d ƒ |  _ |  i i ƒ  |  _ n|  i t j o÷ t d	 j ou t ƒ  a t d	 j o[ d GHt i d	 d t i t i t  d ƒ ƒ } | i! t  d ƒ ƒ | i" ƒ  | i# ƒ  d	 Sqen t$ i% t d d | g ƒ } | i& ƒ  } g  } | i' ƒ  D] } | | i( t) i* ƒ q~ |  _ | i+ ƒ  | i, ƒ  n d G| GHd	 St- |  _. |  i S(
   sÉ   Setup the extractor with archive <src> and destination dir <dst>.
        Return a threading.Condition related to the is_ready() method, or
        None if the format of <src> isn't supported.
        t   rs$   ! Could not find RAR file extractor.i    s"   Could not find RAR file extractor!sg   You need either the <i>rar</i> or the <i>unrar</i> program installed in order to read RAR (.cbr) files.t   vbs   --s   ! Non-supported archive format:N(/   t   _srct   _dstt   archive_mime_typet   _typet   _filest
   _extractedR   t   _stopt   Nonet   _extract_threadt	   threadingt	   Conditiont
   _conditiont   ZIPt   zipfilet   ZipFilet   _zfilet   namelistt   TARt   GZIPt   BZIP2t   tarfilet   opent   _tfilet   getnamest   RARt	   _rar_exect   _get_rar_exect   gtkt   MessageDialogt   MESSAGE_WARNINGt   BUTTONS_CLOSEt   _t   format_secondary_markupt   runt   destroyt   processt   Processt   spawnt	   readlinest   rstript   ost   linesept   closet   waitt   TrueR   (   R   t   srct   dstt   dialogt   proct   fdt   _[1]t   name(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   setup&   sH    							

6
		c         C   s   |  i  S(   s,  Return a list of names of all the files the extractor is currently
        set for extracting. After a call to setup() this is by default all
        files found in the archive. The paths in the list are relative to
        the archive root and are not absolute for the files once extracted.
        (   R   (   R   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt	   get_filesT   s    c         C   s_   |  i  t t f j o< g  } |  i D] } | | j o | | q$ q$ ~ |  _ n
 | |  _ d S(   sy  Set the files that the extractor should extract from the archive in
        the order of extraction. Normally one would get the list of all files
        in the archive using get_files(), then filter and/or permute this
        list before sending it back using set_files().

        Note: Random access on gzip or bzip2 compressed tar archives is
        no good idea. These formats are supported *only* for backwards
        compability. They are fine formats for some purposes, but should
        not be used for scanned comic books. So, we cheat and ignore the
        ordering applied with this method on such archives.
        N(   R
   R   R   R   (   R   t   filesR9   t   x(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt	   set_files\   s    <c         C   s   |  i  i | t ƒ S(   sx   Return True if the file <name> in the extractor's file list
        (as set by set_files()) is fully extracted.
        (   R   t   getR   (   R   R:   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   is_readym   s    c         C   s   |  i  S(   s=   Return the mime type name of the extractor's current archive.(   R
   (   R   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   get_mime_types   s    c         C   s1   t  |  _ |  i o |  i i ƒ  t |  _ n d S(   s‹   Signal the extractor to stop extracting and kill the extracting
        thread. Blocks until the extracting thread has terminated.
        N(   R3   R   R   R   t   joinR   t   setupped(   R   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   stopw   s    	
c         C   s9   t  i d |  i ƒ |  _ |  i i t ƒ |  i i ƒ  d S(   sÒ   Start extracting the files in the file list one by one using a
        new thread. Every time a new file is extracted a notify() will be
        signalled on the Condition that was returned by setup().
        t   targetN(   R   t   Threadt   _thread_extractR   t	   setDaemonR   t   start(   R   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   extract€   s    c         C   sO   |  i  t j o |  i i ƒ  n+ |  i  t t t f j o |  i i ƒ  n d S(   sp   Close any open file objects, need only be called manually if the
        extract() method isn't called.
        N(   R
   R   R   R1   R   R   R   R   (   R   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyR1   ‰   s    c         C   s/   x |  i  D] } |  i | ƒ q
 W|  i ƒ  d S(   s.   Extract the files in the file list one by one.N(   R   t   _extract_fileR1   (   R   R:   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyRH   ’   s    
 c         C   s÷  |  i  o |  i ƒ  t i d ƒ n y„|  i t j oŠ t i i |  i	 | ƒ } t i i
 t i i | ƒ ƒ p t i t i i | ƒ ƒ n t | d ƒ } | i |  i i | ƒ ƒ | i ƒ  nç |  i t t t f j o\ t i i t i i |  i	 | ƒ ƒ i |  i	 ƒ o |  i i | |  i	 ƒ q¨d G| Gd GHnr |  i t j oa t d j	 oK t i t d d d d d	 d
 |  i | |  i	 g
 ƒ } | i ƒ  | i ƒ  q¨d GHn Wn t  j
 o n X|  i! i" ƒ  t# |  i$ | <|  i! i% ƒ  |  i! i& ƒ  d S(   s«   Extract the file named <name> to the destination directory,
        mark the file as "ready", then signal a notify() on the Condition
        returned by setup().
        i    t   wbs   ! Non-local tar member:s   
R>   s   -kbs   -p-s   -o-s   -inuls   --s$   ! Could not find RAR file extractor.N('   R   R1   t   syst   exitR
   R   R/   t   pathRC   R   t   existst   dirnamet   makedirsR   t   writeR   t   readR   R   R   t   normpatht
   startswithR   RK   R   R    R   R*   R+   R   R,   R2   t	   ExceptionR   t   acquireR3   R   t   notifyt   release(   R   R:   t   dst_patht   newR7   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyRL   ˜   s:    

$!
(   t   __name__t
   __module__t   __doc__R   R;   R<   R?   RA   RB   RE   RK   R1   RH   RL   (    (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyR       s   		.											t   Packerc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s
  Packer is a threaded class for packing files into ZIP archives.
    
    It would be straight-forward to add support for more archive types,
    but basically all other types are less well fitted for this particular
    task than ZIP archives are (yes, really).
    c         C   s:   | |  _  | |  _ | |  _ | |  _ d |  _ t |  _ d S(   sm  Setup a Packer object to create a ZIP archive at <archive_path>.
        All files pointed to by paths in the sequences <image_files> and
        <other_files> will be included in the archive when packed.
        
        The files in <image_files> will be renamed on the form
        "NN - <base_name>.ext", so that the lexical ordering of their
        filenames match that of their order in the list.
        
        The files in <other_files> will be included as they are,
        assuming their filenames does not clash with other filenames in
        the archive. All files are placed in the archive root.
        N(   t   _image_filest   _other_filest   _archive_patht
   _base_nameR   t   _pack_threadR   t   _packing_successful(   R   t   image_filest   other_filest   archive_patht	   base_name(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyR   Ë   s    					c         C   s9   t  i d |  i ƒ |  _ |  i i t ƒ |  i i ƒ  d S(   s6   Pack all the files in the file lists into the archive.RF   N(   R   RG   t   _thread_packRf   RI   R   RJ   (   R   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   packß   s    c         C   s(   |  i  d j o |  i  i ƒ  n |  i S(   sv   Block until the packer thread has finished. Return True if the
        packer finished its work successfully.
        N(   Rf   R   RC   Rg   (   R   (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyR2   å   s    c         C   s  y t  i |  i d ƒ } Wn# t j
 o d G|  i GHd  Sn Xg  } d t t t |  i ƒ ƒ ƒ |  i f } x¼ t |  i ƒ D]« \ } } | | d t	 i
 i | ƒ d f } y | i | | t  i ƒ WnQ t j
 oE d | |  i f GH| i ƒ  y t	 i |  i ƒ Wn n Xd  Sn X| i | ƒ q} Wx½ |  i D]² } t	 i
 i | ƒ } x | | j o d | } qQWy | i | | t  i ƒ WnQ t j
 oE d | |  i f GH| i ƒ  y t	 i |  i ƒ Wn n Xd  Sn X| i | ƒ q6W| i ƒ  t |  _ d  S(   Nt   ws   ! Could not create archives   %%0%dd - %s%%si   s1   ! Could not add file %s to add to %s, aborting...s   _%s(   R   R   Rd   RX   t   lent   strRb   Re   t	   enumerateR/   RP   t   splitextRT   t
   ZIP_STOREDR1   t   removet   appendRc   t   basenamet   ZIP_DEFLATEDR3   Rg   (   R   t   zfilet
   used_namest   patternt   iRP   t   filename(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyRl   í   sV    	 $
	
  
	
(   R^   R_   R`   R   Rm   R2   Rl   (    (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyRa   Â   s
   			c         C   s  yï t  i i |  ƒ oØ t  i |  t  i ƒ p d Sn t i |  ƒ o t Sn t	 |  d ƒ } | i
 d ƒ } | i ƒ  t i |  ƒ oQ t  i i |  ƒ d j o8 | i d ƒ o t Sn | i d ƒ o t Sn t Sn | d j o t Sqî n Wn t j
 o d G|  GHn Xd S(	   s;   Return the archive type of <path> or None for non-archives.t   rbi   i    t   BZhs   ‹s   Rar!s   ! Error while readingN(   R/   RP   t   isfilet   accesst   R_OKR   R   t
   is_zipfileR   R   RU   R1   R   t
   is_tarfilet   getsizeRW   R   R   R   R   RX   (   RP   R8   t   magic(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyR	     s(    
)c         C   sS   h  t  d ƒ t <t  d ƒ t <t  d ƒ t <t  d ƒ t <t  d ƒ t <|  S(   s0   Return a text representation of an archive type.s   ZIP archives   Tar archives   Gzip compressed tar archives   Bzip2 compressed tar archives   RAR archive(   R&   R   R   R   R   R   (   t   archive_type(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   get_name0  s
    c         C   sœ   t  i d t  i ƒ } t ƒ  } | i |  d ƒ | i ƒ  } | d j o d Sn | i ƒ  } | i ƒ  t	 t
 | i | ƒ ƒ } t i |  ƒ i } | | | f S(   sŽ   Return a tuple (mime, num_pages, size) with info about the archive
    at <path>, or None if <path> doesn't point to a supported archive.
    s!   \.(jpg|jpeg|png|gif|tif|tiff)\s*$N(   t   ret   compilet   IR    R;   R   RB   R<   R1   Ro   t   filtert   searchR/   t   statt   st_size(   RP   t   image_ret	   extractort   mimeR=   t	   num_pagest   size(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyt   get_archive_info9  s    	
c          C   s<   x5 d D]- }  t  i |  g ƒ i ƒ  d j	 o |  Sq q Wd S(   sf   Return the name of the RAR file extractor executable, or None if
    no such executable is found.
    t   unrart   rar(   R•   R–   N(   R*   R+   R,   R   (   t   command(    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pyR!   J  s
     (    (    (   R`   RN   R/   Rˆ   R   R   R   R"   R*   t   rangeR   R   R   R   R   R   R    R    Ra   R	   R‡   R”   R!   (    (    (    sG   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/archive.pys   <module>   s    ¯V				