re-parsing survex file when editing

This commit is contained in:
Philip Sargent 2023-03-23 01:17:38 +00:00
parent 70d6d9eb77
commit 562ef48f19
3 changed files with 71 additions and 76 deletions

View File

@ -223,10 +223,12 @@ class SvxForm(forms.Form):
comment = f"Online survex edit: {self.data['filename']}.svx on dev machine '{socket.gethostname()}' "
only_commit(fname, comment)
msg = "SAVED and committed to git (if there were differences)"
# should only call this is something changed
parse_one_file(self.data["filename"])
return "SAVED and committed to git (if there were differences)"
if parse_one_file(self.data["filename"]):
return msg
else:
return msg + "\nBUT PARSING failed. Do a completely new databaseReset."
def Process(self):
print(">>>>....\n....Processing\n")

View File

@ -373,7 +373,7 @@ class LoadingSurvex:
if self.inheritteam:
message = (
f"- INHERITING ({survexblock.parent})>({survexblock}) {survexblock.survexfile.path} '{self.inheritteam}'"
f"- no *team INHERITING ({survexblock.parent})>({survexblock}) {survexblock.survexfile.path} '{self.inheritteam}'"
)
print(self.insp + message)
# stash_data_issue(
@ -2315,85 +2315,31 @@ def parse_one_file(fpath): # --------------------------------------in progress--
It gets overwritten, and then nullified, on repeated SAVE & import.
I should learn how to step through with the debugger.
"""
def parse_new_svx(fpath):
newfileroot = MakeFileRoot(fpath)
def parse_new_svx(fpath, blockroot=None, svxfileroot=None):
if svxfileroot == None:
svxfileroot = MakeFileRoot(fpath)
svxfileroot.save()
if blockroot == None:
newname = "adhoc_" + str(Path(str(svxfileroot)).name)
survexblockparent = SurvexBlock(
name="adhoc_parent", survexpath="", survexfile=newfileroot, legsall=0, legslength=0.0
name=newname, survexpath="", survexfile=svxfileroot, legsall=0, legslength=0.0
)
survexblockparent.save()
blockroot = survexblockparent
svx_load.survexdict[newfileroot.survexdirectory] = []
svx_load.survexdict[newfileroot.survexdirectory].append(newfileroot)
svx_load.svxdirs[""] = newfileroot.survexdirectory
svx_load.survexdict[svxfileroot.survexdirectory] = []
svx_load.survexdict[svxfileroot.survexdirectory].append(svxfileroot)
svx_load.svxdirs[""] = svxfileroot.survexdirectory
# ----------------------------------------------------------------
svx_load.LinearLoad(survexblockparent, newfileroot.path, fname)
svx_load.LinearLoad(blockroot, svxfileroot.path, fname)
# ----------------------------------------------------------------
def reparse_existing_svx(svxs):
"""If this SurvexFile object already exists in the database, we want to keep the parent survexblock
but replace everything else by parsing the file.
But we do not want to delete and recreate the object as other survex files may have this as the parent
and we are not processing any *include we find
"""
svx = svxs[0] # first and only member of QuerySet
blocks = SurvexBlock.objects.filter(survexfile=svx)
if len(blocks) >= 1:
print(f"Blocks in '{svx}': {blocks}")
survexblockparent=blocks[0].parent # all should have same parent
# But may have been obliterated by previous error
# Stamp all over the accumulated lengths and legs in the parent block,
# This also obliterates survey lengths from all other 'sibling' survex files
# to the one being re-parsed
if survexblockparent:
survexblockparent.legsall=0
survexblockparent.legslength=0.0
survexblockparent.save()
display_contents(blocks)
print(f"ABORTING - UNSOLVED BUGS. Do a complete databaseReset")
return True
blocks.delete() # deletes all pre-existing SurvexBlocks attached to this SurvexFile
bafter = SurvexBlock.objects.filter(survexfile=svx)
display_contents(bafter)
# all these foreign keys should be recreated properly when the file is parsed.
# so why is /expedition/1996 crashing in nasty template error?
else:
print(f"ABORTING - UNSOLVED BUGS. Do a complete databaseReset")
return True
survexblockparent = SurvexBlock(
name="fresh_parent", survexpath="", survexfile=svx, legsall=0, legslength=0.0
)
survexblockparent.save()
print(f" - {survexblockparent=}")
svx_load.survexdict[svx.survexdirectory] = []
svx_load.survexdict[svx.survexdirectory].append(svx)
svx_load.svxdirs[""] = svx.survexdirectory
# ----------------------------------------------------------------
svx_load.LinearLoad(survexblockparent, fpath, fname)
# ----------------------------------------------------------------
# For some reason I have not yet worked out, I am getting the parent block
# added in as one of the child blocks of the survexfile
# so explicitly remove it.
blocks = SurvexBlock.objects.filter(survexfile=svx)
print(f"\nAfter import. {svx=}")
display_contents(blocks)
#survexblockparent.survexfile = MakeFileRoot("")
survexblockparent.delete()
blocks = SurvexBlock.objects.filter(survexfile=svx)
print(f"\nAfter import, specific removal. {svx=}")
display_contents(blocks)
print(f"\n - Loading One Survex file '{fpath}'", file=sys.stderr)
svx_load = LoadingSurvex()
svx_load.survexdict = {}
fname = Path(settings.SURVEX_DATA, (fpath + ".svx"))
# print(f" - {fname=}")
@ -2403,9 +2349,54 @@ def parse_one_file(fpath): # --------------------------------------in progress--
if len(svxs)>1:
print(f" ! Mistake? More than one survex file object in database with the same file-path {svxs}")
print(f" - Aborting file parsing & import into database.")
return True
return False
print(f" - Pre-existing survexfile {svxs}.")
reparse_existing_svx(svxs)
# reparse_existing_svx(svxs)
existingsvx = SurvexFile.objects.get(path=fpath)
existingcave = existingsvx.cave
print(f" - survexfile is {existingsvx} id={existingsvx.id} {existingcave}")
sbs = existingsvx.survexblock_set.all()
existingparent = None
parents =set()
if sbs:
for sb in sbs:
print(f" - cleaning survex block {sb=}")
try:
if sb.parent:
parents.add(sb.parent)
except:
print(f" ! FAILURE to access sb.parent {sb=}")
sb.delete()
if parents:
print(f" - set of parent blocks {parents}")
if len(parents) > 1:
print(f" - WARNING more than one parent survex block!")
existingparent = parents.pop()
# print(f" - deleting survex file {existingsvx=}")
# existingsvx.delete()
print(f" - Reloading and parsing this survexfile '{fpath}' Loading...")
parse_new_svx(fpath, blockroot=existingparent, svxfileroot=existingsvx)
svxs = SurvexFile.objects.filter(path=fpath)
if len(svxs)>1:
print(f" ! Mistake? More than one survex file object in database with the same file-path {svxs}")
print(f" - Aborting file parsing & import into database.")
return False
replacesvx = SurvexFile.objects.get(path=fpath)
replacesvx.cave = existingcave
print(f" - new/replacement survexfile {svxs}. id={replacesvx.id}")
replacesvx.save()
if parents:
sbs = replacesvx.survexblock_set.all()
for sb in sbs:
print(f" - re-setting survex block parent{sb=}")
sb.parent = existingparent # should be all the same
sb.save()
else:
print(f" - Not seen this survexfile before '{fpath}' Loading...")
parse_new_svx(fpath)
@ -2420,9 +2411,10 @@ def parse_one_file(fpath): # --------------------------------------in progress--
tf += len(svx_load.survexdict[d])
print(f" - Number of SurvexFiles: {tf:,}")
print(f" - Number of Survex legs: {legsnumber:,}")
print(f" - Length of Survex legs: {svx_load.slength:.2f}")
print(f" - Length of Survex legs: {svx_load.slength:.2f} m")
svx_load = None
return True
def MakeSurvexFileRoot():
"""Returns a file_object.path = SURVEX_TOPNAME associated with directory_object.path = SURVEX_DATA"""
@ -2445,6 +2437,7 @@ def MakeFileRoot(fn):
CHANGE THIS to just use the same block root as for SURVEX_TOPNAME ?
"""
print(f" - making a new root survexfile for this import: {fn}")
fileroot = SurvexFile(path=fn, cave=None)
fileroot.survexdirectory = SurvexDirectory.objects.get(id=1) # just re-use the first thing we made
fileroot.save()

View File

@ -81,7 +81,7 @@ LOGMESSAGES
{% endif %}
</div>
underground survey length: {{svxlength|floatformat:2}} metres<br />
parent survex file <a href="/survexfile/{{ survexfile.cave.survex_file }}">{{survexfile.cave.survex_file}}</a> for this cave <br />
parent survex file <a href="/survexfile/{{ survexfile.cave.survex_file }}">{{survexfile.cave.survex_file}}</a> for this cave {{survexfile.cave}} <br />
{% for sb in svxblocks %}
block:({{sb}}) has parent block:<a href="{{sb.parent.path}}">({{sb.parent}})</a>
{% empty %}