Attachment 'awktable-1.2.3.py'
Download 1 """
2 MoinMoin - awktable a Processor for simple formulars in a table
3 @license: GNU GPL, see COPYING for details.
4
5 PURPOSE:
6 This processor is used to do some calculations in a tabular based on awk.
7
8 CALLING SEQUENCE:
9 {{{
10 #!awktable
11 }}}
12
13 INPUTS:
14 a tabular including formulars
15
16 EXAMPLE:
17 {{{
18 #!awktable
19 ||=1+2||a=1||
20 }}}
21
22 {{{
23 #!awktable
24 ||A||B||result||
25 ||1||1||=$1+$2||
26 ||2||2||=$1+$2||
27 ||3||2||=$1+$2||
28 }}}
29
30 {{{
31 #!awktable
32 ||A||B||C ||D ||
33 ||1||1||=$1+$2||=$3/1.5||
34 ||2||2||=$1+$2||=$3/2.5||
35 ||3||2||=$1+$2||=$3/3.5||
36 }}}
37
38 {{{
39 #!awktable
40 ||A||B||SUM||Endpreis||
41 ||1||1||22||=$1+$2||=$1*$3||=$4*$5||
42 ||1||2||3||4||5||6||
43 }}}
44
45 PROCEDURE:
46 This processor needs the external awk routine
47
48 RESTRICTIONS:
49 * a formular has to start by a "=" as first sign
50 * formulars could only access variables in the same line
51 * ">" and "|" in formulars are ignored because
52 they could be used to make some trouble on the server
53
54
55
56 @copyright: 2004-09-16 by ReimarBauer
57 Modified:
58
59
60 """
61
62 Dependencies = []
63
64 from MoinMoin.parser import wiki
65 import string,os
66
67 config_external_awk = "/usr/bin/awk"
68
69 def process(request, formatter, lines):
70
71 if lines[0].strip() == "#!awktable":
72 del lines[0]
73
74 matrix = lines
75
76 name="=" # searchstring
77 bad_sign1=">"
78 bad_sign2="|"
79 l=0 # linecounter
80 for txt in lines:
81 matrix[l]=txt
82 n_name=txt.count(name)
83 if (n_name > 0):
84 sargs=txt.split('||')
85 i=0 # Argument Counter
86 for arg in sargs:
87 test=arg.find(name)
88 bad1=arg.find(bad_sign1)
89 bad2=arg.find(bad_sign2)
90
91 if ((test == 0) and (len(arg) > 1) and (bad1 == -1) and (bad2 == -1)): # formular found
92 # and some protection of the server system
93
94 argument="'{"+arg.replace(name,'print ',1)+"}'" # always replace the first formular
95 subset=sargs
96 calc=string.join(subset,' ')
97
98 cmd = "echo %(text)s | %(external_awk)s %(argument)s " % {
99 "text": calc,
100 "external_awk": config_external_awk,
101 "argument": argument
102 }
103
104 f=os.popen(cmd,'r') # popen to get the result of the calculation
105 result=f.read()
106 f.flush()
107 zresult=string.strip(result)
108 sargs[i]=zresult
109
110 larg=string.join(arg,'')
111 txt=txt.replace(larg,zresult,1) # because of following fomrulars
112 # replace the formular by the result
113
114 i=i+1 # next argument
115 matrix[l]=txt # save the result
116 l=l+1 # next line
117
118 wikiizer = wiki.Parser(string.join(matrix,"\n"),request) # parser for wiki tabular
119 wikiizer.format(formatter)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.