[GRASS-dev] WIKI Python script example won't run

I wanted to see how it would be to port one of the bash scripts to Python. I followed the WIKI template and it didn't run. So I made a script that is ONLY from the WIKI template and it doesn't run either. I've tried it by just typing the script into the command prompt and also by typing:

python $GISBASE/scripts/pytest.py

The error is the same every time and is rather strange:

Line too long or missing newline at line 75

Line 75 is the last line (blank) of the script. If I get rid of this line, I still get the error, but on line 74.

I'll paste in the WIKI script below so you won't have to look it up. It seems OK and the error is kind of enigmatic. I've tried various permutations and get the same results.

Michael

================ WIKI script ===================

#!/usr/bin/python
############################################################################
#
# MODULE: g.example
#
# AUTHOR(S): Jane GrassUser
#
# PURPOSE: Demonstrates GRASS parser usage in a Python script
#
# COPYRIGHT: (c) 2007 The GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
############################################################################
#
# REQUIREMENTS:
# - footool: http://www.example.org

#%Module
#% description: g.parser test script
#% keywords: keyword1, keyword2
#%End
#%flag
#% key: f
#% description: a flag
#%END
#%option
#% key: raster
#% type: string
#% gisprompt: old,cell,raster
#% description: raster input map
#% required : yes
#%end
#%option
#% key: vector
#% type: string
#% gisprompt: old,vector,vector
#% description: vector input map
#% required : yes
#%end
#%option
#% key: option1
#% type: string
#% description: an option
#% required : yes
#%end

import sys
import os

def main():

     #### add your code here ####

     print ""

     if ( os.getenv("GIS_FLAG_F") == "1" ):
         print "Flag -f set"
     else:
         print "Flag -f not set"

     print "Value of GIS_OPT_OPTION1: %s" % os.getenv("GIS_OPT_OPTION1")
     print "Value of GIS_OPT_RASTER: %s" % os.getenv("GIS_OPT_RASTER")
     print "Value of GIS_OPT_VECTOR: %s" % os.getenv("GIS_OPT_VECTOR")

     #### end of your code ####
     return

if __name__ == "__main__":
     if ( len(sys.argv) <= 1 or sys.argv[1] != "@ARGS_PARSED@" ):
         os.execvp("g.parser", [sys.argv[0]] + sys.argv)
     else:
  main();

I've found the answer to the question below, but I'm not sure that I understand. I've used the same editor for all my Python coding for awhile now. It is nice and clean, keeping to indentation standards, etc. I turned on invisible characters and there is a linefeed after every line.

However, if I pass the script through another editor and save it, I don't get this error about a missing line feed.

I'm not sure what is going on. I got the missing linefeed error when I dropped the WIKI code directly into the editor and saved it.

Michael

On Jul 16, 2008, at 10:38 PM, Michael Barton wrote:

I wanted to see how it would be to port one of the bash scripts to Python. I followed the WIKI template and it didn't run. So I made a script that is ONLY from the WIKI template and it doesn't run either. I've tried it by just typing the script into the command prompt and also by typing:

python $GISBASE/scripts/pytest.py

The error is the same every time and is rather strange:

Line too long or missing newline at line 75

Line 75 is the last line (blank) of the script. If I get rid of this line, I still get the error, but on line 74.

I'll paste in the WIKI script below so you won't have to look it up. It seems OK and the error is kind of enigmatic. I've tried various permutations and get the same results.

Michael

================ WIKI script ===================

#!/usr/bin/python
############################################################################
#
# MODULE: g.example
#
# AUTHOR(S): Jane GrassUser
#
# PURPOSE: Demonstrates GRASS parser usage in a Python script
#
# COPYRIGHT: (c) 2007 The GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
############################################################################
#
# REQUIREMENTS:
# - footool: http://www.example.org

#%Module
#% description: g.parser test script
#% keywords: keyword1, keyword2
#%End
#%flag
#% key: f
#% description: a flag
#%END
#%option
#% key: raster
#% type: string
#% gisprompt: old,cell,raster
#% description: raster input map
#% required : yes
#%end
#%option
#% key: vector
#% type: string
#% gisprompt: old,vector,vector
#% description: vector input map
#% required : yes
#%end
#%option
#% key: option1
#% type: string
#% description: an option
#% required : yes
#%end

import sys
import os

def main():

   #### add your code here ####

   print ""

   if ( os.getenv("GIS_FLAG_F") == "1" ):
       print "Flag -f set"
   else:
       print "Flag -f not set"

   print "Value of GIS_OPT_OPTION1: %s" % os.getenv("GIS_OPT_OPTION1")
   print "Value of GIS_OPT_RASTER: %s" % os.getenv("GIS_OPT_RASTER")
   print "Value of GIS_OPT_VECTOR: %s" % os.getenv("GIS_OPT_VECTOR")

   #### end of your code ####
   return

if __name__ == "__main__":
   if ( len(sys.argv) <= 1 or sys.argv[1] != "@ARGS_PARSED@" ):
       os.execvp("g.parser", [sys.argv[0]] + sys.argv)
   else:
  main();

Michael Barton wrote:

I wanted to see how it would be to port one of the bash scripts to
Python. I followed the WIKI template and it didn't run. So I made a
script that is ONLY from the WIKI template and it doesn't run either.

And the URL is ...?

I've found the answer to the question below, but I'm not sure that I
understand. I've used the same editor for all my Python coding for
awhile now. It is nice and clean, keeping to indentation standards,
etc. I turned on invisible characters and there is a linefeed after
every line.

However, if I pass the script through another editor and save it, I
don't get this error about a missing line feed.

I'm not sure what is going on. I got the missing linefeed error when I
dropped the WIKI code directly into the editor and saved it.

It looks like the text is being pasted with CRLF terminators, and the
editor (or maybe Python itself) is treating both CR and LF as
individual terminators.

--
Glynn Clements <glynn@gclements.plus.com>