summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--parsers/survex.py262
1 files changed, 153 insertions, 109 deletions
diff --git a/parsers/survex.py b/parsers/survex.py
index fc96fb8..b0b0a8e 100644
--- a/parsers/survex.py
+++ b/parsers/survex.py
@@ -583,14 +583,18 @@ class LoadingSurvex():
self.currentsurvexfile.save() # django insists on this although it is already saved !?
self.datastar = copy.deepcopy(self.datastardefault)
- #self.datastack.append(self.datastar) # and extra push will do it ?
-
self.flagsstar = copy.deepcopy(self.flagsdefault)
- #self.flagsstack.append(self.flagsstar)
+ blkid = None
+ pathlist = None
+ args = None
+ previousnlegs = None
+ oldflags = None
blockcount = 0
self.lineno = 0
+
def tickle():
nonlocal blockcount
+
blockcount +=1
if blockcount % 10 ==0 :
print(".", file=sys.stderr,end='')
@@ -600,6 +604,151 @@ class LoadingSurvex():
print(" ", file=sys.stderr,end='')
sys.stderr.flush()
+ def printbegin():
+ nonlocal blkid
+ nonlocal pathlist
+
+ depth = " " * self.depthbegin
+ print("{:2}{} - Begin for :'{}'".format(self.depthbegin,depth, blkid))
+ pathlist = ""
+ for id in self.stackbegin:
+ if len(id) > 0:
+ pathlist += "." + id
+
+ def printend():
+ nonlocal args
+ nonlocal previousnlegs
+
+ depth = " " * self.depthbegin
+ print("{:2}{} - End from:'{}'".format(self.depthbegin,depth,args))
+ legsinblock = self.survexlegsnumber - previousnlegs
+ print("{:2}{} - LEGS: {} (previous: {}, now:{})".format(self.depthbegin,
+ depth, legsinblock, previousnlegs, self.survexlegsnumber))
+ survexblock.legsall = legsinblock
+
+ def pushblock():
+ nonlocal blkid
+
+ print(" # datastack at 1 *begin {} 'type':".format(blkid), end="")
+ for dict in self.datastack:
+ print("'{}' ".format(dict["type"].upper()), end="")
+ print("")
+ print("'{}' self.datastar ".format(self.datastar["type"].upper()))
+ # ------------ * DATA
+ self.datastack.append(copy.deepcopy(self.datastar))
+ # ------------ * DATA
+ print(" # datastack at 2 *begin {} 'type':".format(blkid), end="")
+ for dict in self.datastack:
+ print("'{}' ".format(dict["type"].upper()), end="")
+ print("")
+ print("'{}' self.datastar ".format(self.datastar["type"].upper()))
+ # ------------ * FLAGS
+ self.flagsstack.append(copy.deepcopy(self.flagsstar))
+ # ------------ * FLAGS
+
+ def popblock():
+ nonlocal blkid
+ nonlocal oldflags
+
+ print(" # datastack at *end '{} 'type':".format(blkid), end="")
+ for dict in self.datastack:
+ print("'{}' ".format(dict["type"].upper()), end="")
+ print("")
+ print("'{}' self.datastar ".format(self.datastar["type"].upper()))
+ # ------------ * DATA
+ self.datastar = copy.deepcopy(self.datastack.pop())
+ # ------------ * DATA
+ print(" # datastack after *end '{} 'type':".format(blkid), end="")
+ for dict in self.datastack:
+ print("'{}' ".format(dict["type"].upper()), end="")
+ print("")
+ print("'{}' self.datastar ".format(self.datastar["type"].upper()))
+ # ------------ * FLAGS
+ self.flagsstar = copy.deepcopy(self.flagsstack.pop())
+ # ------------ * FLAGS
+ if oldflags["any"] != self.flagsstar["any"]:
+ print(" # POP 'any' flag now:'{}' was:{} ".format(self.flagsstar["any"], oldflags["any"]))
+
+ def starstatement(mstar):
+ nonlocal survexblock
+ nonlocal blkid
+ nonlocal pathlist
+ nonlocal args
+ nonlocal previousnlegs
+ nonlocal oldflags
+
+ cmd, args = mstar.groups()
+ cmd = cmd.lower()
+
+ # ------------------------BEGIN
+ if re.match("begin$(?i)", cmd):
+ blkid = args.lower()
+ # PUSH state ++++++++++++++
+ self.stackbegin.append(blkid)
+ pushblock()
+ # PUSH state ++++++++++++++
+ previousnlegs = self.survexlegsnumber
+ printbegin()
+ newsurvexblock = models_survex.SurvexBlock(name=blkid, parent=survexblock,
+ survexpath=pathlist,
+ cave=self.currentcave, survexfile=self.currentsurvexfile,
+ legsall=0, legssplay=0, legssurfc=0, totalleglength=0.0)
+ newsurvexblock.save()
+ newsurvexblock.title = "("+survexblock.title+")" # copy parent inititally
+ survexblock = newsurvexblock
+ survexblock.save() # django insists on this , but we want to save at the end !
+ tickle()
+
+ # ---------------------------END
+ elif re.match("end$(?i)", cmd):
+ printend()
+ try:
+ survexblock.parent.save() # django insists on this although it is already saved !?
+ except:
+ print(survexblock.parent, file=sys.stderr)
+ raise
+ try:
+ survexblock.save() # save to db at end of block
+ except:
+ print(survexblock, file=sys.stderr)
+ raise
+ # POP state ++++++++++++++
+ popblock()
+ blkid = self.stackbegin.pop()
+ # POP state ++++++++++++++
+ self.currentsurvexblock = survexblock.parent
+ survexblock = survexblock.parent
+ oldflags = self.flagsstar
+ self.depthbegin -= 1
+
+ # -----------------------------
+ elif re.match("(?i)title$", cmd):
+ survexblock.title = args # block has own title, overwrite that from parent
+ elif re.match("(?i)ref$", cmd):
+ self.LoadSurvexRef(survexblock, args)
+ elif re.match("(?i)flags$", cmd):
+ oldflags = self.flagsstar
+ self.LoadSurvexFlags(args)
+ if oldflags["any"] != self.flagsstar["any"]:
+ print(" # CHANGE 'any' flag now:'{}' was:{} ".format(self.flagsstar["any"], oldflags["any"]))
+
+ elif re.match("(?i)data$", cmd):
+ self.LoadSurvexDataCmd(survexblock, args)
+ elif re.match("(?i)date$", cmd):
+ self.LoadSurvexDate(survexblock, args)
+ elif re.match("(?i)team$", cmd):
+ self.LoadSurvexTeam(survexblock, args)
+ elif re.match("(?i)set$", cmd) and re.match("(?i)names", args):
+ pass
+ elif re.match("(?i)include$", cmd):
+ message = " ! -ERROR *include command not expected here {}. Re-run a full Survex import.".format(path)
+ print(message)
+ print(message,file=sys.stderr)
+ models.DataIssue.objects.create(parser='survex', message=message)
+ else:
+ self.LoadSurvexIgnore(survexblock, args, cmd)
+
+
for svxline in svxlines:
self.lineno += 1
sline, comment = self.rx_comment.match(svxline).groups()
@@ -613,112 +762,7 @@ class LoadingSurvex():
# detect a star command
mstar = self.rx_star.match(sline)
if mstar: # yes we are reading a *cmd
- cmd, args = mstar.groups()
- cmd = cmd.lower()
-
- # ------------------------BEGIN
- if re.match("begin$(?i)", cmd):
- depth = " " * self.depthbegin
- blkid = args.lower()
- self.stackbegin.append(blkid)
- # PUSH state ++++++++++++++
- print(" # datastack at 1 *begin {} 'type':".format(blkid), end="")
- for dict in self.datastack:
- print("'{}' ".format(dict["type"].upper()), end="")
- print("")
- print("'{}' self.datastar ".format(self.datastar["type"].upper()))
- self.flagsstack.append(copy.deepcopy(self.flagsstar))
- self.datastack.append(copy.deepcopy(self.datastar))
- print(" # datastack at 2 *begin {} 'type':".format(blkid), end="")
- for dict in self.datastack:
- print("'{}' ".format(dict["type"].upper()), end="")
- print("")
- print("'{}' self.datastar ".format(self.datastar["type"].upper()))
- # PUSH state ++++++++++++++
-
- previousnlegs = self.survexlegsnumber
- print("{:2}{} - Begin for :'{}'".format(self.depthbegin,depth, blkid))
- pathlist = ""
- for id in self.stackbegin:
- if len(id) > 0:
- pathlist += "." + id
- newsurvexblock = models_survex.SurvexBlock(name=blkid, parent=survexblock,
- survexpath=pathlist,
- cave=self.currentcave, survexfile=self.currentsurvexfile,
- legsall=0, legssplay=0, legssurfc=0, totalleglength=0.0)
- newsurvexblock.save()
- newsurvexblock.title = "("+survexblock.title+")" # copy parent inititally
- survexblock = newsurvexblock
- survexblock.save() # django insists on this , but we want to save at the end !
- tickle()
-
- # ---------------------------END
- elif re.match("end$(?i)", cmd):
- depth = " " * self.depthbegin
-
- print("{:2}{} - End from:'{}'".format(self.depthbegin,depth,args))
- legsinblock = self.survexlegsnumber - previousnlegs
- print("{:2}{} - LEGS: {} (previous: {}, now:{})".format(self.depthbegin,
- depth,legsinblock,previousnlegs,self.survexlegsnumber))
- survexblock.legsall = legsinblock
- try:
- survexblock.parent.save() # django insists on this although it is already saved !?
- except:
- print(survexblock.parent, file=sys.stderr)
- raise
- try:
- survexblock.save() # save to db at end of block
- except:
- print(survexblock, file=sys.stderr)
- raise
- print(" # datastack at *end '{} 'type':".format(blkid), end="")
- for dict in self.datastack:
- print("'{}' ".format(dict["type"].upper()), end="")
- print("")
- print("'{}' self.datastar ".format(self.datastar["type"].upper()))
- # POP state ++++++++++++++
- self.datastar = copy.deepcopy(self.datastack.pop())
- print(" # datastack after *end '{} 'type':".format(blkid), end="")
- for dict in self.datastack:
- print("'{}' ".format(dict["type"].upper()), end="")
- print("")
- print("'{}' self.datastar ".format(self.datastar["type"].upper()))
- self.flagsstar = copy.deepcopy(self.flagsstack.pop())
- if oldflags["any"] != self.flagsstar["any"]:
- print(" # POP 'any' flag now:'{}' was:{} ".format(self.flagsstar["any"], oldflags["any"]))
- # POP state ++++++++++++++
- self.currentsurvexblock = survexblock.parent
- survexblock = survexblock.parent
- blkid = self.stackbegin.pop()
- oldflags = self.flagsstar
- self.depthbegin -= 1
-
- # -----------------------------
- elif re.match("(?i)title$", cmd):
- survexblock.title = args # block has own title, overwrite that from parent
- elif re.match("(?i)ref$", cmd):
- self.LoadSurvexRef(survexblock, args)
- elif re.match("(?i)flags$", cmd):
- oldflags = self.flagsstar
- self.LoadSurvexFlags(args)
- if oldflags["any"] != self.flagsstar["any"]:
- print(" # CHANGE 'any' flag now:'{}' was:{} ".format(self.flagsstar["any"], oldflags["any"]))
-
- elif re.match("(?i)data$", cmd):
- self.LoadSurvexDataCmd(survexblock, args)
- elif re.match("(?i)date$", cmd):
- self.LoadSurvexDate(survexblock, args)
- elif re.match("(?i)team$", cmd):
- self.LoadSurvexTeam(survexblock, args)
- elif re.match("(?i)set$", cmd) and re.match("(?i)names", args):
- pass
- elif re.match("(?i)include$", cmd):
- message = " ! -ERROR *include command not expected here {}. Re-run a full Survex import.".format(path)
- print(message)
- print(message,file=sys.stderr)
- models.DataIssue.objects.create(parser='survex', message=message)
- else:
- self.LoadSurvexIgnore(survexblock, args, cmd)
+ starstatement(mstar)
else: # not a *cmd so we are reading data OR rx_comment failed
self.LoadSurvexLineLeg(survexblock, sline, comment)