"""
    MoinMoin - awktable a Processor for simple formulars in a table
    @license: GNU GPL, see COPYING for details.

    PURPOSE:
        This processor is used to do some calculations in a tabular based on awk.

    CALLING SEQUENCE:
       {{{
       #!awktable
       }}}

    INPUTS:
        a tabular including formulars

    EXAMPLE:
        {{{
        #!awktable
        ||=1+2||a=1||
        }}}

       {{{
       #!awktable
       ||A||B||result||
       ||1||1||=$1+$2||
       ||2||2||=$1+$2||
       ||3||2||=$1+$2||
       }}}

       {{{
       #!awktable
       ||A||B||C     ||D     ||
       ||1||1||=$1+$2||=$3/1.5||
       ||2||2||=$1+$2||=$3/2.5||
       ||3||2||=$1+$2||=$3/3.5||
       }}}

       {{{
       #!awktable
       ||A||B||SUM||Endpreis||
       ||1||1||22||=$1+$2||=$1*$3||=$4*$5||
       ||1||2||3||4||5||6||
       }}}

    PROCEDURE:
      This processor needs the external awk routine

    RESTRICTIONS:
      * a formular has to start by a "=" as first sign
      * formulars could only access variables in the same line
      * ">" and "|" in formulars are ignored because
        they could be used to make some trouble on the server



    @copyright: 2004-09-16 by ReimarBauer
    Modified:


"""

Dependencies = []

from MoinMoin.parser import wiki
import  string,os

config_external_awk = "/usr/bin/awk"

def process(request, formatter, lines):

    if lines[0].strip() == "#!awktable":
        del lines[0]

    matrix = lines

    name="=" # searchstring
    bad_sign1=">"
    bad_sign2="|"
    l=0      # linecounter
    for txt in lines:
      matrix[l]=txt
      n_name=txt.count(name)
      if (n_name > 0):
         sargs=txt.split('||')
	 i=0    # Argument Counter
         for arg in sargs:
             test=arg.find(name)
	     bad1=arg.find(bad_sign1)
             bad2=arg.find(bad_sign2)

             if ((test == 0) and (len(arg) > 1) and (bad1 == -1) and (bad2 == -1)): # formular found
	                                        # and some protection of the server system

                 argument="'{"+arg.replace(name,'print ',1)+"}'" # always replace the first formular
	         subset=sargs
	         calc=string.join(subset,' ')

	         cmd = "echo %(text)s | %(external_awk)s %(argument)s " % {
                       "text": calc,
                       "external_awk": config_external_awk,
                       "argument": argument
                 }

	         f=os.popen(cmd,'r') # popen to get the result of the calculation
                 result=f.read()
                 f.flush()
	         zresult=string.strip(result)
	         sargs[i]=zresult

	         larg=string.join(arg,'')
	         txt=txt.replace(larg,zresult,1) # because of following fomrulars
		                                 # replace the formular by the result

             i=i+1 # next argument
         matrix[l]=txt # save the result
      l=l+1 # next line

    wikiizer = wiki.Parser(string.join(matrix,"\n"),request) # parser for wiki tabular
    wikiizer.format(formatter)







