From d0a05af9c69b15a156d50388f5e7dfff588618aa Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@gmail.com>
Date: Mon, 13 Mar 2023 19:44:39 +0000
Subject: [PATCH] Now inheriting *date from parent survexblock

---
 parsers/survex.py | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/parsers/survex.py b/parsers/survex.py
index 763bcf1..1f7859b 100644
--- a/parsers/survex.py
+++ b/parsers/survex.py
@@ -297,6 +297,8 @@ class LoadingSurvex:
     slengthstack = []
     teaminheritstack = []
     teamcurrentstack = []
+    dateinheritstack = []
+    datecurrentstack = []
     stackbegin = []
     flagsstack = []
     datastack = []
@@ -331,6 +333,8 @@ class LoadingSurvex:
     caverndate = None
     currentteam = set()
     inheritteam = set()
+    currentdate = set()
+    inheritdate = set()
     pending = []
     adhocload = False
 
@@ -376,10 +380,39 @@ class LoadingSurvex:
             # )
             return self.inheritteam
             
+    def fix_undated(self, survexblock):
+        """Called when we reach *end of a block
+        Checks to see if the block has no *date, in which case it uses the
+        inherited date.
+        This is fine if the inherited date is from the same SurvexFile,
+        but inheriting dates across *include files is almost certainly NOT
+        expected behaviour, even though it is syntactically "correct".
+        """
+        if survexblock.parent.name == "troggle_unseens":
+            # Bolluxed up if we try to inherit from this random junk, so don't.
+            return
+            
+        if not self.currentdate: 
+            if self.inheritdate:
+                message = (
+                    f"- INHERITING ({survexblock.parent})>({survexblock}) {survexblock.survexfile.path} '{self.inheritdate:%Y-%m-%d}'"
+                )
+                print(self.insp + message)
+                stash_data_issue(
+                    parser="survex", message=message, url=None, sb=(survexblock.survexfile.path)
+                )
+                survexblock.date = self.inheritdate
+                return self.inheritdate
+        return
+
     def fix_anonymous(self, survexblock):
         """Called when we reach *end of a block
         Checks to see if the block has no team attached, in which case it uses the
         inherited team.
+        This is fine if the inherited team is from the same SurvexFile,
+        but inheriting team across *include files is almost certainly NOT
+        expected behaviour, even though it is syntactically "correct".
+
         If the block has no date, then it is assumed to be an  abstract container,
         with no relevant team, and anyway we can't attach a PersonExpedition without 
         knowing the year. Unless its parent has an identified expo"""
@@ -642,6 +675,10 @@ class LoadingSurvex:
             # raise
             
         setdate_on_survexblock(year)
+        if survexblock.date:
+            # do not actually need a distict variable 'currentdate' but it makes the code clearer
+            self.currentdate = survexblock.date
+
 
     def LoadSurvexLeg(self, survexblock, sline, comment, svxline):
         """This reads compass, clino and tape data but only keeps the tape lengths,
@@ -1266,7 +1303,7 @@ class LoadingSurvex:
         if survexblock.date:
             expoyear = str(survexblock.date.year)
         else:
-            message = f" ! No survexblock.date in'{survexblock.survexfile.path}', setting to 1976"
+            message = f" ! No survexblock.date in {survexblock} in '{survexblock.survexfile.path}', setting to 1976"
             print(insp + message)
             stash_data_issue(
                 parser="survex", message=message, url=None, sb=(survexblock.survexfile.path)
@@ -1528,6 +1565,8 @@ class LoadingSurvex:
                 self.slengthstack.append(self.slength)
                 self.teaminheritstack.append(self.inheritteam) 
                 self.teamcurrentstack.append(self.currentteam) 
+                self.dateinheritstack.append(self.inheritdate) 
+                self.datecurrentstack.append(self.currentdate) 
                 pushblock()
                 # PUSH state ++++++++++++++
                 self.legsnumber = 0
@@ -1535,6 +1574,8 @@ class LoadingSurvex:
                 self.units = "metres"
                 self.inheritteam = self.currentteam 
                 self.currentteam = set() # zero the current team when we start a new block
+                self.inheritdate = self.currentdate 
+                self.currentdate = set() # zero the current date when we start a new block
                 printbegin()
                 newsurvexblock = SurvexBlock(
                     name=blkid,
@@ -1560,6 +1601,7 @@ class LoadingSurvex:
                 slengthtotal += self.slength
                 nlegstotal += self.legsnumber
                 
+                self.fix_undated(survexblock)
                 self.fix_anonymous(survexblock)
                 try:
                     survexblock.parent.save()  # django insists on this although it is already saved !?
@@ -1576,6 +1618,8 @@ class LoadingSurvex:
                 popblock()
                 self.inheritteam = self.teaminheritstack.pop() 
                 self.currentteam = self.teamcurrentstack.pop()
+                self.inheritdate = self.dateinheritstack.pop() 
+                self.currentdate = self.datecurrentstack.pop()
                 self.legsnumber = self.legsnumberstack.pop()
                 self.units, self.unitsfactor = self.unitsstack.pop()
                 self.slength = self.slengthstack.pop()