[Dive4elements-commits] [PATCH 02 of 15] Improve error handling and unify dbconn for all importers

Wald Commits scm-commit at wald.intevation.org
Mon Jan 28 12:27:29 CET 2013


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1358951409 -3600
# Node ID a563e9f58f93f67402a4a45e1af5ff3ac3f9f1de
# Parent  890eaa0a5162993c0e9d113fb69cb592af5fb44f
Improve error handling and unify dbconn for all importers

diff -r 890eaa0a5162 -r a563e9f58f93 flys-backend/contrib/shpimporter/importer.py
--- a/flys-backend/contrib/shpimporter/importer.py	Wed Jan 23 11:51:14 2013 +0100
+++ b/flys-backend/contrib/shpimporter/importer.py	Wed Jan 23 15:30:09 2013 +0100
@@ -1,19 +1,19 @@
-import ogr, osr
+try:
+    from osgeo import ogr
+except ImportErrror:
+    import ogr
+import osr
 import shpimporter
 
 class Importer:
 
-    def __init__(self, config):
+    def __init__(self, config, dbconn):
         self.config = config
-        if config.ogr_connection:
-            self.dbconn = '%s' % config.ogr_connection
-        else:
-            self.dbconn = 'OCI:%s/%s@%s' % (config.user, config.password, config.host)
+        self.dbconn = dbconn
         self.river_id = config.river_id
         self.dest_srs = osr.SpatialReference()
         self.dest_srs.ImportFromEPSG(config.target_srs)
 
-
     def getKind(self, path):
         raise NotImplementedError("Importer.getKind is abstract!")
 
@@ -86,12 +86,7 @@
 
 
     def shape2Database(self, srcLayer, name, path):
-        table     = ogr.Open(self.dbconn)
-        if not table:
-            shpimporter.ERROR("Could not connect to database %s" % self.dbconn)
-            return -1
-
-        destLayer = table.GetLayerByName(self.getTablename())
+        destLayer = self.dbconn.GetLayerByName(self.getTablename())
 
         if srcLayer is None:
             shpimporter.ERROR("Shapefile is None!")
@@ -127,11 +122,11 @@
                                                 path=path)
 
                 if newFeat is not None:
-		    newFeat.SetField("path", path)
+                    newFeat.SetField("path", path)
                     newFeat = self.transform(newFeat)
                     res = destLayer.CreateFeature(newFeat)
                     if res is None or res > 0:
-                        shpimporter.ERROR("Unable to insert feature: %r" % res)
+                        shpimporter.ERROR("Unable to insert feature. Error: %r" % res)
                     else:
                         success = success + 1
                 else:
diff -r 890eaa0a5162 -r a563e9f58f93 flys-backend/contrib/shpimporter/shpimporter.py
--- a/flys-backend/contrib/shpimporter/shpimporter.py	Wed Jan 23 11:51:14 2013 +0100
+++ b/flys-backend/contrib/shpimporter/shpimporter.py	Wed Jan 23 15:30:09 2013 +0100
@@ -1,4 +1,7 @@
-import ogr
+try:
+    from osgeo import ogr
+except ImportErrror:
+    import ogr
 
 import utils, optparse
 
@@ -35,21 +38,21 @@
     print "ERROR: %s" % msg
 
 
-def getImporters(config):
+def getImporters(config, dbconn):
     return [
-        Axis(config),
-        KM(config),
-        CrosssectionTrack(config),
-        Line(config),
-        Fixpoint(config),
-        Building(config),
-        Floodplain(config),
-        HydrBoundary(config),
-        HydrBoundaryPoly(config),
-        HWS(config),
-        GaugeLocation(config),
-        Catchment(config),
-        UESG(config)
+        Axis(config, dbconn),
+        KM(config, dbconn),
+        CrosssectionTrack(config, dbconn),
+        Line(config, dbconn),
+        Fixpoint(config, dbconn),
+        Building(config, dbconn),
+        Floodplain(config, dbconn),
+        HydrBoundary(config, dbconn),
+        HydrBoundaryPoly(config, dbconn),
+        HWS(config, dbconn),
+        GaugeLocation(config, dbconn),
+        Catchment(config, dbconn),
+        UESG(config, dbconn)
         ]
 
 
@@ -129,12 +132,12 @@
     return False
 
 
-def parse():
+def main():
     config=None
     try:
         config = getConfig()
     except:
-        return
+        return -1
 
     if config == None:
         ERROR("Unable to read config from command line!")
@@ -143,7 +146,16 @@
     if config.dry_run > 0:
         INFO("You enable 'dry_run'. No database transaction will take place!")
 
-    importers = getImporters(config)
+    if config.ogr_connection:
+        dbconn = ogr.Open(config.ogr_connection)
+    else:
+        dbconn = ogr.Open('OCI:%s/%s@%s' % (config.user, config.password, config.host))
+
+    if not dbconn:
+        shpimporter.ERROR("Could not connect to database %s" % self.dbconn)
+        return -1
+
+    importers = getImporters(config, dbconn)
     types = {}
 
     for importer in importers:
@@ -170,4 +182,4 @@
 
 
 if __name__ == '__main__':
-    parse()
+    main()


More information about the Dive4elements-commits mailing list