ó
÷PïPc           @   sà   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l m Z d  d l
 m Z m Z m Z m Z d  d l m Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   Node(   t   printTracebackt   trimt	   xmlStringt   getYesNo(   t   Imaget   Picturec           B   s©  e  Z d- d- d  „ 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 d „  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 d „  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) d- d' „ Z* d- d( „ Z+ d) „  Z, d* „  Z- d+ „  Z. d, „  Z/ RS(.   c         C   s  t  j |  ƒ | |  _ d |  _ d |  _ d |  _ d |  _ d  |  _ d |  _	 d |  _
 d |  _ d |  _ d |  _ d |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d |  _ | r| d |  _ | d |  _ d  |  _ | d |  _ n  |  j ƒ  d  S(   Ni    t    t   sizet	   thumbsizet   faster(   R    t   __init__t   absfilenamet   widtht   heightt	   res_widtht   thumb_widtht   Nonet   formatt
   res_heightt   thumb_heightt   datet   wheret   whot   whatt   exift   imt   modet   infot   camerat   highQualityt   getImage(   t   selft   propsR   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyR      s8    																						c         C   sc   d |  j  d d t |  j ƒ d d t |  j ƒ d d t |  j ƒ d d t |  j ƒ d S(   Ns
   Filename: s   
s   Width:s   Height:s   Resized width:s   Resized height:(   R   t   strR   R   R   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   tostring5   s    c         C   s   |  j  S(   N(   t   absFilename(   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getAbsfilename8   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getWidth;   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getResizedWidth>   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getThumbWidthA   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt	   getHeightD   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getResizedHeightG   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getThumbHeightJ   s    c         C   s#   |  j  d k r |  j ƒ  n  |  j  S(   s6   Return date, if empty get it from EXIF data in pictureR   (   R   t   getExifDate(   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getDateM   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getWhoS   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getWhereV   s    c         C   s   |  j  S(   N(   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getWhatY   s    c         C   s   | |  _  d  S(   N(   R   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setAbsfilename\   s    c         C   s+   | d  k r d |  _ n t | ƒ |  _ d  S(   Ni    (   R   R   t   int(   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setWidth_   s    c         C   s+   | d  k r d |  _ n t | ƒ |  _ d  S(   Ni    (   R   R   R2   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setResizedWidthe   s    c         C   s+   | d  k r d |  _ n t | ƒ |  _ d  S(   Ni    (   R   R   R2   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setThumbWidthk   s    c         C   s+   | d  k r d |  _ n t | ƒ |  _ d  S(   Ni    (   R   R   R2   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt	   setHeightq   s    c         C   s+   | d  k r d |  _ n t | ƒ |  _ d  S(   Ni    (   R   R   R2   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setResizedHeightw   s    c         C   s+   | d  k r d |  _ n t | ƒ |  _ d  S(   Ni    (   R   R   R2   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setThumbHeight}   s    c         C   s   | |  _  d  S(   N(   R   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setDateƒ   s    c         C   s   | |  _  d  S(   N(   R   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setWho†   s    c         C   s   | |  _  d  S(   N(   R   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setWhere‰   s    c         C   s   | |  _  d  S(   N(   R   (   R    R   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   setWhatŒ   s    c         C   s   d |  _ d S(   s   Unload picture from memoryN(   R   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   unload   s    c         C   s2   y# |  j  d j d d d ƒ |  _ Wn d SXd S(   s    Extract timestamp from EXIF datai2  t   :t   -i   N(   R   t   replaceR   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyR,   “   s    #c   
   	   C   sM  y9|  j  d } |  j  d } | j ƒ  d | k r< | } n | d | } |  j  d } |  j  d } |  j  d } t |  j  d ƒ } |  j  d	 } d
 d d d d d d d d g	 }	 | t t |	 ƒ ƒ k r× |	 | } n t | ƒ } | d | d | d t | d ƒ | d f } | d | | d | d | f } Wn d } n X| S(   s   Extract camera from EXIF datai  i  i    t    iš‚  i‚  i
’  i	’  i"ˆ  s   undefined (0)t   manualt   normals   aperture prioritys   shutter prioritys
   max. deptht   speedt   portraitt	   landscapes    <BR>Exposure: 1/%d sec. F/%.1fi   sP    &nbsp;&nbsp;flash: %s &nbsp;&nbsp;focus %d mm. &nbsp;&nbsp;Exposure program: %st   unknown(   R   t   splitR   t   ranget   lenR"   t   float(
   R    t   maket   modelt   resultt   tt   dt   focust   flasht   ept   eps(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getExifCameraš   s(    	!2'
c         C   s6   |  j  d } i d d 6d d 6d d 6} | | } | S(   Ni  i´   i   i  i   iZ   i   (   R   (   R    t   orientationt   rotationOffsetst   rotationDegrees(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getExifOrientation´   s    
c         C   s   |  j  |  j f S(   N(   R   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getSizeº   s    c         C   s5  y|  j  r|  j d	 k rt j t |  j  d ƒ ƒ |  _ t |  j j d ƒ |  _ t |  j j d ƒ |  _ |  j j	 |  _	 |  j j
 |  _
 |  j	 d k rç |  j rÄ d |  j j d <d |  j j d <n d |  j j d <d |  j j d <n  |  j j |  _ y |  j j ƒ  |  _ WqqXqn  Wn t ƒ  n X|  j S(
   s<   Return a PIL Image object of picture, set properties on selft   rbi    i   t   JPEGid   t   qualityt   optimizet   progressionN(   R   R   R   R   t   openR2   R   R   R   R   R   R   R   t   _getexifR   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyR   ½   s,    		c         C   s˜   d } d } yw |  j  |  j k rN |  j  d | } t |  j | ƒ } | } n* |  j d | } | } t |  j  | ƒ } | | f SWn t ƒ  n Xd S(   s–   Return tuple describing (width,height) resized picture given a
           requested size. The requested size is the size of the
           longer edgei    g      ð?N(   R   R   R2   R   (   R    t   requestedSizet   _heightt   _widtht   ratio(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   calculateSizeÞ   s    	c         C   si   |  j  ƒ  } |  j ƒ  } | d k r6 | j | ƒ } n  |  j rR | j | t j ƒ S| j | t j ƒ Sd S(   s„   Return a PIL Image instance, where size is
           given in supplied tuple (width, heigth) 
           Also check for orientationi    N(   R   RY   t   rotateR   t   resizeR   t	   ANTIALIASt   NEAREST(   R    t	   sizeTupleR   t   offsetDegree(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getResizedPictureô   s    	c         C   s_   yJ |  j  | ƒ } |  j |  j ƒ } |  j | ƒ j t | d ƒ |  j ƒ Wn t ƒ  n Xd S(   s8   Put myself in destdirectory and resize to self.res_widtht   wbN(   t   getLocationRf   R   Rm   t   saveR`   R   R   (   R    t   destdirectoryt   locationRk   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt
   putResized  s    )c         C   s_   yJ |  j  | ƒ } |  j |  j ƒ } |  j | ƒ j t | d ƒ |  j ƒ Wn t ƒ  n Xd S(   s:   Put myself in destdirectory and resize to self.thumb_widthRn   N(   Ro   Rf   R   Rm   Rp   R`   R   R   (   R    Rq   Rr   Rk   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   putThumbnail  s    )c         C   sG   |  j  | ƒ } |  j | ƒ } |  j | ƒ j t | d ƒ |  j ƒ d S(   sZ   Put myself resized to size in descdirectory. Size is the length of the
        longer sideRn   N(   Ro   Rf   Rm   Rp   R`   R   (   R    Rq   R   Rr   Rk   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   putInDir  s    c         C   s&   |  j  | ƒ } t j |  j | ƒ d S(   s&   Put self in destdirectory, no resizingN(   Ro   t   shutilt   copyfileR   (   R    Rq   Rr   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   putFullsize  s    c         C   s&   t  j j | t  j j |  j ƒ d ƒ S(   sJ   Construct directory location from given destinationDir and sourcefile taili   (   t   ost   patht   joinRH   R   (   R    t   destinationDir(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyRo   #  s    c         C   s)   t  j j |  j ƒ } | d | j d ƒ !S(   s&   Return filename stripped for extensioni    t   .(   Ry   Rz   t   basenameR   t   rfind(   R    t   filename(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getNameNoExtension)  s    c         C   s   t  j j |  j ƒ d S(   s&   Return filename stripped for extensioni   (   Ry   Rz   RH   R   (   R    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getTail.  s    c         C   s  |  j  |  j ƒ \ } } g  } | j d t j |  j ƒ  ƒ d d ƒ | j d t j |  j ƒ  ƒ d t | ƒ d t | ƒ d |  j ƒ  d ƒ | j d	 ƒ | rê | j d
 ƒ | j d t j |  j ƒ  j ƒ  ƒ ƒ | j d ƒ n  | j d ƒ d j	 | ƒ S(   s/   Return HTML for inclusion in gallery index files   
     <td><a href="s   .html"t   >s   <img src="thumbnails/s
   " height="s	   " width="s   " alt="s   "s   ></a>s   <br><a href="s   fullsize/%ss   ">Full size</a>s   </td>R   (
   Rf   R   t   appendt	   functionst   quoteUrlR   R‚   R"   t   stripR{   (   R    t   fullsizet   xthumb_widtht   xthumb_heightt   html(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getLinkHTML3  s    (N&c         C   s†   g  } | j  d t j |  j ƒ  ƒ d d ƒ | j  d t j |  j ƒ  ƒ d ƒ | j  d ƒ | j  d ƒ | j  d ƒ d	 j | ƒ S(
   sS   If a directory has only a single picture,
           resize it to 640 and center itsH   <td id="sidelinks"><div></div></td>

<td width="100 %"><center><a href="s   .html"Rƒ   s   <img src="indexpic/s   "s   </a>s   </center></td>

s	   <td></td>R   (   R„   R…   R†   R   R‚   R{   (   R    Rˆ   R‹   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getLinkHTMLForSinglePicB  s    ($c         C   s†  g  } | j  d |  j ƒ  ƒ | j  d | d ƒ | j  d ƒ | j  d ƒ | j  |  j | ƒ ƒ | j  d t j |  j ƒ  ƒ d ƒ |  j |  j k rÈ | j  d ƒ | j  |  j ƒ  ƒ | j  d	 ƒ n- | j  d
 ƒ | j  |  j ƒ  ƒ | j  d ƒ | j  d ƒ | j  |  j | ƒ ƒ | j  | j ƒ  ƒ t	 j
 j | |  j ƒ  ƒ d } t | d ƒ } | j d j | ƒ j d ƒ ƒ | j ƒ  d S(   s+   Create HTML file to display resized pictures"   <html>
  <head>
    <title>
      s?   
    </title>
    <link rel="stylesheet" type="text/css" href="s   ">
sC   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">s   </head>
  <body>
sL   
     <hr>
     <table>
     <tr>
        <td>
           <img src="resized/s   ">
        </td>s<   
       <td width=30>
       </td>
       <td  valign="top">s   
        </td>sb   
      </tr>
       <tr>
        <td height=10>
        </td>
      </tr>
      <tr>
         <td>s   
         </td>s$   
       </tr>
     </table>
    <hr>s   .htmlt   wR   s   utf-8N(   R„   R‚   t   navigationBarR…   R†   R   R   t   tagLabelContentt   getHTMLRy   Rz   R{   R   R`   t   writet   encodet   close(   R    t	   directoryt
   stylesheetRˆ   t   footerR‹   t   rawt   file(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   createPictureHTMLO  s,    c         C   s¿   g  } |  j  d k r? | j d t j |  j  j ƒ  ƒ d ƒ n  |  j d k rx | j d t j |  j j ƒ  ƒ d ƒ n  | r¥ | j d t j |  j ƒ  ƒ d ƒ n  | j d ƒ t j	 | ƒ S(	   s6   Return navigation bar, only simple links at the moments   
     <p>
      [<a href="s   .html">Next</a>]s   
      [<a href="s   .html">Previous</a>]s   
      [<a href="fullsize/s   ">Fullsize</a>]s4   
      [<a href="index.html">Gallery</a>]
     </p>
N(
   t   nextR   R„   R…   R†   R   t   previousR‚   t   stringt
   joinfields(   R    Rˆ   R‹   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyR   q  s    **'c         C   s­   g  } | j  d |  j ƒ  ƒ |  j r= | j  d |  j ƒ n  |  j r] | j  d |  j ƒ n  |  j r} | j  d |  j ƒ n  |  j r  | j  d |  j ƒ  ƒ n  t j | ƒ S(   s   Return picture descriptionss(   
          <B class=tag_label>Date:</B> s+   <BR>
          <B class=tag_label>Who:</B> s-   <BR>
          <B class=tag_label>Where:</B> s,   <BR>
          <B class=tag_label>What:</B> s.   <BR>
          <B class=tag_label>Camera:</B> (	   R„   R-   R   R   R   R   RU   R   Rž   (   R    R‹   (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyR     s    				c         C   sŒ  | j  d ƒ | j  d t |  j ƒ  ƒ d ƒ | j  d t |  j ƒ d ƒ | j  d t |  j ƒ d ƒ | j  d t |  j ƒ d	 ƒ | j  d
 t |  j ƒ d ƒ | j  d t |  j ƒ d ƒ | j  d t |  j	 ƒ d ƒ | j  d t |  j
 ƒ d ƒ | j  d t |  j ƒ d ƒ | j  d t |  j ƒ d ƒ | j  d t |  j ƒ d ƒ | j  d t |  j ƒ  ƒ d ƒ | j  d ƒ | S(   s    Return XML code for this pictures   
            <picture>s   
                <filename>s   </filename>s   
                <width>s   </width>s    
                <resized_width>s   </resized_width>s   
                <thumb_width>s   </thumb_width>s   
                <height>s	   </height>s!   
                <resized_height>s   </resized_height>s   
                <thumb_height>s   </thumb_height>s   
                <date>s   </date>s   
                <who>s   </who>s   
                <where>s   </where>s   
                <what>s   </what>s   
                <camera>s	   </camera>s   
            </picture>(   R„   R   R‚   R"   R   R   R   R   R   R   R   R   R   R   RU   (   R    t   xml(    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   getXML  s    !!N(0   t   __name__t
   __module__R   R   R#   R%   R&   R'   R(   R)   R*   R+   R-   R.   R/   R0   R1   R3   R4   R5   R6   R7   R8   R9   R:   R;   R<   R=   R,   RU   RY   RZ   R   Rf   Rm   Rs   Rt   Ru   Rx   Ro   R   R‚   RŒ   R   Rš   R   R   R    (    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyR      sZ   																													!			
	
						"		(   t
   exceptionst   bodyRy   t   syst	   tracebackRv   R   t   reR   t   codecsR…   t   nodeR    R   R   R   R   t   PILR   (    (    (    s'   /home/brian/tmp/gallery2-2.0/picture.pyt   <module>   s   0"