2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-28 10:02:10 +00:00
troggle/parsers/scans.py

199 lines
7.9 KiB
Python
Raw Normal View History

2023-01-19 18:33:04 +00:00
import datetime
from pathlib import Path
2021-05-03 20:36:29 +01:00
import settings
from troggle.core.models.survex import SingleScan
2021-05-03 20:36:29 +01:00
from troggle.core.models.troggle import DataIssue
2024-08-15 20:54:18 +01:00
from troggle.core.models.wallets import Wallet, archaic_wallets
2021-05-03 20:36:29 +01:00
"""Searches through all the survey scans directories (wallets) in expofiles, looking for images to be referenced. Loads all the wallets .
todo - update the survexblock scanswallet field to refer to the wallet, if it isn't already.
But we can't do that, we only have the survexfile, not the blcok. But we could set it for ALL the blocks? Hmm.
2023-01-19 21:18:42 +00:00
"""
2021-05-03 20:36:29 +01:00
contentsjson = "contents.json"
git = settings.GIT
2023-01-19 21:18:42 +00:00
def set_walletyear(wallet):
_ = wallet.year() # don't need return value. Just calling this saves it as w.walletyear Syntactic.
2023-01-19 21:18:42 +00:00
def set_JSONwalletdate(wallet):
"""At this point in the import process, the survex files have not been imported so
2023-10-23 20:54:46 +01:00
we cannot get dates from them. There are about 40 JSON files (in 2022) which we read here.
Actually, doing anything that reads the JSON sets .walletdate"""
_ = wallet.date() # don't need return value. Sets .walletdate as side effect
2023-10-21 14:22:20 +01:00
def set_caves(wallet):
_ = wallet.allcaves() # don't need return value. Just calling this saves it as w.caves This ONLY gets the list on the wallet JSON
2021-05-03 20:36:29 +01:00
def load_all_scans():
2023-01-19 21:18:42 +00:00
"""This iterates through the scans directories (either here or on the remote server)
and builds up the models we can access later.
Loads people as a simple string of fullnames. We should replace this with a list of Person slugs,
and change the wallet editor to save People as slugs.
2023-01-19 21:18:42 +00:00
"""
print(" - Loading Survey Scans")
2021-05-03 20:36:29 +01:00
SingleScan.objects.all().delete()
Wallet.objects.all().delete()
# print(" - deleting all Wallet and SingleScan objects, and resize error messages")
2023-01-19 21:18:42 +00:00
DataIssue.objects.filter(parser="scans").delete()
DataIssue.objects.filter(parser="wallets").delete()
2023-09-14 13:08:10 +01:00
DataIssue.objects.filter(parser="mogrify").delete()
2023-01-19 21:18:42 +00:00
# These are valid old file types to be visible, they are not necessarily allowed to be uploaded to a new wallet.
# But we ignore this list.. anything goes..
2023-01-19 21:18:42 +00:00
valids = [
".top",
".txt",
".tif",
".png",
".jpg",
".jpeg",
".pdf",
".svg",
".gif",
".xvi",
".json",
".autosave",
".sxd",
".svx",
".th",
".th2",
".tdr",
".sql",
".zip",
".dxf",
".3d",
".ods",
".csv",
".xcf",
".xml",
]
validnames = ["thconfig", "manifest"]
2022-07-22 09:23:00 +01:00
2021-05-03 20:36:29 +01:00
# iterate into the surveyscans directory
# Not all folders with files in them are wallets.
2023-01-19 21:18:42 +00:00
# they are if they are /2010/2010#33
# or /1996-1999NotKHbook/
# but not if they are /2010/2010#33/therion/ : the wallet is /2010#33/ not /therion/
2024-08-11 16:41:27 +01:00
# READ THE FUNCTION get_json(self) in models/wallets which ALSO does this SAME job
# needs refactoring
2023-01-19 21:18:42 +00:00
print(" ", end="")
scans_path = Path(settings.SCANS_ROOT)
seen = set()
2023-01-19 21:18:42 +00:00
c = 0
wallets = {}
2024-08-11 13:19:59 +01:00
for p in scans_path.rglob("*"): # this implicitly ignoires empty folders as it looks only for files
if p.is_file():
2024-08-11 13:19:59 +01:00
# if p.suffix.lower() not in valids and p.name.lower() not in validnames:
# # we do not care how the files are called. Many have no suffices at all.
# print(f"'{p}'", end='\n')
2024-08-11 13:19:59 +01:00
# pass
if p.parent == scans_path: # skip files directly in /surveyscans/
pass
else:
2023-01-19 21:18:42 +00:00
c += 1
2024-08-11 16:41:27 +01:00
if c % 120 == 0:
2023-01-19 21:18:42 +00:00
print(".", end="")
2024-08-11 16:41:27 +01:00
if c % 6000 == 0:
2023-01-19 21:18:42 +00:00
print("\n ", end="")
2024-08-11 16:41:27 +01:00
# Finding the wallet from deeply hidden subdirectories
if p.parent.parent.parent.parent.parent.parent == scans_path:
fpath = p.parent.parent.parent.parent
walletname = p.parent.parent.parent.parent.name
elif p.parent.parent.parent.parent.parent == scans_path:
fpath = p.parent.parent.parent
walletname = p.parent.parent.parent.name
elif p.parent.parent.parent.parent == scans_path:
fpath = p.parent.parent
2024-08-11 16:41:27 +01:00
walletname = p.parent.parent.name
2023-01-19 21:18:42 +00:00
else:
fpath = p.parent
walletname = p.parent.name
2024-08-11 16:41:27 +01:00
2023-01-19 21:18:42 +00:00
if walletname in wallets: # assumes all walletnames are unique
wallet = wallets[walletname]
else:
2023-01-19 21:18:42 +00:00
print("", flush=True, end="")
# Create the wallet object. But we don't have a date for it yet.
2023-10-23 20:54:46 +01:00
wallet = Wallet.objects.create(walletname=walletname, fpath=fpath)
wallets[walletname] = wallet
set_walletyear(wallet)
2023-10-21 14:22:20 +01:00
set_caves(wallet)
2023-10-23 20:54:46 +01:00
wallet.save()
singlescan = SingleScan(ffile=fpath, name=p.name, wallet=wallet)
singlescan.save()
2023-01-19 21:18:42 +00:00
# only printing progress:
tag = str(p.parent.name) # folder containing the scan file
if len(tag) > 4:
if tag[4] != "#":
2024-08-15 21:25:49 +01:00
# relative_path = p.relative_to(scans_path, walk_up=False) # from python 3.12
2024-08-15 21:24:45 +01:00
relative_path = p.relative_to(scans_path)
2024-08-11 16:41:27 +01:00
seen.add((str(relative_path.parent)+"/", walletname))
wjson = 0
seenlist = list(seen)
seenlist.sort()
2024-08-15 20:54:18 +01:00
print(f"\n - modern wallets with sub-folders")
for tag in seenlist:
wjson += 1
2024-08-15 20:54:18 +01:00
dirc, wall = tag
if wall not in archaic_wallets:
print(f" {wall} {dirc}", end="\n")
2023-01-19 21:18:42 +00:00
print(f"\n - found and loaded {c:,} acceptable scan files in {len(wallets):,} wallets")
# but we also need to check if JSON exists, even if there are no uploaded scan files.
# Here we know there is a rigid folder structure, so no need to look for sub folders
2023-01-19 21:34:09 +00:00
print("\n - Checking for wallets where JSON exists, but there may be no uploaded scan files:")
2023-01-19 21:18:42 +00:00
print(" ", end="")
wjson = 0
2023-01-19 21:18:42 +00:00
contents_path = Path(settings.DRAWINGS_DATA, "walletjson")
for yeardir in contents_path.iterdir():
if yeardir.is_dir():
2023-01-19 21:18:42 +00:00
for walletpath in yeardir.iterdir():
if Path(walletpath, contentsjson).is_file():
walletname = walletpath.name
2023-01-19 21:18:42 +00:00
if walletname not in wallets:
wjson += 1
2023-01-19 21:18:42 +00:00
if wjson % 10 == 0:
print("\n ", end="")
2023-01-19 21:18:42 +00:00
print(f"{walletname} ", end="")
fpath = Path(settings.SCANS_ROOT, str(yeardir.stem), walletname)
# The wallets found from JSON should all have dates already
2022-10-03 19:18:35 +01:00
wallet, created = Wallet.objects.update_or_create(walletname=walletname, fpath=fpath)
wallets[walletname] = wallet
set_walletyear(wallet)
2023-10-21 14:22:20 +01:00
set_caves(wallet)
2023-10-23 20:54:46 +01:00
wallet.save()
if not created:
2023-01-19 21:18:42 +00:00
print(
f"\n - {walletname} was not created, but was not in directory walk of /surveyscans/. Who created it?"
)
print(f"\n - found another {wjson:,} JSON files, making a total of {len(wallets):,} wallets")
2024-08-11 13:19:59 +01:00
# Only the 1999 wallets have filepaths which would mean that the walletyear will be unset:
wallets = Wallet.objects.filter(walletyear=None)
for w in wallets:
w.walletyear = datetime.date(1999, 1, 1)
# Although the survex files haven't been processed yet, we can at least check if the wallets refer to a real file or not
for wallet in Wallet.objects.all():
# this reads JSON
wallet.check_survexlist()