[Zope] External method building a ksh script

Albert Boulanger aboulanger@ldeo.columbia.edu
Tue, 04 May 1999 22:09:52 -0500


I am puzzled on this. The following dtml calls the following external
method makeScript (template2 is a property on the containing folder
which is the text of the ksh script template):

<!--#var standard_html_header-->
<h2><!--#var title_or_id--></h2>

<!--#in expr="REQUEST.form.items()"-->

<!--#var sequence-key-->: <!--#var sequence-item-->
<br>
<!--#/in-->


<!--#var expr="makeScript(template2,REQUEST.form)"-->

<!--#var standard_html_footer-->

I get the following error:

Traceback (innermost last):
  File lib/python/ZPublisher/Publish.py, line 877, in publish_module
  File lib/python/ZPublisher/Publish.py, line 590, in publish
    (Info:
/SeisRes/SeisResSub/ST295/Exp-2-18-99/SeisMod/submission/process)
  File lib/python/OFS/DTMLDocument.py, line 212, in __call__
    (Object: process)
  File lib/python/OFS/DTMLDocument.py, line 208, in __call__
    (Object: process)
  File lib/python/DocumentTemplate/DT_String.py, line 513, in __call__
    (Object: process)
  File lib/python/DocumentTemplate/DT_Util.py, line 266, in eval
    (Object: makeScript(template2,REQUEST.form))
  File <string>, line 0, in ?
  File lib/python/Products/ExternalMethod/ExternalMethod.py, line 254,
in __call__
    (Object: CopySource)
    (Info: (('#!/bin/ksh\012# @ input = %%input%%\012# @ output =
%%output%%\012# @ error =  %%error%%\012# @ notify_user =
%%notify%%\012# @ class = %%class%%\012# @ notification =
%%notification%%\012# @ checkpoint = %%checkpoint%%\012# @ restart =
%%restart%%\012# @ requirements = (Arch == "R6000") && (OpSys ==
"AIX42") && (Adapter == "hps_user") &&  (Pool == 01)\012# @
min_processors =%%min_processors%%\012# @ max_processors
=%%max_processors%%\012# @ job_type = %%job_type%%\012# @
queue\012\012export MP_PROCS=24  \012export MP_RMPOOL401\012export
MP_EUIDEVICE=css0\012export MP_EUILIB=us\012export
MP_STDOUTMODE=ordered\012export MP_INFOLEVEL=3\012export
MP_LABELIO=yes\012export MP_STDOUTMODE=ordered\012\012/usr/bin/poe
%%program%% %%parameter_file%% \012# send notice\012# mail %%notify%% <
%%output%%\012# mail %%notify%% < %%error%%\012', {'checkpoint:':
'false', 'restart:': 'false', 'notification:': 'always',
'max_processors:': '24', 'class:': 'sp-15minp', 'job_type:': 'parallel',
'program:': '/data/rat/liqing/SeisRes/SRFC/programs/FDM_SP2_V2/fdmodel',
'min_processors:': '24', 'error:': '/data/4draid/liqing/FDM/run_24.out',
'input:': '/dev/null', 'notify:': 'liqing@ldeo.columbia.edu',
'parameter_file:': '/data/4draid/liqing/FDM/model_parameter.dat',
'output:': '/data/4draid/liqing/FDM/run_24.out'}), {}, None))
TypeError: (see above)

python code for makeScript.py:
-------------------------------------------------------------------------------

from string import split

def makeScript(lines,d):
    """Given a string of lines call the helper function
    """
    return makeScripthelper(split(lines,'\n'),d)


def makeScripthelper(listOfLines, d):
    """Given a list of Lines and a dictonary, replace all lines of the
    form %%foo%% with the value of the dictionary.  Given the input
    file:

    # This is before
    # This is a comment input=%%input%%
    input=%%input%% # A comment here
    output=%%output%%
    # This is after
    The last line

    With the dictonary:

    {'input': '/tmp/foo.input', 'output' : '/tmp/foo.output'}

    Produces the output of:

    # This is before
    # This is a comment input=%%input%%
    input=/tmp/foo.input # A comment here
    output=/tmp/foo.output
    # This is after
    The last line
    """

    hash = re.compile("#")

    result = []                         # Result

    # For every line
    for line in listOfLines:
        found = 0
        m = hash.search(line)
        left = line
        right = ''
        if m:
            # We have a comment, don't replace it
            l = m.start()
            left = line[0:l]            # left of the comment
            right = line[l:]            # right of the comment
        # For every key in the dictionary
        for item in d.keys():
            # See if we have a match
            (data, n) = re.subn('%%%%%s%%%%' % item , d[item], left)
            if n:
                # if we do, print it and break out
                result.append("%s%s" % (data, right))
                found = 1
                break
        if found == 0:
            # no substitution found, print the original line
            result.append("%s" % (line))

    return result

-----------------------------

Any ideas? (I know about the extra colons in the var names, have to deal
with that as well.)

Stlill ascending the Zope ladder,
Albert Boulanger
aboulanger@ldeo.columbia.edu