# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Gallery2 parser

    PURPOSE:
        This parser is used to visualize a couple of images as a thumbnail gallery.
        Optional a description of an image could be added including WikiName.
        On default the image name and it's creation date is shown.
        If you click on a thumbnail you get the webnails shown. By a menue you are able to toggle between the slides.

    CALLING SEQUENCE:
      {{{
      #!Gallery2 [columns=columns],[filter=filter],[mode=mode],
                 [show_text=show_text],[show_date=show_date], [show_tools=show_tools],
                 [sort_by_name=sort_by_name],[sort_by_date=sort_by_date], [sort_by_alias=sort_by_alias],
                 [reverse_sort=reverse_sort],
                 [only_items=only_items],[template_itemlist=template_itemlist],
                 [album=album],[album_name=album_name],[front_image=front_image],
                 [thumbnail_width=thumbnail_width],[webnail_width=webnail_width],[text_width=text_width],
                 [image_for_webnail=image_for_webnail],
                 [sequence_name=sequence_name], [sequence_type=sequence_type],
                 [border_thick=border_thick],[renew=renew],[help=help]
      * [image1.jpg alias]
      * [image2.jpg alias]
      }}}

    KEYWORD PARAMETERS:
        columns:           number of columns for thumbnails
        filter:            regex to select images
        show_text:         default is 1 description is shown
                           any other means no description
        show_date:         default is 1 date info from exif header if available is shown
        show_tools:        default is 1 icon toolbar is show any other disables this
        sort_by_name:      default is 1, the images are sorted by name, but not if only_items is 1
        sort_by_date:      default is 0, if set to 1 the images are sorted to the modification time
        sort_by_alias      default is 0, if set to 1 and only_items set to 1 it is used to order the images by the alias name
        reverse_sort:      default is 0, if set to 1 the file list is reversed
                           any other means no description
        mode:              default is 1 this means description below the image
                           any other number means description right of image
        only_items:        default is 0 if it is set to 1 only images which are described in listitem are shown
                           dependend on the order of the items
        template_itemlist: default is 0, if set to 1 an item list is shown which could be copied into the script. 
        album:             default is 0 if set to 1 only the first image of a series is shown but slideshow over all images 
        album_name:        useful for album. default is 'album' use it as short name for the album. 
        front_image:       Useful for album.  default is ''. The first image is shown in front of the album and slideshow.
                           If set to an existing image name this is shown in front of album and slideshow. 
                           The slide show could start by this somewhere.
        border_thick:      default is 1 this is the thickness in pixeln of the outer frame
        renew:             default is 0 if set to 1 then all selected thumbnails_* and webnails_* removed.
                           Afterwards they are new created.
        thumbnail_width:   default is 128
        webnail_width:     default is 640
        text_width:        default is 140		   
        image_for_webnail  default is 0 if set to 1 then the image is shown as preview and not the webnail
        help:              default is 0 if set a copy of the CALLING SEQUENCE is shown, 
                           (there are some new ideas around to show help to an user so this will be later replaced)
        sequence_name:     (ONLY POSIX) default is ''  if set this name is used for an image 
                           sequence with an duration of one image per second.
                           While for gif and png files the fli format is used a mpg file is created for jpegs.
                           For more info see the description on PROCEDURE below
                           Because mpeg creation could be take much cpu time this is done as background process. 
        sequence_type:     (ONLY POSIX) default is '' by this for png/gif fli format and the original image is used
                           and for jpg the mpeg format and the webnails are used. It could be set to fli or mpg. 
                           If it is set to mpg then always the original images are used.
                           mpg creation takes much more time and the file could be much bigger as a standard fli file      
                                                      

    OPTIONAL INPUTS:
        itemlist : if it is used and only_items is 1 then only the images in this list are ahown.
                   The alias text is used as description of the image instead of the file name


    EXAMPLE:
= GalleryTest =

== all images shown, one is decribed ==
{{{
{ { {
#!Gallery2
* [100_1185.JPG Bremen, SpaceCenter]
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2
* [100_1185.JPG Bremen, SpaceCenter]
}}}

== only thumbnails and only_items ==
{{{
{ { {
#!Gallery2 show_text=0,show_tools=0,show_date=0,columns=2,only_items=1
 * [100_1185.JPG Bremen, SpaceCenter]
 * [100_1194.JPG Bremen]
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2 show_text=0,show_tools=0,show_date=0,columns=2,only_items=1
 * [100_1185.JPG Bremen, SpaceCenter]
 * [100_1194.JPG Bremen]
}}}

== only_items by two columns and text right ==

{{{
{ { {
#!Gallery2 mode=2,columns=2,only_items=1
 * [100_1185.JPG Bremen, SpaceCenter]
 * [100_1194.JPG Bremen]
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2 mode=2,columns=2,only_items=1
 * [100_1185.JPG Bremen, SpaceCenter]
 * [100_1194.JPG Bremen, behind SpaceCenter]
}}}

----

== only_items by two columns, date supressed ==

{{{
{ { {
#!Gallery2 columns=2,only_items=1,show_date=0
 * [100_1185.JPG Bremen, SpaceCenter]
 * [100_1194.JPG Bremen, behind SpaceCenter]
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2 columns=2,only_items=1,show_date=0
 * [100_1185.JPG Bremen, SpaceCenter]
 * [100_1194.JPG Bremen, behind SpaceCenter]
}}}


== filter regex used, mode 2, icons and date supressed, one column and border_thick=5 ==
{{{
{ { {
#!Gallery2 columns=1,filter=100_118[0-5],mode=2,show_date=0,show_tools=0,border_thick=5
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2 columns=1,filter=100_118[0-7],mode=2,show_date=0,show_tools=0,border_thick=5
}}}

== other macro calls ==
{{{
{ { {
#!Gallery2 only_items=1,show_date=0
 * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2 only_items=1,show_date=0
 * [100_1189.JPG [[MiniPage(||Bremen||SpaceCenter||\n|| ||SpaceJump||)]]]
}}}

== renew means always new thumbnails and webnails of selection ==
{{{
{ { {
#!Gallery2 only_items=1,show_date=0,show_tools=0,renew=1
 * [100_1189.JPG [[MiniPage(||["Bremen"]||SpaceCenter||\n|| ||SpaceJump||)]]]
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2 only_items=1,show_date=0,renew=1
 * [100_1189.JPG [[MiniPage(||["Bremen"]||SpaceCenter||\n|| ||SpaceJump||)]]]
}}}

== template_itemlist ==
{{{
{ { {
#!Gallery2 template_itemlist=1
* [100_1185.JPG Bremen, SpaceCenter]
} } }
}}}

Result: [[BR]]
 {{{
#!Gallery2 template_itemlist=1
* [100_1185.JPG Bremen, SpaceCenter]
}}}

== help to show Calling Sequence ==
 {{{
{ { {
#!Gallery2 help=1
} } }
}}}

Result: [[BR]]
{{{
#!Gallery2 help=1
}}}


    PROCEDURE:
    
      HOWTO:
      Download some images to a page and start with the examples.
      Aliasing/Description of the filenames are done by adding an itemlist, see example.

      NEEDS:
      This routine requires the Action macro gallery2Image which is used to rotate or delete a
      selected image. The actual version is gallery2image-1.3.5-10.py.
      Only users which have the rights to delete are able to execute this action macro. 
      The icons of these are only shown if you have enough rights. 
      Furthermore it requires:
       * the PIL (Python Imaging Library).
       * the EXIF routine from http://home.cfl.rr.com/genecash/digital_camera.html
      
      and on POSIX systems optional:
       * ppm2fli: see  http://vento.pi.tu-berlin.de/fli.html
       * jpeg2mpeg: see http://www.stillhq.com/jpeg2mpeg

      At the moment I have added the EXIF routine to the parsers dir.
      It's not the best place but during developing it is nice to have it there
      If you put it to another place you have to change the line
      from MoinMoin.parser import EXIF too.

      The gallery2image macro does not take care on the EXIF header. This is lost by rotating.
      If a file is deleted by this macro it is moved to a bak file.
      
      ABOUT FLI/MPG:
      The sequence feature is at the moment only for servers using posix platforms available. 
      If one has an idea how to do it on other platforms I like to implement it.
      
      Sometimes people want to have the image sequence in one file playable without internet.
      Scientific data could be best animated by the common flc/fli format which was
      introduced by autodesk.
      If png/gif files are used fli output is generated. 
      These files could be played by quicktime, mplayer, xanim and several others
      For additional informations on this format look at:
      http://woodshole.er.usgs.gov/operations/modeling/flc.html
      http://vento.pi.tu-berlin.de/fli.html
      The largest X and largest Y dimension is used and the images are centered to these.
      The maximum size is 1280x1024 for this format so it is scaled to this if the images are larger
      It is not the best for true color images if they have very different color tables.
      Creating a sequence in this format does not take much cpu time while it could be very 
      long time for a mpg file.
                          
      If the parameter sequnece_name is used with jpg then the webnails of the images are used, 
      because they are normally in a size defined  viewable on screen. 
      The files are written into a mpg file using these libraries:
      Follow this inforamtion for installation at http://www.stillhq.com/jpeg2mpeg
      Because mpeg creation with a duration of 1 second does take a lot of cpu time this is 
      done as background process
      You have to take care yourself on equal dimensions of all images in a sequence of jpg files
      
      If you don't like the defaults you could ommit them by using the sequence_type parameter.
      By this it is possible to write fli files from jpg or mpg files from png/gif files. 
      Always if choosen the original images are used for mpg creation. 
      But if they are larger as 1280x1024 they are scaled down
      Probably you could got poor quality if you try mpg for png/gif files.
      The resize mechanism is not used for jpg files by this parameter the original files ae used.
      
      
      It does not go well if you mix up in a call png, gif and jpg files.
      
      GENERAL:
      Please remove the Version number from the code!
      
      If you want to upload many files at once please look on MoinMoin versions less 1.5.0
      at FeatureRequests/UploadMultipleAttachmentFiles/RulesForUnzip

    RESTRICTIONS:
      The movie mode is at the moment implemented for POSIX only.  
      As soon as  I know how to do it on other platforms I like to add it
      If you rotate an image at the moment the exif is destroyed. PIL ignores the exif header.
      This is not a quite big problem normally files with an EXIF header are right rotated.

    Required Images:
      I have put them to wiki/modern/img/ dir. The icons were created by me. License: GPL

    attachment:to_bak.png
    attachment:to_left.png
    attachment:to_right.png
    attachment:to_slide.png
    attachment:to_full.png

    HISTORY:
    While recognizing how to write MiniPage I got the idea to write a Gallery Parser.
    We have used in our wikis in the past the Gallery macro of SimonRyan.
    I have tried to modify it a bit to change it for 1.3 but my python skills weren't enough
    or it was easier to write it completly new.
    So this one shows now a way how a Gallery could be used by the parser and an action Macro.
    Probably it is a good example for others who like to know how to do this

    MODIFICATION HISTORY:
        Version 1.3.3.-1
        @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
        @license: GNU GPL, see COPYING for details.
        2005-03-26: Version 1.3.3-2 keyword renew added
                    creation of thumbnails and webnails in two calls splitted
                    Version 1.3.3-3 bug fixed if itemlist is given to describe only some of the images
                                    but only_items is not set to 1
                                    Example code changed
        2005-03-27: Version 1.3.3-4 Action macro added and the form to call it. User which have rights to delete
                                    could use the functions of gallery2Image.
        2005-08-03: Version 1.3.3-5 theme path for icons corrected and a platform independent path joining
                                    os.unlink removed as suggested by CraigJohnson
                                    sort_by_name is default if not only_items is 1
                                    optional sort_by_date could be used
                                    keyword template_itemlist added 
                                    keyword help added
                                    extra frame by mode=2 removed 
        2005-08-06: Version 1.3.5-6 slideshow mode added
                                    keyword image_for_webnail added
        2005-08-13: Version 1.3.5-7 syntax changed from GET to POST
                                    forms instead of links
                                    filenames from images submitted to gallery2image too
                                    new keyword sort_by_alias
                                    internal code clean up
                                    this version needs: gallery2image-1.3.5-5.py
        2005-08-14: Version 1.3.5-8 (TW) cleanup                                    
        2005-08-14: Version 1.3.5-9 html code for tables changed
                                    because of the ugly extra space of form elements
                                    div tag removed so now we use the page style
                                    slide show action goes to right webnail now
                                    this version needs: gallery2image-1.3.5-5.py
       2005-08-17: Version 1.3.5-10 html code separated in functions     
                                    structure of code changed, now you see the thumbnails after creation
                                    bug removed if quote is given but file does not exist
       2005-09-02: Version 1.3.5-11 keyword album, album_name and front_image added          
                                    image urls changed to complete server url      
       2005-11-12: Version 1.3.5-12 bug fixed for image_for_webnail=1            
                                    bug fixed at last cell table end tr instead of td
                                    bug fixed don't render a filename as WikiName
                                    bug fixed " is allowed in alias name
                                    bug fixed ' is allowed in alias name
                                    bug fixed linebreak by a space in alias  
                                    not quite a bug but makes it very difficult to code in 
                                    gallery2image so additional id removed in alias name
      2005-11-17: Version 1.3.5-13  implementation of sequence video clips at first step for posix only 
                                    sequence_type could be used to ommit the autoselection
                                    fli/flc files are used for gif and png files,  mpeg files for
                                    jpeg files. Duration on both is 1 image/second 
                                    feature added of recognising the right url pattern (http opr https)
      2005-12-03: Version 1.3.5-14  bug fixed of to_slide tools function a wrong mark was used
                                    all output of convert or ppm2fli redirected to /dev/null
                                    it is not necessary to keep output in the webservers log file
                                    
"""
Dependencies = []
from MoinMoin.action import AttachFile
from MoinMoin import wikiutil, config
from MoinMoin.Page import Page

import os, string, re, Image, StringIO, codecs

from MoinMoin.parser import EXIF

from MoinMoin.parser import wiki

def server(request):

  if request.is_ssl:
     url_pattern = 'https'
  else:
     url_pattern = 'http'
        
  return "%(url_pattern)s://%(server_name)s:%(server_port)s" %   {
                             "url_pattern":url_pattern,
                             "server_name":request.server_name, 
                             "server_port":str(request.server_port)}

def show_tools_restricted(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):
    if request.user.may.delete(pagename):
        return tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request)
    else:
        return ''    

def tools_restricted_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):
    text='''
                   <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                        <td>
                            <input type="hidden" name="action" value="gallery2image">
                            <input type="hidden" name="do" value="RL">
                            <input type="hidden" name="target" value="%(this_target)s">
                            <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_left.png" title="rotate to left">
                        </td>
                    </form>
                    <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                        <td>
                            <input type="hidden" name="action" value="gallery2image">
                            <input type="hidden" name="do" value="RR">
                            <input type="hidden" name="target" value="%(this_target)s">
                            <input type="image"  value="submit" src="%(server)s/wiki/modern/img/to_right.png" title="rotate to right" >
                        </td>
                    </form>
                    <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                        <td>
                            <input type="hidden" name="action" value="gallery2image">
                            <input type="hidden" name="do" value="RM">
                            <input type="hidden" name="target" value="%(this_target)s">
                            <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_bak.png" title="move to bak" >
                        </td>
                    </form>''' %  {
            "server" : server(request), 
            'baseurl': request.getScriptname(),
            "pagename":pagename,
            "this_target":this_target}                   
    return text                
    
def tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request):
    text='''
            <TABLE align="center" width="%(thumbnail_width)s">
                <TR>
                    <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                        <td>
                            <input type="hidden" name="action" value="AttachFile">
                            <input type="hidden" name="do" value="get">
                            <input type="hidden" name="target" value='%(this_target)s'>
                            <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_full.png" title="load image">
                        </td>
                    </form>
                    <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
                        <td>
                            <input type="hidden" name="action" value="gallery2image">
                            <input type="hidden" name="do" value="VS">
                            <input type="hidden" name="full" value='%(full)s'>
                            <input type="hidden" name="alias" value='%(alias)s'>
                            <input type="hidden" name="target" value='%(target)s'>
                            <input type="hidden" name="exif_date" value='%(exif_date)s'>
                            <input type="image" value="submit" src="%(server)s/wiki/modern/img/to_slide.png" title="slide_show" >
                       </td>
                    </form>
                    %(show_tools_restricted)s
                </TR>
            </TABLE>'''   %  {
            "server" : server(request),
            'baseurl': request.getScriptname(),
            "pagename":pagename,
            "thumbnail_width":thumbnail_width,
            "full":full,
            "alias":alias,
            "exif_date":exif_date,
            "target":target,
            "this_target":this_image,
            "show_tools_restricted":show_tools_restricted(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request)
            } 
   
    return text

def show_alias_mode2(show_alias,thumbnail_width,this_alias,text_width):
   if show_alias == '1': 
       return '''
        <td valign="top" width="%(text_width)s">
           %(this_alias)s
        </td>''' % {
        "this_alias":this_alias,
        "text_width":text_width}
   else:
      return ''     
        
def show_date_mode2(show_date,this_exif_date):    
   if show_date == '1': 
       return '''
   <td>
     <p>%(this_exif_date)s</p>
   </td>''' % {
   "this_exif_date":this_exif_date }
   else:
        return '';
               
def show_tools_mode2(show_tools,pagename,this_target,thumbnail_width,full,alias,target,exif_date,request):  
    if show_tools == '1' :
        return "<td align=""center""> %s </td>" % tools_html(pagename,this_target,thumbnail_width,full,alias,target,exif_date,request)
    else:
        return ''
      


def mode2_html(pagename,border_thick,width,thumbnail_width,text_width,full,this_image,alias,this_alias,exif_date,this_exif_date,target,this_target,submit,show_tools,show_date,show_alias,request):
    text='''
    <tr valign="center">
        <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
            <td align="center" valign="center" width="%(thumbnail_width)s">
                <input type="hidden" name="action" value="gallery2image">
                <input type="hidden" name="do" value="VS">
                <input type="hidden" name="full" value='%(full)s'>
                <input type="hidden" name="alias" value='%(alias)s'>
                <input type="hidden" name="exif_date" value='%(exif_date)s'>
                <input type="hidden" name="target" value='%(target)s'>
                <input type="image" value="submit" src="%(server)s%(submit)s">
            </td>
        </form>
            %(alias_html)s 
    </tr>
    <tr>%(tools_html)s%(date_html)s</tr>'''%       {   
     "server" : server(request),
     "baseurl": request.getScriptname(),
     "pagename":pagename,
     "thumbnail_width":thumbnail_width,
     "full":full,
     "alias":alias,
     "exif_date":exif_date,
     "target":target,
     "submit":submit,
     "tools_html":show_tools_mode2(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request),
     "date_html": show_date_mode2(show_date,this_exif_date),
     "alias_html": show_alias_mode2(show_alias,thumbnail_width,this_alias,text_width)
     }
    
    return text

def show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request):  
    if show_tools == '1' :
        text="<tr><td align=""center"">%s </td></tr>" % tools_html(pagename,this_image,thumbnail_width,full,alias,target,exif_date,request)
    else:
        text=''
    return text        

def show_date_mode1(show_date,this_exif_date):    
   if show_date == '1': 
       return '''
    <TR>
        <td>%(this_exif_date)s</td>
    </TR>''' % {
    "this_exif_date":this_exif_date}
   else:
       return ''

def show_alias_mode1(show_alias,thumbnail_width,this_alias,text_width):
   if show_alias == '1': 
       return '''
    <TR>
        <td align="left" width="%(thumbnail_width)s"> %(this_alias)s</td>
    </TR>''' % {
    "thumbnail_width": thumbnail_width,
    "this_alias":this_alias}
   else:
       return ''     
       
def mode1_html(pagename,border_thick,width,thumbnail_width,text_width,full,this_image,alias,this_alias,exif_date,this_exif_date,target,this_target,submit,
               show_tools,show_date,show_alias,request):
   text='''
<table width="%(thumbnail_width)s" align="center" valign="center">
    <TR align="center" valign="center">
        <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
            <td align="center" valign="middle" width="%(thumbnail_width)s">
                <input type="hidden" name="action" value="gallery2image">
                <input type="hidden" name="do" value="VS">
                <input type="hidden" name="full" value='%(full)s'>
                <input type="hidden" name="alias" value='%(alias)s'>
                <input type="hidden" name="exif_date" value='%(exif_date)s'>
                <input type="hidden" name="target" value='%(target)s'>
                <input type="image" value="submit" src="%(server)s%(submit)s" >
            </td>
        </form>
    </TR>
      %(alias_html)s
      %(date_html)s
      %(tools_html)s
</table>'''%       {  
     "server" : server(request), 
     "baseurl": request.getScriptname() ,
     "pagename":pagename,
     "full":full,
     "alias":alias,
     "exif_date":exif_date,
     "target":target,
     "submit":submit,
     "thumbnail_width":thumbnail_width,
     "tools_html": show_tools_mode1(show_tools,pagename,this_image,thumbnail_width,full,alias,target,exif_date,request),  
     "date_html":show_date_mode1(show_date,this_exif_date),
     "alias_html": show_alias_mode1(show_alias,thumbnail_width,this_alias,text_width)
     }

   return text

def get_files(kw,path,files,quotes,request):
    web=[]
    full=[]
    thumb=[]
    exif_date=[]
    img_type=[]
    description=[]
    
    
    ddict={}
    n=len(quotes['image'])
    if n > 0 :
        i = 0
        for txt in quotes['image']:
            ddict[txt]=quotes['alias'][i]
            i += 1    
            
    video_type = ''
    source_type = ''
    for attfile in files:
        # only files not thumb or webnails
        if attfile.find('thumbnail_') == -1 and attfile.find('webnail_') == -1:
            # only images
            if wikiutil.isPicture(attfile):
                description.append(ddict.get(attfile, attfile))
                full.append(attfile)
                
                fname, ext = os.path.splitext(attfile)
                if ext in ('.gif', '.png'):
                    img_type.append('PNG')
                    webnail = 'webnail_%s.png' % fname
                    thumbfile = 'thumbnail_%s.png' % fname
                    video_type = 'fli'
                    source_type = ext[1:]
                else:
                    img_type.append("JPEG")
                    webnail = 'webnail_%s.jpg' % fname
                    thumbfile = 'thumbnail_%s.jpg' % fname
                    video_type = 'mpg'
                    source_type = 'jpg'    
                
                    
                infile = os.path.join(path, attfile)
                if os.path.exists(infile):
                     web.append(webnail)
                     thumb.append(thumbfile)
                     
                f = open(infile, 'rb')
                tags = EXIF.process_file(f)
                if tags.has_key('EXIF DateTimeOriginal'):
                    date = str(tags['EXIF DateTimeOriginal'])
                    date = date.replace(':', '-', 2)
                else:
                    date = '--'
                exif_date.append(date)
                f.close() 
    if kw['sequence_type'] != '': 
          video_type = kw['sequence_type']
          
    return thumb,web,full,video_type, exif_date,img_type,source_type,description  
    
def to_htmltext(text):        
  
    if text.find ("'"):   
        text = text.split("'")
        text = '&#39;'.join(text)
       
    return text

def to_wikiname(request,formatter,text):
  ##taken from MiniPage
    out=StringIO.StringIO()
    request.redirect(out)
    wikiizer = wiki.Parser(text.strip(),request)
    wikiizer.format(formatter)
    result=out.getvalue()
    request.redirect()
    del out
    
   
    
    result = result.replace('<a id="line-1"></a>','')
    result = result.replace('<p>','')
    result = result.replace('</p>','')
    result = result.strip() 
    return result
    
        
def get_quotes(self,formatter):
    quotes = self.raw.split('\n')
    quotes = [quote.strip() for quote in quotes]
    quotes = [quote[2:] for quote in quotes if quote.startswith('* ')]

    
    image=[]
    text=[]
    
    for line in quotes:
        im, na=line[1:-1].split(' ',1)
        na = na.strip()
        na = to_htmltext(na)
        na = to_wikiname(self.request,formatter,na)
        text.append(na)
        image.append(im.strip())

    return {
        'alias': text,
        'image': image,
    }



class Parser:
        
    def __init__(self, raw, request, **kw):
        self.raw = raw
        self.request = request
        self.form = request.form
        self._ = request.getText
        self.kw = {
            'sort_by_date': '0',
            'sort_by_name': '1',
            'sort_by_alias': '0',
            'album': '0',
            'album_name': 'album',
            'front_image':'',
            'template_itemlist': '0', 
            'reverse_sort': '0',
            'border_thick': '1',
            'columns': '4',
            'filter': '.',
            'mode': '1',
            'help': '0',
            'show_text': '1',
            'show_date': '1',
            'show_tools': '1',
            'only_items': '0',
            'image_for_webnail': '0',
            'renew': '0',
            'thumbnail_width': '128',
            'webnail_width': '640',
            'text_width': '140',
            'sequence_name' : '',
            'sequence_type' : '',
        }
       

        for arg in kw.get('format_args','').split(','):

            if arg.find('=') > -1:
                key, value=arg.split('=')
                self.kw[key]=wikiutil.escape(value, quote=1)


        self.kw['width']=str((int(self.kw['thumbnail_width'])+int(self.kw['text_width'])))


    def format(self, formatter):
        kw=self.kw
        Dict = {}
        quotes=get_quotes(self,formatter)
        current_pagename=formatter.page.page_name
        attachment_path = AttachFile.getAttachDir(self.request, current_pagename, create=1)
 
        if kw['help'] == '1':
            self.request.write('''
<br>
{{{<br>
#!Gallery2 [columns=columns],[filter=filter],[mode=mode],<br>
           [show_text=show_text],[show_date=show_date], [show_tools=show_tools],<br>
           [sort_by_name=sort_by_name],[sort_by_date=sort_by_date],[sort_by_alias=sort_by_alias]<br>
           [reverse_sort=reverse_sort],<br>
           [only_items=only_items],[template_itemlist=template_itemlist],<br>
           [album=album],[album_name=album_name],[front_image=front_image],<br>
           [thumbnail_width=thumbnail_width],[webnail_width=webnail_width],[text_width=text_width],<br>
           [image_for_webnail=image_for_webnail],<br>
           [sequence_name=sequence_name],[sequence_type=sequence_type]<br>
           [border_thick=border_thick],[renew=renew],[help=help]<br>
 * [image1.jpg alias]<br>
 * [image2.jpg alias]<br>
}}}<br>''')
            return    
            
        
        if kw['only_items'] == '1':
            all_files=quotes['image']
            result=[]
            for attfile in all_files:
                infile=os.path.join(attachment_path,attfile)
                if os.path.exists(infile):
                   result.append(attfile) 
            all_files = result        
            
            if kw['sort_by_alias'] == '1':
                new_ordered_files=[]
                alias_text=quotes['alias']
                
                i=0
                for attfile in all_files:
                    infile=os.path.join(attachment_path,attfile)
                    ft_file=str(os.path.getmtime(infile))+os.tmpnam()
                    Dict[alias_text[i]]=attfile
                    i += 1
       
                keys = Dict.keys()   
                keys.sort()
                for txt in keys:
                    new_ordered_files.append(Dict[txt])
                   

                all_files=new_ordered_files
                Dict.clear()
           
        else:
            all_files=os.listdir(attachment_path)

        result = []

        for test in all_files:
           if re.match(kw['filter'], test):
              result.append(test)
        all_files=result
        
        if not all_files:
            self.request.write("<br><br><h1>No matching image file found!</h1>")
            return
            
        

        if kw['sort_by_name'] == '1' and kw['only_items'] == '0': 
            all_files.sort()
          
        if kw['sort_by_date']=='1': 
           for attfile in all_files:
               infile=os.path.join(attachment_path,attfile)
               ft_file=str(os.path.getmtime(infile))+os.tmpnam()
               Dict[ft_file]=attfile
               
           keys = Dict.keys()
           keys.sort()
           file_mdate=[]
           for txt in keys:
               file_mdate.append(Dict[txt])
           all_files=file_mdate
           Dict.clear()
         
        if kw['reverse_sort']=='1': 
             all_files.reverse()   

 
        cells=[]
        cell_name=[]
        img=[]
        
        thumb, web, full, video_type, exif_date, imgtype, source_type, description = get_files(kw, attachment_path, all_files, quotes, self.request)
        
        if kw['template_itemlist'] == '1':
            self.request.write('Copy the following listitems into the script. Replace alias with the label you want. Afterwards disable template_itemlist by setting it to 0:<BR>')
            for attfile in full : 
                self.request.write(' * [%(attfile)s %(date)s]<br>' % {
                                   'attfile' : attfile,
                                   'date'    : 'alias'
                                    })
                                    
        
        i = 0  
        z = 1
        cols = int(kw['columns'])
        
           
        n = len(full)
        if  kw['album'] == '0' :
            self.request.write("<table align='center' border='%s' >" % self.kw['border_thick'])
            if kw['mode'] == '1' or cols > 1   :
                self.request.write('<TR valign="bottom">')      
                self.request.write('<TD>') 
 
                           
        if kw['album'] == '1' :
            if kw['front_image'] == '' :
                front_image = full[0]  
            else:
                front_image =  kw['front_image']   
            ii = 0
            for tst in full : 
                if tst == front_image :
                    break 
                ii += 1   
            
        
        for attfile in full :
            if  kw['album'] == '1' :
                if tst == front_image :
                   i = ii
             
                                 
            this_description=description[i]
            this_exif_date=exif_date[i]
            this_webnail=web[i]
            this_imgtype=imgtype[i]
            this_thumbfile=thumb[i]
            
           
            thumbf=os.path.join(attachment_path,this_thumbfile)
            webf=os.path.join(attachment_path,this_webnail)            
            
            
            if kw['renew'] == '1':
                if os.path.exists(thumbf):
                   os.unlink(thumbf)
                if os.path.exists(webf):
                   os.unlink(webf)
                   
            if not os.path.exists(webf) or not os.path.exists(thumbf):
                infile = os.path.join(attachment_path,attfile)
                im = Image.open(infile)
                
                if not os.path.exists(webf):
                    im.thumbnail(((int(kw['webnail_width'])),((int(kw['webnail_width'])))), Image.ANTIALIAS)
                    if kw['image_for_webnail'] == '1' :
                       os.link(os.path.join(attachment_path,attfile),webf)
                    else:
                       im.save(webf, this_imgtype)
                if not os.path.exists(thumbf):
                    im.thumbnail(((int(kw['thumbnail_width'])),((int(kw['thumbnail_width'])))),
                                   Image.ANTIALIAS)
                    im.save(thumbf, this_imgtype)       
                    
                    
            if kw['image_for_webnail'] == '1' :     
                 this_webnailimg = attfile
                 webimg = full
            else: 
                 this_webnailimg = this_webnail
                 webimg = web
                 
           
            if kw['mode'] == '1':
                text = mode1_html(current_pagename,
                        kw['border_thick'],
                        kw['width'],
                        kw['thumbnail_width'],
                        kw['text_width'],
                        attfile + "," + ','.join(full),
                        attfile,
                        this_description + '!,!' + '!,!'.join(description),
                        this_description,
                        to_htmltext(this_exif_date + ',' + ','.join(exif_date)),
                        to_htmltext(this_exif_date),
                        this_webnailimg + ',' + ','.join(webimg),
                        this_webnailimg,
                        AttachFile.getAttachUrl(current_pagename, this_thumbfile, self.request),
                        kw['show_tools'],
                        kw['show_date'],
                        kw['show_text'],
                        self.request
                        )
                self.request.write(''.join(text))    
                        
            if kw['mode'] == '2':
                text = mode2_html(current_pagename,
                        kw['border_thick'],
                        kw['width'],
                        kw['thumbnail_width'],
                        kw['text_width'],
                        attfile + "," + ','.join(full),
                        attfile,
                        this_description + '!,!' + '!,!'.join(description),
                        this_description,
                        to_htmltext(this_exif_date + ',' + ','.join(exif_date)),
                        to_htmltext(this_exif_date),
                        this_webnailimg + ',' + ','.join(webimg),
                        this_webnailimg,
                        AttachFile.getAttachUrl(current_pagename, this_thumbfile, self.request),
                        kw['show_tools'],
                        kw['show_date'],
                        kw['show_text'],
                        self.request
                        )
            
                if cols > 1 : self.request.write('<table valign="bottom">')
                self.request.write(''.join(text))
                if cols > 1 : self.request.write('</table>')
            
            if kw['mode'] == '1' or cols > 1:    
                if kw['album'] == '0' :
                    if  z < cols :
                        self.request.write('</TD>')
                        if z <  n and  i < n - 1 :
                            self.request.write('<TD>')
                        if i == n - 1 :    
                            self.request.write('</TR>')
                    else: 
                        self.request.write('</TD>')
                        self.request.write('</TR>')
                        if i < n - 1 :
                            self.request.write('<TR valign="bottom">')
                            self.request.write('<TD>')
                            
            i += 1         
            z += 1
            if z > cols :
                z = 1
            
            if kw['album'] == '1' :
                self.request.write("%(n)s images (%(album_name)s)" % {"n": str(n), "album_name":kw['album_name']})
                break
        if kw['album'] == '0' :        
            if i < n :
                self.request.write('</TD>')
                self.request.write('</TR>')
            self.request.write('</table>')       
            
        ############################################
        ##TODO: syntax change to formatter - later #
        ############################################
        
        if os.name !=  'posix':
           text = '<p><FONT color="Red" >sequence_name feature not defined for this platform: %(platform)s </FONT><p>' % {"platform":os.name}
           self.request.write(text)  
           return
            
        if  kw['sequence_name'] != '' :       
            
            ppm2fli = '/usr/local/bin/ppm2fli'
            video_file = os.path.join(attachment_path,kw['sequence_name']) 
            
            if video_type == 'fli' : 
               
               if kw['renew'] == '1':
                  if os.path.exists(video_file + '.fli'):
                     os.unlink(video_file + '.fli')
               liste_file = '%(flcfile)s.txt' % { 'flcfile': video_file}     
               if not os.path.exists(video_file+'.fli'):
                   
                   data = open(liste_file,'w')
                   x=[]
                   y=[]
                   for attfile in full :
                       file = os.path.join(attachment_path,attfile)   
                       im = Image.open(file)
                       size = im.size
                       x.append(size[0])
                       y.append(size[1])
                       data.write("%(file)s\n" % {'file':attfile})
                   data.close()
         
                   
                   if max(x) > 1280.0:
                      this_x = 1280.0
                      f = this_x / max(x)
                      this_y = max(y) *  f
                   else: 
                      this_x = max(x)
                      this_y = max(y)   
                      
                   if this_y > 1024.0:
                       new_y = 1024.0
                       f =   new_y /this_y
                       this_y = new_y
                       this_x =  this_x * f
                   
                   sz = '%(szx)sx%(szy)s' % { 'szx': long(this_x),
                                              'szy': long(this_y) }
#wir mueesen in den pfad wechseln    
#wenn Zeit ist ppm12fli überarbeiten                                  
                   cmd = ''
                   if source_type == 'png':         
                      cmd = 'cd "%(attachment_path)s" &&  %(ppm2fli)s  -g %(sz)s -fpngtopnm -s 1000 %(flcfile)s.txt %(flcfile)s.fli > /dev/null ' % {
                                 'ppm2fli': ppm2fli,
                                 'flcfile': kw['sequence_name'] ,
                                 'attachment_path': attachment_path,
                                 'sz': sz }
                                 
                   if source_type == 'gif':             
                      cmd = 'cd "%(attachment_path)s" && %(ppm2fli)s  -g %(sz)s -fgiftopnm -s 1000 %(flcfile)s.txt %(flcfile)s.fli > /dev/null ' % {
                                 'ppm2fli': ppm2fli,
                                 'flcfile': kw['sequence_name'] ,
                                 'attachment_path': attachment_path,
                                 'sz': sz }
                   
                   if source_type == 'jpg':    
                              
                      cmd = 'cd "%(attachment_path)s" && %(ppm2fli)s  -g %(sz)s -fjpegtopnm -s 1000 %(flcfile)s.txt %(flcfile)s.fli 1> /dev/null 2> /dev/null' % {
                                 'ppm2fli': ppm2fli,
                                 'flcfile': kw['sequence_name'] ,
                                 'attachment_path': attachment_path,
                                 'sz': sz }
                     
                           
                   if cmd != '':
                      os.system(cmd)    
               
               if os.path.exists(video_file+'.fli'):
                  if os.path.exists(liste_file):
                     os.unlink(liste_file) # liste entfernen wenn file da ist
                  dict = {}
                  dict['src'] = AttachFile.getAttachUrl(current_pagename,'%(flcfile)s.fli'  % { 
                     'flcfile': kw['sequence_name']},self.request)
                  image_link = '%(flcfile)s.fli' % { 'flcfile':kw['sequence_name']}
               
                  self.request.write('<BR>')
                  text = formatter.url(1,dict['src'] ) + image_link + formatter.url(0)
                  self.request.write('Download this image sequence %(text)s for your archive' % { 'text': text})
            
                      
            if video_type == 'mpg':      
            #  http://www.stillhq.com/jpeg2mpeg
               if kw['renew'] == '1':
                  if os.path.exists(video_file + '.mpg'):
                     os.unlink(video_file + '.mpg')
               if not os.path.exists(os.path.join(attachment_path,'%(file)s.mpg' % {'file':kw['sequence_name']})):
                   if kw['sequence_type'] == 'mpg':
                      liste = string.join(full,' ')
                   else:
                      liste = string.join(web,' ')
                      
                   cmd = 'cd "%(attachment_path)s" && convert -delay 100  %(liste)s %(file)s.mpg 1> /dev/null 2> /dev/null &' % {
                                    'liste':liste,
                                    'file':kw['sequence_name'] ,
                                    'attachment_path':attachment_path}
                   os.system(cmd)  
                   self.request.write('<P><FONT color="Red" > MPEG creation as background process started </FONT></P>')
               if os.path.exists(os.path.join(attachment_path,'%(file)s.mpg' % {'file':kw['sequence_name']})):
                  dict = {}
                  dict['src'] = AttachFile.getAttachUrl(current_pagename,'%(file)s.mpg'  % { 
                     'file': kw['sequence_name']},self.request)
                     
                  image_link = '%(file)s.mpg' % { 'file':kw['sequence_name']}
               
                  self.request.write('<BR>')
                  text = formatter.url(1,dict['src'] ) + image_link + formatter.url(0)
                  self.request.write('Download this image sequence %(text)s for your archive' % { 'text': text})
              
              
         ## the sequence block needs to be refactored by better names
        
       