diff flys-backend/contrib/shpimporter/importer.py @ 5006:769593a84606 dami

Importer: Behold, Logging!
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 15 Feb 2013 16:22:13 +0100
parents f1c01fecf194
children c5187ab9f571
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/importer.py	Fri Feb 15 16:19:52 2013 +0100
+++ b/flys-backend/contrib/shpimporter/importer.py	Fri Feb 15 16:22:13 2013 +0100
@@ -4,6 +4,9 @@
     import ogr, osr
 import utils
 import re
+import logging
+
+logger = logging.getLogger("importer")
 
 class Importer:
 
@@ -78,20 +81,20 @@
 
         shp = ogr.Open(shape[1])
         if shp is None:
-            shpimporter.ERROR("Shapefile '%s' could not be opened!" % path)
+            logger.error("Shapefile '%s' could not be opened!" % path)
             return
 
         if not self.isShapeRelevant(name, path):
-            shpimporter.INFO("Skip shapefile: '%s' of Type: %s" % (path,
+            logger.info("Skip shapefile: '%s' of Type: %s" % (path,
                 utils.getWkbString(shp.GetLayerByName(name).GetGeomType())))
             return
 
 
-        shpimporter.INFO("Processing shapefile '%s'" % path)
+        logger.info("Processing shapefile '%s'" % path)
         srcLayer = shp.GetLayerByName(name)
 
         if srcLayer is None:
-            shpimporter.ERROR("Layer '%s' was not found!" % name)
+            logger.error("Layer '%s' was not found!" % name)
             return
 
         return self.shape2Database(srcLayer, name, path)
@@ -101,7 +104,7 @@
         src_srs  = geometry.GetSpatialReference()
 
         if src_srs is None:
-            shpimporter.ERROR("No source SRS given! No transformation possible!")
+            logger.error("No source SRS given! No transformation possible!")
             return feat
 
         transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
@@ -158,15 +161,15 @@
         destLayer = self.dbconn.GetLayerByName(self.getTablename())
 
         if srcLayer is None:
-            shpimporter.ERROR("Shapefile is None!")
+            logger.error("Shapefile is None!")
             return -1
 
         if destLayer is None:
-            shpimporter.ERROR("No destination layer given!")
+            logger.error("No destination layer given!")
             return -1
 
         count = srcLayer.GetFeatureCount()
-        shpimporter.DEBUG("Try to add %i features to database." % count)
+        logger.debug("Try to add %i features to database." % count)
 
         srcLayer.ResetReading()
         self.srcLayer = srcLayer
@@ -181,7 +184,7 @@
             geom     = feat.GetGeometryRef()
 
             if geom is None:
-                shpimporter.DEBUG("Unkown Geometry reference for feature")
+                logger.debug("Unkown Geometry reference for feature")
                 continue
 
             geomType = geom.GetGeometryType()
@@ -198,11 +201,11 @@
                     if newFeat:
                         res = destLayer.CreateFeature(newFeat)
                         if res is None or res > 0:
-                            shpimporter.ERROR("Unable to insert feature. Error: %r" % res)
+                            logger.error("Unable to insert feature. Error: %r" % res)
                         else:
                             success = success + 1
                     else:
-                        shpimporter.ERROR("Could not transform feature: %s " % feat.GetFID())
+                        logger.error("Could not transform feature: %s " % feat.GetFID())
                         creationFailed += 1
                 else:
                     creationFailed = creationFailed + 1
@@ -210,10 +213,10 @@
                 unsupported[utils.getWkbString(geomType)] = \
                         unsupported.get(utils.getWkbString(geomType), 0) + 1
 
-        shpimporter.INFO("Inserted %i features" % success)
-        shpimporter.INFO("Failed to create %i features" % creationFailed)
+        logger.info("Inserted %i features" % success)
+        logger.info("Failed to create %i features" % creationFailed)
         for key, value in unsupported.items():
-            shpimporter.INFO("Found %i unsupported features of type: %s" % (value, key))
+            logger.info("Found %i unsupported features of type: %s" % (value, key))
 
         if self.tracking_import:
             unhandled = []
@@ -223,7 +226,7 @@
                     unhandled.append(act_field)
 
             if len(unhandled):
-                shpimporter.INFO("Did not import values from fields: %s " % \
+                logger.info("Did not import values from fields: %s " % \
                         " ".join(unhandled))
 
         try:
@@ -231,6 +234,6 @@
                 return geomType
             destLayer.CommitTransaction()
         except e:
-            shpimporter.ERROR("Exception while committing transaction.")
+            logger.error("Exception while committing transaction.")
 
         return geomType

http://dive4elements.wald.intevation.org