diff --git a/parsers/caves.py b/parsers/caves.py index e7eebb4..391501e 100644 --- a/parsers/caves.py +++ b/parsers/caves.py @@ -1,7 +1,7 @@ import os import re import string - +import sys from pathlib import Path @@ -103,15 +103,37 @@ def set_dummy_entrance(id, slug, cave, msg="DUMMY"): # 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 :loser: repo which is not recognised as a known cave. 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}") # - 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("/") a = parts[0][-4:] caveid = parts[1] @@ -142,9 +164,8 @@ def create_new_cave(svxpath, msg=None): print(message) 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 - cave.survex_file=survex_file + # 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.areacode=areacode cave.save() 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. Convoluted. Needs rewriting. 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": 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(): return survex_file + # This should find the file even if the capitalisation is different, or if the directory name is totaly different survex_file = "" d = Path(settings.SURVEX_DATA, f"caves-{areacode}/{id}") if d.is_dir(): @@ -257,10 +282,12 @@ def do_pending_cave(slug, caveid, url, areacode, msg=None): prime_suspect = survex_file if prime_suspect: survex_file = prime_suspect - # 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) - # print(message) - return survex_file + # 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) + # print(message) + if Path(settings.SURVEX_DATA, survex_file).is_file(): + return survex_file + return "" g = GetCaveLookup() with transaction.atomic(): @@ -324,7 +351,7 @@ def do_pending_cave(slug, caveid, url, areacode, msg=None): ) if cave: 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) print(message) diff --git a/parsers/survex.py b/parsers/survex.py index 651d879..679ed53 100644 --- a/parsers/survex.py +++ b/parsers/survex.py @@ -1170,8 +1170,10 @@ class LoadingSurvex: fixedpts/gps 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 + + 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: cave = self.caveslist[key] if type(cave) != Cave: @@ -1185,8 +1187,8 @@ class LoadingSurvex: message = (f" - {cavepath} starts with (while creating '{svxid}.svx' )") return False - if cavepath in self.caveslist: # primed with GCaveLookup - return self.caveslist[cavepath] + if cavepath.lower() in self.caveslist: # primed with GCaveLookup + return self.caveslist[cavepath.lower()] 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+)") @@ -1204,8 +1206,8 @@ class LoadingSurvex: if cavepath[6:10] in ARGEAREAS: 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 - self.caveslist[cavepath] = cave + 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.lower()] = cave message = f"\n ! MAKING cave {sluggy} for {cavepath=} {svxid=}" # stash_data_issue(parser="survex", message=message, url="/survexfile/{svxid}.svx", sb=(svxid)) return cave @@ -2439,13 +2441,14 @@ def MakeFileRoot(svxpath): """Returns a file_object.path Used by the online survex file editor when re-parsing or tries to find the primary survex file for this cave + Looks horrible, rewrite all this.. """ cave = IdentifyCave(svxpath) if not cave: if svxpath != UNSEENS: - cave = create_new_cave(svxpath) - - # is this really necessayr ?! + cave = create_new_cave(svxpath, "", "Make dummy Cave for MakeFileRoot {svxpath}") + + # is this really necessary ?! fileroot = SurvexFile(path=svxpath, cave=cave) fileroot.save() print(f" - Making/finding a new dummy root survexfile for this import: {svxpath} with cave {cave}")