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

Importer: Behold, Logging!
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 15 Feb 2013 16:22:13 +0100
parents eb2d6609387c
children 514948fdae54
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/shpimporter.py	Fri Feb 15 16:19:52 2013 +0100
+++ b/flys-backend/contrib/shpimporter/shpimporter.py	Fri Feb 15 16:22:13 2013 +0100
@@ -6,6 +6,7 @@
 import utils, optparse
 import sys
 import os
+import logging
 
 from uesg  import UESG
 from axis  import Axis
@@ -20,25 +21,16 @@
 from catchments import Catchment
 from dgm import insertRiverDgm
 
-
-VERBOSE_DEBUG=2
-VERBOSE_INFO=1
-
+logger = logging.getLogger("shpimporter")
 
-def DEBUG(msg):
-    config = getConfig()
-    if config.verbose >= VERBOSE_DEBUG:
-        print "DEBUG: %s" % msg
-
-def INFO(msg):
-    config = getConfig()
-    if config.verbose >= VERBOSE_INFO:
-        print "INFO: %s" % msg
-
-def ERROR(msg):
-    config = getConfig()
-    print "ERROR: %s" % msg
-
+def initialize_logging(level):
+    """Initializes the logging system"""
+    root = logging.getLogger()
+    root.setLevel(level)
+    hdlr = logging.StreamHandler()
+    fmt = logging.Formatter("%(levelname)s %(name)s: %(message)s")
+    hdlr.setFormatter(fmt)
+    root.addHandler(hdlr)
 
 def getImporters(river_id, dbconn, dry_run):
     return [
@@ -84,18 +76,25 @@
     parser.add_option("--skip_dgm", type="int")
     (config, args) = parser.parse_args()
 
+    if config.verbose > 1:
+        initialize_logging(logging.DEBUG)
+    elif config.verbose == 1:
+        initialize_logging(logging.INFO)
+    else:
+        initialize_logging(logging.WARN)
+
     if config.directory == None:
-        ERROR("No river directory specified!")
+        logger.error("No river directory specified!")
         raise Exception("Invalid config")
     if not config.ogr_connection:
         if not config.host:
-            ERROR("No database host specified!")
+            logger.error("No database host specified!")
             raise Exception("Invalid config")
         if not config.user:
-            ERROR("No databaser user specified!")
+            logger.error("No databaser user specified!")
             raise Exception("Invalid config")
         if not config.password:
-            ERROR("No password specified!")
+            logger.error("No password specified!")
             raise Exception("Invalid config")
 
     return config
@@ -139,11 +138,11 @@
         return -1
 
     if config == None:
-        ERROR("Unable to read config from command line!")
+        logger.error("Unable to read config from command line!")
         return
 
     if config.dry_run > 0:
-        INFO("You enable 'dry_run'. No database transaction will take place!")
+        logger.info("You enable 'dry_run'. No database transaction will take place!")
 
     if config.ogr_connection:
         connstr = config.ogr_connection
@@ -155,7 +154,7 @@
             import cx_Oracle as dbapi
             raw_connstr=connstr.replace("OCI:", "")
         except ImportErrror:
-            ERROR("Module cx_Oracle not found in: %s\n"
+            logger.error("Module cx_Oracle not found in: %s\n"
                   "Neccessary to connect to a Oracle Database.\n"
                   "Please refer to the installation "
                   "documentation." % sys.path)
@@ -165,7 +164,7 @@
             import psycopg2 as dbapi
             raw_connstr=connstr.replace("PG:", "")
         except ImportError:
-            ERROR("Module psycopg2 not found in: %s\n"
+            logger.error("Module psycopg2 not found in: %s\n"
                   "Neccessary to connect to a Posgresql Database.\n"
                   "Please refer to the installation "
                   "documentation." % sys.path)
@@ -174,7 +173,7 @@
     dbconn = ogr.Open(connstr)
 
     if dbconn == None:
-        ERROR("Could not connect to database %s" % connstr)
+        logger.error("Could not connect to database %s" % connstr)
         return -1
 
     types = {}
@@ -197,21 +196,21 @@
         river_id = utils.getRiverId(dbconn_raw, river_name)
 
         if not river_id:
-            INFO("Could not find river in database. Skipping: %s"
+            logger.info("Could not find river in database. Skipping: %s"
                   % river_name)
             continue
         else:
-            INFO("Importing River: %s" % river_name)
+            logger.info("Importing River: %s" % river_name)
 
         for importer in getImporters(river_id, dbconn, config.dry_run):
             if skip_importer(config, importer):
-                INFO("Skip import of '%s'" % importer.getName())
+                logger.info("Skip import of '%s'" % importer.getName())
                 continue
 
-            INFO("Start import of '%s'" % importer.getName())
+            logger.info("Start import of '%s'" % importer.getName())
 
             shapes = utils.findShapefiles(importer.getPath(config.directory))
-            DEBUG("Found %i Shapefiles" % len(shapes))
+            logger.debug("Found %i Shapefiles" % len(shapes))
 
             for shpTuple in shapes:
                 geomType = importer.walkOverShapes(shpTuple)
@@ -223,17 +222,17 @@
                     types[geomType] = 1
 
         for key in types:
-            DEBUG("%i x geometry type %s" % (types[key], key))
+            logger.debug("%i x geometry type %s" % (types[key], key))
 
         if not config.skip_dgm:
             dgmfilename = os.path.join(
                     config.directory, "..", "DGMs.csv")
             if not os.access(dgmfilename, os.R_OK) or not \
                     os.path.isfile(dgmfilename):
-                INFO("Could not find or access DGM file: %s \n"
+                logger.info("Could not find or access DGM file: %s \n"
                      "Skipping DGM import." % dgmfilename)
             else:
-                INFO("Inserting DGM meta information in 'dem' table.")
+                logger.info("Inserting DGM meta information in 'dem' table.")
                 insertRiverDgm(dbconn_raw, dgmfilename, river_name, config.dry_run)
 
 if __name__ == '__main__':

http://dive4elements.wald.intevation.org