ó
ôYµDc           @   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
 y d  d l Z Wn n Xd Z d a d Z d Z d Z d Z d Z e j 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 „  Z( d S(   iÿÿÿÿNi    i   i   i   i   s   ^\.[\/]c         C   s   |  } | d j  d ƒ a d S(   sc   provide functions a reference to the props dictionary.Must be given immediately after program startt   pictypest   ;N(   t   splitR    (   t   xpropst   props(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   init    s    c         C   s   t  j t  j |  ƒ d ƒ S(   s)   Remove redundant whitespace from a stringt    (   t   stringt   joinR   (   t   text(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   trim'   s    c      	   C   s¾   d } x± t D]© } d | } t j | t j ƒ } x t j |  ƒ D]p } | j | ƒ rB t j	 j
 t j	 j |  | ƒ ƒ rB | s‡ g  } n  | j t j	 j |  t j d | ƒ ƒ ƒ qB qB Wq W| S(   sk   Find all files of filetype defined in props["pictypes"] in 'dir'. Return sorted list with filenames or Nones   .*%st    N(   t   NoneR    t   ret   compilet
   IGNORECASEt   dircachet   listdirt   searcht   ost   patht   isfileR   t   appendt   removeDotSlasht   sub(   t   dirt   picturest   pictypet   regext   checkextensiont   file(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt	   globFiles,   s    
0	3c         C   sd   d } xW t j |  ƒ D]F } t j j t j j |  | ƒ ƒ r | sL g  } n  | j | ƒ q q W| S(   s^   Return sorted list of subdirectories in supplied dir.
    Supplied dir must be fully qualifiedN(   R   R   R   R   R   t   isdirR   R   (   R   t   resultR   (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   getDirs=   s    !	c         C   sj   xc t  D][ } d | } t j | t j ƒ } x3 | D]+ } | j | ƒ r3 t j d | ƒ ‚ q3 q3 Wq Wd S(   s¯   Callback for walk() which throws NE_FoundPic if a
       file of filetype props["pictypes"] is found.

       The exception is thrown to make walk() quit it's
       operations   .*%ss   Found %sN(   R    R   R   R   R   t
   exceptionst   NE_FoundPic(   t   dummyt   dirnamet	   filenamesR   R   R   R   (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   hasPicJ   s    
c         C   s’   t  |  ƒ } d } | rŽ xs | D]h } y) t j j t j j |  | ƒ t d ƒ Wq t j k
 r† | d k rv g  } n  | j	 | ƒ q Xq Wn  | S(   sh   Return list of dirs which has pics or subdirs with pics
       Resultlist is appended in callback methodN(
   R"   R   R   R   t   walkR   R(   R#   R$   R   (   R   t   subdirst
   resultlistt   subdir(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   getSubdirsWithPicsZ   s    )	c         C   sP   d } y< x5 |  D]- } t  j j | ƒ } | | k r | } q q WWn n X| S(   s,   Return max unix timestamp from list of filesi    (   R   R   t   getmtime(   t   filelistR!   R   t   mtime(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   findYoungestl   s    c         C   sp   yQ t  j j |  ƒ d k r+ t  j |  ƒ n  t  j j |  | ƒ } t j | | ƒ Wn t ƒ  t j	 ƒ  n Xd S(   s#   Copy fullsize pictures to directoryi    N(
   R   R   t   existst   mkdirR   t   shutilt   copyfilet   printTracebackt   syst   exit(   t	   directoryR   t   outfile(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   copyFullSizey   s    c   	      C   sB  y#|  d } |  d } |  d } |  d } |  d } |  d } |  d } g  } | j  d | d	 | d
 | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d ƒ t t j j | | ƒ d ƒ } | j t j | ƒ ƒ | j ƒ  Wn t	 ƒ  t
 j ƒ  n Xd S(   s$   Construct stylesheet unconditionallyt   galleryDirectoryt
   stylesheett   fgColort   bgColort   lnColort   lnVisitColort
   hoverColors#   
        
BODY { background-color: s
   ;
 color: s»   ;
 font-family: verdana, helvetica, univers, sans-serif;  
}

H1 { font-size: x-large;
 font-weight: bold;
 text-align: center;
 margin: 2px 2px 2px 2px;
 padding: 0px 0px;
}

P { color: sZ   ;
 font-size: 10pt;
 font-weight: normal;
 text-align: left;
}
        
P.footer { color: sÿ   ;
 font-size: 8pt ;
 font-style: italic;
 text-align: left;
 margin: 0px 10px 2px 5px;
 padding: 0px 5px 5px 5px;
}

IMG { border-style: solid;
      border-color: #D4D6D2;
      border-width: 2px;
}

td#sidelinks a:link {
 text-decoration: none;
 color: s   ;
 padding: 5px 1px 5px 20px;
 font-size: 12pt; font-weight: normal;                 
}

td#sidelinks a:hover {
 text-decoration: none;
 color: sY   ;
 padding: 5px 1px 5px 20 px;
 font-size: 12pt; font-weight: normal;
 background-color: sD   ;
}

td#sidelinks a:visited:hover {
 text-decoration: none;
 color: s)   ;
}


A {text-decoration : none;
 color: s?   ;
 margin: 0px 10px 0px 0 px;
 border: 0px;
 background-color: s   ;
}

A:link {
 color: s   ;
}

A:visited {
   color: s   ;
}

A:hover {
 color: s   ;
 background-color: s3   ;
 font-weight: bold;
}

A:visited:hover {
 color: s  ;
 font-weight: bold;
}


A IMG {
  border-width: 1px;
}

HR { color: #EAEAEA;
 height: 1;    
}

HR.headlineupper {
  margin: 5px 0px 0px 0px;
  padding: 0px 0px;
}

HR.headlinelower {
  margin: 0px 0px 1em 0px;
  padding: 0px 0px;
}

HR.abovegallerylink { margin: 0px 0px 0px 0px;
}
t   wN(   R   t   openR   R   R   t   writeR   t
   joinfieldst   closeR6   R7   R8   (	   R   R9   R=   R>   R?   R@   RA   RB   t   html(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   createStylesheet…   s$    






P‡c         C   s“   d } d } | j  d ƒ } | d k r4 | |  } n
 | d  } x6 y( |  | d t | ƒ d | d } WqA PqA XqA | d t | ƒ d } | S(   sN   When adding pictures, nameclashes may arise. If so, construct unique name herei   R   t   _iÿÿÿÿiüÿÿÿs   .jpg(   t   rfindt   str(   t   fileNameDictionaryR   t   countert   slicet   underscorePost   newFileName(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   constructUniqueFilename  s    
c           C   sF   t  j d d IJt  j d IJt j d t  j ƒ t  j d d IJd  S(   Nt   -i<   s   Error in applicationR   (   R7   t   stderrt	   tracebackt	   print_exc(    (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyR6     s    c         C   s   |  r
 d Sd Sd  S(   Nt   Yest   No(    (   t   boolean(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   getYesNo"  s    c         C   s'   |  s
 d St  j j j |  j ƒ  ƒ Sd  S(   NR   (   t   xmlt   saxt   saxutilst   escapet   strip(   RL   (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt	   xmlString*  s    c         C   s”   |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d	 d
 ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  S(   s!   Quote xml for &, >, <, \, /, [, ]t   &s   &amp;t   <s   &lt;t   >s   &gt;s   "s   &quot;s   \/s   &#047;s   \s   &#092;t   ]s   &#091;t   [s   &#093;(   t   replace(   R   (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   quoteIt1  s    c         C   s   t  j |  ƒ |  S(   sO   Quote so that links with those
    chars works on IE, it does on FireFox anyway(   t   urllibt
   quote_plus(   R   (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   quoteUrl?  s    c         C   s  t  j j |  d ƒ \ } } y t  j | ƒ Wn n Xt  j j t  j j |  d ƒ ƒ d k r} t  j t  j j |  d ƒ ƒ n  t  j j t  j j |  d ƒ ƒ d k rÃ t  j t  j j |  d ƒ ƒ n  | rt  j j t  j j |  d ƒ ƒ d k rt  j t  j j |  d ƒ ƒ qn  d S(   s,   Create destination dir and necessary subdirss   /dummyt
   thumbnailsi    t   resizedt   fullsizeN(   R   R   R   t   makedirsR2   R   R3   (   t   pictureDirectoryRm   t	   Directoryt   FileName(    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   createDirectoriesH  s    '''c         C   s¹   d } t j j |  d ƒ } t j j | ƒ rR t | ƒ } | j ƒ  } | j ƒ  n  | d k rw t j j |  ƒ d } n  | d d k rµ y! | d d k	 r§ | d } n  Wqµ qµ Xn  | S(   sÑ   Return title, if default filename 'title.txt' is present in a 
    directory the content is used, else the dirname is used.
    If recursion is not on and a supplied title parameter is specified it is returneds	   title.txti   t   recursei    t   titleN(	   R   R   R   R   R   RD   t   readRG   R   (   t   srcdirectoryR   Rt   t   filenameR   (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   getTitle`  s    c         C   sY   |  s
 |  Sd | d } d | d } g  |  D] } d | | | f ^ q- } d j  | ƒ S(   sB   Wrap listitems in <tag>listitem</tag>
       returns a string
    Rb   Rc   s   </s   %s%s%s
R   (   R   (   t   listt   tagt   starttagt   endtagt   itemR!   (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   wrapItemsInTagy  s    &()   R#   t   globR   R7   t   getoptRU   R   t   structR   R   Rh   t   xml.sax.saxutilsR[   R   R   R    t   status_workingt   status_finishedt   status_errort   status_infot   status_finished_okR   R   R   R
   R   R"   R(   R-   R1   R;   RI   RR   R6   RZ   R`   Rg   Rj   Rr   Rx   R~   (    (    (    s)   /home/brian/tmp/gallery2-2.0/functions.pyt   <module>   sF   <									ƒ								