[GRASS-user] Definintion of cleanup in pyhton script

Johannes Radinger JRadinger at gmx.at
Thu Mar 1 06:08:54 EST 2012


Hi GRASS users,

maybe someone can help me with the cleanup process in a
GRASS python script I am writing. The script uses the 
gparser functionality to get the needed files etc via
the GUI.

I also try to include a proper cleanup process within
the script which deletes all the tmp maps created. This
should also work if the script stops (e.g due to an error).
So I think I FIRST need to define all the files which
should be removed. The problem is that my script includes
a while loop (iteration) so I can't really say which
files will be created in advance. How can I define a proper
cleanup in such cases?

The script looks at the moment like:

#!/usr/bin/env python
...
import grass.script as grass

tmp_rast = None
tmp_vect = None

def cleanup():
	if (tmp_rast):
		grass.run_command("g.remove", 
				rast = tmp_map_rast,
				quiet = True)

def main():

	max_count = 5 # maximum number of iterations

	#global variables for cleanup - standard values
	global tmp_rast
	tmp_rast = [f + str(os.getpid()) for f in
		(["r%d_tmp_" % x for x in range(max_count+1)]+
		["rt%d_tmp_" % x for x in range(max_count+1)]+
		["r_diff%d_tmp_" % x for x in range(max_count+1)])]

	...
	diff = 9999
	while (diff is not None):
	#In this while loop all the tmp-files are created
	...

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


How can I improve this? Maybe someone of you is more experienced than I am.
Thank you so much!

Best regards,
Johannes





-- 
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!                                  
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a


More information about the grass-user mailing list