#!/usr/bin/python2.4
# -*- coding: iso-8859-1 -*-
#Author: Igor Támara igor@tamarapatino.org
#Date: 12/04/2006
#No warranties.
"""
      MoinMoin - Random Banner
   Purpose : The purpose of this script is to link to another 
   place with a banner, like a webring

   Inputs : Receives an URL of a csv file separated with semicolon 
   with three fields :
   name of site;url site;url banner

     The second argument is the width of the banner
     The third argument is the height of the banner

   Output:  Shows the banner, when you move over the banner it shows 
   the legend and finally when you click you are directed to the site.

   Examples :
   ||<tablestyle="text-align:center;width:100%">[[RandomBanner(http://servidor.slec.net/~igotam/anillo.txt)]]||
   will show any of the records on the file as a banner linking to the site.

   [[RandomBanner(http://servidor.slec.net/~igotam/anillo.txt,200,20)]]
   will show a random banner on 200x20 size, linking.

A file containing the following lines worked for me.

Slec;http://www.slec.net;http://servidor.slec.net/~igotam/images/banner.gif
Gleducar;http://www.gleducar.org.ar;http://servidor.slec.net/~igotam/images/gledubanner.gif
Educalibre;http://www.educalibre.cl;http://servidor.slec.net/~igotam/images/banner.gif
Structio;http://structio.sourceforge.net;http://structio.sourceforge.net/images/bannerstructio.gif
Flisol;http://www.installfest.info;http://www.linuxpreview.org/images/banners/banner-flisol2006-p1latin.gif


   Side Notes :
   Alternate ways of doing this would include using ImageLink macro with
   RandomQuote :) .  I remembered that when I have just finished doing this.
   RandomBanner DOES NOT rely on those macros.
   
   Please check that your file is well constructed.
   
   Comments :
   Are welcome
"""

#Dependencies = ["pages"]
import urllib
import random

def parseargs(args) :
    a=args.split(",")
    w=468
    h=60
    works=0
    if len(a)==1 :
        txtfile=a[0]
        works=1
    elif len(a)==3:
        txtfile=a[0]
        w=a[1]
        h=a[2]
        works=1
    return txtfile,w,h,works

def mypict(txtfile,width,height) :
    f=urllib.urlopen(txtfile)
    lines=f.readlines()
    text,dest,imageurl=random.choice(lines).split(";")
    if imageurl[-1]=="\n" :
        imageurl=imageurl[:-1]
    return '<a href="%s"><img src="%s" alt="%s %s" title="%s %s" width="%d" height="%d"></a>' % (dest,imageurl,text,dest,text,dest,width,height)
def execute(macro, args):
    fine=1
    res=""
    if len(args)>10 :
        txtfile,width,height,fine=parseargs(args)
    if fine :
        res=mypict(txtfile,width,height)
    return macro.formatter.rawHTML(res)
