From 79a31a41f979e8ae51b5f2a4a308542bf54a887e Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@gmail.com>
Date: Sat, 11 Apr 2020 00:36:27 +0100
Subject: [PATCH] Fixed bad import of surveyscans references from tunnel files

---
 databaseReset.py   |  8 ++++----
 parsers/surveys.py | 19 ++++++++++++-------
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/databaseReset.py b/databaseReset.py
index b81a640..1eed075 100644
--- a/databaseReset.py
+++ b/databaseReset.py
@@ -102,10 +102,10 @@ def reset():
     import_logbooks()
     import_QMs()
     import_survex()
-    try:
-        import_tunnelfiles()
-    except:
-        print("Tunnel files parser broken.")
+    #try:
+    import_tunnelfiles()
+    #except:
+    #   print("Tunnel files parser broken.")
     
     import_surveys()
 
diff --git a/parsers/surveys.py b/parsers/surveys.py
index be8a368..9bd063d 100644
--- a/parsers/surveys.py
+++ b/parsers/surveys.py
@@ -243,7 +243,7 @@ def LoadListScans():
 
 def FindTunnelScan(tunnelfile, path):
     scansfolder, scansfile = None, None
-    mscansdir = re.search(r"(\d\d\d\d#X?\d+\w?|1995-96kh|92-94Surveybookkh|1991surveybook|smkhs)/(.*?(?:png|jpg))$", path)
+    mscansdir = re.search(r"(\d\d\d\d#X?\d+\w?|1995-96kh|92-94Surveybookkh|1991surveybook|smkhs)/(.*?(?:png|jpg|pdf|jpeg))$", path)
     if mscansdir:
         scansfolderl = SurvexScansFolder.objects.filter(walletname=mscansdir.group(1))
         if len(scansfolderl):
@@ -252,9 +252,11 @@ def FindTunnelScan(tunnelfile, path):
         if scansfolder:
             scansfilel = scansfolder.survexscansingle_set.filter(name=mscansdir.group(2))
             if len(scansfilel):
-                print(scansfilel, len(scansfilel))
-                print "ttt", tunnelfile.tunnelpath, path
-                assert len(scansfilel) == 1
+                if len(scansfilel) > 1:
+                    print "BORK more than one image filename matches filter query. ", scansfilel[0]
+                    print "BORK ", tunnelfile.tunnelpath, path
+                    print "BORK ", mscansdir.group(1),  mscansdir.group(2), len(scansfilel)
+                #assert len(scansfilel) == 1
                 scansfile = scansfilel[0]
             
         if scansfolder:
@@ -262,9 +264,9 @@ def FindTunnelScan(tunnelfile, path):
         if scansfile:
             tunnelfile.survexscans.add(scansfile)
     
-    elif path and not re.search(r"\.(?:png|jpg|jpeg)$(?i)", path):
+    elif path and not re.search(r"\.(?:png|jpg|pdf|jpeg)$(?i)", path):
         name = os.path.split(path)[1]
-        print("ttt", tunnelfile.tunnelpath, path, name)
+        #print("debug-tunnelfileobjects ", tunnelfile.tunnelpath, path, name)
         rtunnelfilel = TunnelFile.objects.filter(tunnelname=name)
         if len(rtunnelfilel):
             assert len(rtunnelfilel) == 1, ("two paths with name of", path, "need more discrimination coded")
@@ -281,8 +283,11 @@ def SetTunnelfileInfo(tunnelfile):
     fin = open(ff)
     ttext = fin.read()
     fin.close()
-    
+    if tunnelfile.filesize <= 0:
+        print "DEBUG - zero length xml file", ff
+        return
     mtype = re.search("<(fontcolours|sketch)", ttext)
+     
     assert mtype, ff
     tunnelfile.bfontcolours = (mtype.group(1)=="fontcolours")
     tunnelfile.npaths = len(re.findall("<skpath", ttext))