2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-04-02 16:41:52 +01:00

finding the right survex file for a cave

This commit is contained in:
Philip Sargent 2023-09-15 22:41:40 +03:00
parent 3390f17aa4
commit a8d4b05617
2 changed files with 50 additions and 20 deletions

View File

@ -1,7 +1,7 @@
import os import os
import re import re
import string import string
import sys
from pathlib import Path from pathlib import Path
@ -103,15 +103,37 @@ def set_dummy_entrance(id, slug, cave, msg="DUMMY"):
# print(message) # print(message)
def create_new_cave(svxpath, msg=None): def create_new_cave(svxpath, svxid=None, msg=None):
"""This is called only when a new survex file is edited online which has a path on the """This is called only when a new survex file is edited online which has a path on the
:loser: repo which is not recognised as a known cave. :loser: repo which is not recognised as a known cave.
ALSO called by survex parser when it finds a cave it doesn't recognise ALSO called by survex parser when it finds a cave it doesn't recognise
""" """
# e.g. svxpath = "caves-1623/666/beast" .svx # e.g. svxpath = "caves-1623/666/beast" .svx - from the *inlcude tree
# e.g. svxid = "caves-1623/666/beast"
print(f"Create new cave at {svxpath} - {msg}") print(f"Create new cave at {svxpath} - {msg}")
# #
survex_file = svxpath.replace("caves-","") + ".svx" survex_file = ""
if svxid:
sv = Path(settings.SURVEX_DATA, svxid + ".svx")
if sv.is_file:
survex_file = svxid + ".svx"
else:
sv = Path(settings.SURVEX_DATA, svxpath + ".svx")
if sv.is_file:
survex_file = svxpath + ".svx"
if survex_file:
# message = f"Found a survex file {survex_file=} {svxpath=} {svxid=} "
# DataIssue.objects.create(parser="caves", message=message)
# print(message, file=sys.stderr)
# print(message)
pass
else:
message = f"NOT found a survex file {svxpath=} {svxid=}"
DataIssue.objects.create(parser="caves", message=message)
print(message, file=sys.stderr)
print(message)
parts = svxpath.split("/") parts = svxpath.split("/")
a = parts[0][-4:] a = parts[0][-4:]
caveid = parts[1] caveid = parts[1]
@ -142,9 +164,8 @@ def create_new_cave(svxpath, msg=None):
print(message) print(message)
raise raise
# we know what the survex file is, we don't need to use the guess. # But this sets the survex file on the Cave from the first one we find, not necessarily the best survex file for this cave
# But this sets the survex file on the cave from the first one we find, not necessarily the best survex file for this cave cave.survex_file=survex_file # primary survex file for Cave
cave.survex_file=survex_file
cave.areacode=areacode cave.areacode=areacode
cave.save() cave.save()
return cave return cave
@ -230,6 +251,9 @@ def do_pending_cave(slug, caveid, url, areacode, msg=None):
"""Guesses at and finds a survex file for this pending cave. """Guesses at and finds a survex file for this pending cave.
Convoluted. Needs rewriting. Convoluted. Needs rewriting.
Pointless if this cave is being created because we found a survex file... Pointless if this cave is being created because we found a survex file...
On problem is that the Cave name may have different capitalisation from the survex filename,
e.g. 2018-NTU-02 has a survex file 2018-ntu-02.svx
""" """
if k[0:3] == "162": if k[0:3] == "162":
id = Path(k[5:]) id = Path(k[5:])
@ -244,6 +268,7 @@ def do_pending_cave(slug, caveid, url, areacode, msg=None):
if Path(settings.SURVEX_DATA, survex_file).is_file(): if Path(settings.SURVEX_DATA, survex_file).is_file():
return survex_file return survex_file
# This should find the file even if the capitalisation is different, or if the directory name is totaly different
survex_file = "" survex_file = ""
d = Path(settings.SURVEX_DATA, f"caves-{areacode}/{id}") d = Path(settings.SURVEX_DATA, f"caves-{areacode}/{id}")
if d.is_dir(): if d.is_dir():
@ -257,10 +282,12 @@ def do_pending_cave(slug, caveid, url, areacode, msg=None):
prime_suspect = survex_file prime_suspect = survex_file
if prime_suspect: if prime_suspect:
survex_file = prime_suspect survex_file = prime_suspect
# message = f" ! {k:14} Found a survex file which might be the right one: {survex_file} - {msg}" # message = f" ! {k:14} Found a survex file which might be the right one: {survex_file} - {msg}"
# DataIssue.objects.create(parser='caves', message=message, url=url) # DataIssue.objects.create(parser='caves', message=message, url=url)
# print(message) # print(message)
return survex_file if Path(settings.SURVEX_DATA, survex_file).is_file():
return survex_file
return ""
g = GetCaveLookup() g = GetCaveLookup()
with transaction.atomic(): with transaction.atomic():
@ -324,7 +351,7 @@ def do_pending_cave(slug, caveid, url, areacode, msg=None):
) )
if cave: if cave:
cave.save() # must save to have id before foreign keys work. This is also a ManyToMany key. cave.save() # must save to have id before foreign keys work. This is also a ManyToMany key.
message = f" ! {slug:18} Pending cave write-up url: {url} - {msg}" message = f" ! {slug:18} Pending cave write-up url: {url} - {survex_file=} - {msg}"
DataIssue.objects.create(parser="caves", message=message, url=url) DataIssue.objects.create(parser="caves", message=message, url=url)
print(message) print(message)

View File

@ -1170,8 +1170,10 @@ class LoadingSurvex:
fixedpts/gps fixedpts/gps
and everything at top level, directly in caves-1623/ not in a subdir and everything at top level, directly in caves-1623/ not in a subdir
NOTE self.cavelist is a superset of GCaveLookup, which already contians both uppercase and lowercase aliases NOTE self.cavelist is a superset of GCaveLookup, which already contians both uppercase and lowercase aliases
why is this called with cavepath="caves-1623/2023-kt-02" when this is a cave where the files are in "caves-1623/2023-kt-02/"
""" """
if cavepath == "caves-1623/99ob02": if cavepath == "caves-1623/99ob02": # nothing special about this cave, just used as a marker to dump the cavelist to file
for key in self.caveslist: for key in self.caveslist:
cave = self.caveslist[key] cave = self.caveslist[key]
if type(cave) != Cave: if type(cave) != Cave:
@ -1185,8 +1187,8 @@ class LoadingSurvex:
message = (f" - {cavepath} starts with <ignoreprefix> (while creating '{svxid}.svx' )") message = (f" - {cavepath} starts with <ignoreprefix> (while creating '{svxid}.svx' )")
return False return False
if cavepath in self.caveslist: # primed with GCaveLookup if cavepath.lower() in self.caveslist: # primed with GCaveLookup
return self.caveslist[cavepath] return self.caveslist[cavepath.lower()]
rx_svxcollection = re.compile(r"(?i)caves-(\d\d\d\d)/(.*)$") rx_svxcollection = re.compile(r"(?i)caves-(\d\d\d\d)/(.*)$")
# rx_cave = re.compile(r"(?i)caves-(\d\d\d\d)/([-\d\w]+|\d\d\d\d-?\w+-\d+)") # rx_cave = re.compile(r"(?i)caves-(\d\d\d\d)/([-\d\w]+|\d\d\d\d-?\w+-\d+)")
@ -1204,8 +1206,8 @@ class LoadingSurvex:
if cavepath[6:10] in ARGEAREAS: if cavepath[6:10] in ARGEAREAS:
return do_ARGE_cave(sluggy, caveid, area, svxid) return do_ARGE_cave(sluggy, caveid, area, svxid)
cave = create_new_cave(cavepath, f"Cave mentioned only in a survex file {svxid}") # uses the pending code to create pending cave descriptions cave = create_new_cave(cavepath, svxid, f"Cave mentioned only in a survex file {svxid=}") # uses the pending code to create pending cave descriptions
self.caveslist[cavepath] = cave self.caveslist[cavepath.lower()] = cave
message = f"\n ! MAKING cave {sluggy} for {cavepath=} {svxid=}" message = f"\n ! MAKING cave {sluggy} for {cavepath=} {svxid=}"
# stash_data_issue(parser="survex", message=message, url="/survexfile/{svxid}.svx", sb=(svxid)) # stash_data_issue(parser="survex", message=message, url="/survexfile/{svxid}.svx", sb=(svxid))
return cave return cave
@ -2439,13 +2441,14 @@ def MakeFileRoot(svxpath):
"""Returns a file_object.path """Returns a file_object.path
Used by the online survex file editor when re-parsing Used by the online survex file editor when re-parsing
or tries to find the primary survex file for this cave or tries to find the primary survex file for this cave
Looks horrible, rewrite all this..
""" """
cave = IdentifyCave(svxpath) cave = IdentifyCave(svxpath)
if not cave: if not cave:
if svxpath != UNSEENS: if svxpath != UNSEENS:
cave = create_new_cave(svxpath) cave = create_new_cave(svxpath, "", "Make dummy Cave for MakeFileRoot {svxpath}")
# is this really necessayr ?! # is this really necessary ?!
fileroot = SurvexFile(path=svxpath, cave=cave) fileroot = SurvexFile(path=svxpath, cave=cave)
fileroot.save() fileroot.save()
print(f" - Making/finding a new dummy root survexfile for this import: {svxpath} with cave {cave}") print(f" - Making/finding a new dummy root survexfile for this import: {svxpath} with cave {cave}")