[GRASS-SVN] r68119 - grass-addons/grass7/raster/r.recode.attr

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Mar 23 02:12:28 PDT 2016


Author: pvanbosgeo
Date: 2016-03-23 02:12:28 -0700 (Wed, 23 Mar 2016)
New Revision: 68119

Modified:
   grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py
Log:
r.recode.attr addon: fix how temp txt files are removed + cleaning up code

Modified: grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py
===================================================================
--- grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py	2016-03-23 09:09:27 UTC (rev 68118)
+++ grass-addons/grass7/raster/r.recode.attr/r.recode.attr.py	2016-03-23 09:12:28 UTC (rev 68119)
@@ -44,7 +44,7 @@
 #% multiple: no
 #%end
 
-#%option G_OPT_F_OUTPUT
+#%option G_OPT_F_INPUT
 #% key: rules
 #% label: Full path to rules file
 #% required: yes
@@ -59,36 +59,16 @@
 import os
 import sys
 import numpy as np
-from tempfile import gettempdir, gettempprefix
-from os.path import join
-from string import ascii_lowercase, digits
-from random import choice
+import tempfile
 import grass.script as grass
 
-def cleanup():
-	grass.run_command('g.remove',
-      type = 'raster',
-      pattern = 'tmp_map',
-      flags='f',
-      quiet = True)
-
-def CreateFileName():
-    (suffix1, suffix2) = ("", "")
-    for _ in xrange( 8 ):
-        suffix1 += choice( ascii_lowercase )
-        suffix2 += choice( digits )
-    flname = join(gettempdir(), '-'.join([gettempprefix(), suffix1, suffix2 ]))
-    while os.path.isfile(flname):
-        flname = flname + "1"
-    return flname
-
 # main function
 def main():
 
     # check if GISBASE is set
     if "GISBASE" not in os.environ:
     # return an error advice
-       grass.fatal(_("You must be in GRASS GIS to run this program"))
+       grass.fatal("You must be in GRASS GIS to run this program")
 
     # input raster map and parameters
     inputmap = options['input']
@@ -109,7 +89,8 @@
     for x in numVar:
         y = x + 1
         myRecode = np.column_stack((myData[:,0], myData[:,0], myData[:,y]))
-        tmpname = CreateFileName()
+
+        fd1, tmpname = tempfile.mkstemp()
         np.savetxt(tmpname, myRecode, delimiter=":")
 
         if len(numVar) == lengthNames:
@@ -117,6 +98,11 @@
         else:
             nmOutput = outNames[0] + '_' + nmsData[y]
 
+        cf = grass.find_file(name=nmOutput, element = 'cell',
+                          mapset=grass.gisenv()['MAPSET'])
+        if cf['fullname'] != '':
+            grass.fatal("The layer " + nmOutput + " already exist in this mapset")
+
         if flag_a:
             grass.run_command('r.recode',
                     input = inputmap,
@@ -128,6 +114,7 @@
                     input = inputmap,
                     output = nmOutput,
                     rules = tmpname)
+        os.close(fd1)
         os.remove(tmpname)
 
 



More information about the grass-commit mailing list