[GRASS-SVN] r62214 - in grass/trunk/lib/python/pygrass/raster: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Oct 8 09:32:54 PDT 2014
Author: zarch
Date: 2014-10-08 09:32:54 -0700 (Wed, 08 Oct 2014)
New Revision: 62214
Modified:
grass/trunk/lib/python/pygrass/raster/__init__.py
grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py
Log:
pygrass: Rewrite RasterRow open method
Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py 2014-10-08 14:04:21 UTC (rev 62213)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py 2014-10-08 16:32:54 UTC (rev 62214)
@@ -167,36 +167,30 @@
self.mtype = mtype if mtype else self.mtype
self.overwrite = overwrite if overwrite is not None else self.overwrite
- # check if exist and instantiate all the private attributes
- if self.exist():
- self.info.read()
- self.cats.mtype = self.mtype
- self.cats.read()
- self.hist.read()
- if self.mode == 'r':
- # the map exist, read mode
+ if self.mode == 'r':
+ if self.exist():
+ self.info.read()
+ self.cats.mtype = self.mtype
+ self.cats.read()
+ self.hist.read()
self._fd = libraster.Rast_open_old(self.name, self.mapset)
self._gtype = libraster.Rast_get_map_type(self._fd)
self.mtype = RTYPE_STR[self._gtype]
-# try:
-# self.cats.read(self)
-# self.hist.read(self.name)
-# except:
-# import ipdb; ipdb.set_trace()
- elif self.overwrite:
- if self._gtype is None:
- raise OpenError(_("Raster type not defined"))
- self._fd = libraster.Rast_open_new(self.name, self._gtype)
else:
- str_err = _("Raster map <{0}> already exists")
- raise OpenError(str_err.format(self))
- else:
- # Create a new map
- if self.mode == 'r':
- # check if we are in read mode
str_err = _("The map does not exist, I can't open in 'r' mode")
raise OpenError(str_err)
+ elif self.mode == 'w':
+ if self.exist():
+ if not self.overwrite:
+ str_err = _("Raster map <{0}> already exists"
+ " and will be not overwritten")
+ raise OpenError(str_err.format(self))
+ if self._gtype is None:
+ raise OpenError(_("Raster type not defined"))
self._fd = libraster.Rast_open_new(self.name, self._gtype)
+ else:
+ raise OpenError("Open mode: %r not supported,"
+ " valid mode are: r, w")
# read rows and cols from the active region
self._rows = libraster.Rast_window_rows()
self._cols = libraster.Rast_window_cols()
Modified: grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py 2014-10-08 14:04:21 UTC (rev 62213)
+++ grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py 2014-10-08 16:32:54 UTC (rev 62214)
@@ -1,41 +1,79 @@
# -*- coding: utf-8 -*-
-"""
-Created on Mon Sep 15 13:37:24 2014
-
- at author: lucadelu
-"""
-
+from grass.exceptions import OpenError
from grass.gunittest import TestCase, test
+from unittest import skip
from grass.pygrass.raster import RasterRow
class RasterRowTestCate(TestCase):
- name = 'elevation'
+ @classmethod
+ def setUpClass(cls):
+ """Create a not empty table instance"""
+ from grass.pygrass.modules.shortcuts import general as g
+ cls.name = 'elevation'
+ cls.tmp = 'tmp' + cls.name
+ g.copy(rast=[cls.name, cls.tmp], overwrite=True)
+
+ @classmethod
+ def tearDownClass(cls):
+ """Remove the generated vector map, if exist"""
+ from grass.pygrass.modules.shortcuts import general as g
+ g.remove(type='rast', pattern=cls.tmp, flags='f')
+
def test_type(self):
- eletype = 'FCELL'
r = RasterRow(self.name)
- r.open()
- self.assertTrue(r.mtype, eletype)
+ r.open(mode='r')
+ self.assertTrue(r.mtype,'FCELL')
r.close()
def test_isopen(self):
r = RasterRow(self.name)
self.assertFalse(r.is_open())
- r.open()
+ r.open(mode='r')
self.assertTrue(r.is_open())
r.close()
+ self.assertFalse(r.is_open())
def test_name(self):
r = RasterRow(self.name)
- r.open()
+ r.open(mode='r')
self.assertEqual(r.name, self.name)
fullname = "{name}@{mapset}".format(name=r.name, mapset=r.mapset)
self.assertEqual(r.fullname(), fullname)
r.close()
+ def test_exist(self):
+ notexist = RasterRow(self.name + 'notexist')
+ self.assertFalse(notexist.exist())
+ exist = RasterRow(self.name)
+ self.assertTrue(exist.exist())
+ def test_open_r(self):
+ notexist = RasterRow(self.tmp + 'notexist')
+ with self.assertRaises(OpenError):
+ # raster does not exist
+ notexist.open(mode='r')
+ r = RasterRow(self.name)
+ r.open(mode='r', mtype='DCELL')
+ # ignore the mtype if is open in read mode
+ self.assertEqual(r.mtype, 'FCELL')
+ r.close()
+
+ def test_open_w(self):
+ r = RasterRow(self.tmp)
+ with self.assertRaises(OpenError):
+ # raster type is not defined!
+ r.open(mode='w')
+ with self.assertRaises(OpenError):
+ # raster already exist
+ r.open(mode='w', mtype='DCELL')
+ # open in write mode and overwrite
+ r.open(mode='w', mtype='DCELL', overwrite=True)
+ self.assertTrue(r.mtype, 'DCELL')
+
+
if __name__ == '__main__':
test()
More information about the grass-commit
mailing list