Converting Bash scripts to Python

From GRASS-Wiki
Revision as of 13:03, 17 January 2011 by Landa (talk | contribs) (Created page with "This page contains note for users who convert their Bash script to Python. == User break == In Bash: <source lang=bash> # what to do in case of user break: exitprocedure() { ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page contains note for users who convert their Bash script to Python.

User break

In Bash:

# what to do in case of user break:
exitprocedure()
{
 g.remove rast=$TMP1 > /dev/null
}
# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 15

In Python:

import sys
import atexit

import grass.script as grass

def cleanup():
    grass.run_command('g.remove',
                      rast = tmp1)

def main():
    ...
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()
    atexit.register(cleanup)
    sys.exit(main())