2021-05-03 20:36:29 +01:00
|
|
|
import sys
|
|
|
|
import os
|
2022-07-27 21:24:19 +01:00
|
|
|
import subprocess
|
2021-05-03 20:36:29 +01:00
|
|
|
import types
|
|
|
|
import stat
|
|
|
|
import csv
|
|
|
|
import re
|
|
|
|
import datetime
|
2022-07-27 21:24:19 +01:00
|
|
|
import shutil, filecmp
|
2021-05-03 20:36:29 +01:00
|
|
|
|
|
|
|
from functools import reduce
|
2022-07-27 21:24:19 +01:00
|
|
|
from pathlib import Path
|
2021-05-03 20:36:29 +01:00
|
|
|
|
|
|
|
import settings
|
|
|
|
from troggle.core.models.survex import SingleScan, Wallet, DrawingFile
|
|
|
|
from troggle.core.models.troggle import DataIssue
|
2021-05-04 20:57:16 +01:00
|
|
|
from troggle.core.utils import save_carefully, GetListDir
|
2022-08-01 15:32:35 +01:00
|
|
|
from troggle.core.views.scans import datewallet
|
2021-05-03 20:36:29 +01:00
|
|
|
|
|
|
|
'''Searches through all the survey scans directories (wallets) in expofiles, looking for images to be referenced.
|
|
|
|
'''
|
|
|
|
|
2022-03-15 17:04:43 +00:00
|
|
|
contentsjson = "contents.json"
|
2022-09-20 00:36:40 +01:00
|
|
|
|
2022-07-27 21:24:19 +01:00
|
|
|
git = settings.GIT
|
2022-03-15 17:04:43 +00:00
|
|
|
|
2022-09-20 00:36:40 +01:00
|
|
|
# to do: Actually read all the JSON files and set the survex file field appropriately!
|
2022-08-14 21:40:56 +01:00
|
|
|
|
2022-07-29 15:49:07 +01:00
|
|
|
|
|
|
|
def CheckEmptyDate(wallet):
|
2022-08-01 15:32:35 +01:00
|
|
|
'''If date is not set, get it from a linked survex file.
|
|
|
|
Could also look at filedates for the scans in expofiles/surveyscans/ , but these can be re-set by copying.
|
2022-07-29 15:49:07 +01:00
|
|
|
'''
|
2022-08-01 15:32:35 +01:00
|
|
|
earliest = datetime.datetime.now().date()
|
|
|
|
|
|
|
|
# This is not working, can't see why. An scans parser now taking a very long time..
|
|
|
|
#datewallet(wallet, earliest)
|
2022-07-29 15:49:07 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
def CheckEmptyPeople(wallet):
|
|
|
|
'''If people list is empty, copy them from the survex files: all of them
|
|
|
|
|
|
|
|
To be a Troggle model change; a many:many relationship between wallets and people,
|
|
|
|
as well as being a list in the JSON file (which is the permanent repository). We want the many:many
|
|
|
|
relationship so that we can filter wallets based on a person.
|
2022-08-01 15:32:35 +01:00
|
|
|
|
|
|
|
For the moment, we will just get a list..
|
2022-07-29 15:49:07 +01:00
|
|
|
'''
|
|
|
|
return
|
2021-05-03 20:36:29 +01:00
|
|
|
|
|
|
|
def LoadListScansFile(wallet):
|
|
|
|
gld = [ ]
|
|
|
|
# flatten out any directories in these wallet folders - should not be any
|
|
|
|
for (fyf, ffyf, fisdiryf) in GetListDir(wallet.fpath):
|
|
|
|
if fisdiryf:
|
|
|
|
gld.extend(GetListDir(ffyf))
|
|
|
|
else:
|
|
|
|
gld.append((fyf, ffyf, fisdiryf))
|
|
|
|
|
|
|
|
c=0
|
|
|
|
for (fyf, ffyf, fisdiryf) in gld:
|
2022-08-13 23:47:53 +01:00
|
|
|
if re.search(r"\.(?:png|jpg|jpeg|pdf|svg|gif|xvi)(?i)$", fyf):
|
2021-05-03 20:36:29 +01:00
|
|
|
singlescan = SingleScan(ffile=ffyf, name=fyf, wallet=wallet)
|
|
|
|
singlescan.save()
|
|
|
|
c+=1
|
|
|
|
if c>=10:
|
|
|
|
print(".", end='')
|
|
|
|
c = 0
|
2022-09-22 22:49:40 +01:00
|
|
|
|
2021-05-03 20:36:29 +01:00
|
|
|
def load_all_scans():
|
2022-08-07 21:41:45 +01:00
|
|
|
'''This iterates through the scans directories (either here or on the remote server)
|
|
|
|
and builds up the models we can access later.
|
|
|
|
It does NOT read or validate anything in the JSON data attached to each wallet. Those checks
|
|
|
|
are done at runtime, when a wallet is accessed, not at import time.
|
2022-08-24 16:28:15 +01:00
|
|
|
|
2022-09-22 22:49:40 +01:00
|
|
|
Replace GetListDir with a more modern Path.iter idiom
|
|
|
|
path = Path("scans")
|
|
|
|
for p in path.rglob("*"):
|
|
|
|
print(p.name)
|
|
|
|
|
2022-08-07 21:41:45 +01:00
|
|
|
'''
|
2021-05-03 20:36:29 +01:00
|
|
|
print(' - Loading Survey Scans')
|
|
|
|
|
|
|
|
SingleScan.objects.all().delete()
|
|
|
|
Wallet.objects.all().delete()
|
2022-07-22 09:23:00 +01:00
|
|
|
print(' - deleting all Wallet and SingleScan objects')
|
2022-03-15 20:53:55 +00:00
|
|
|
DataIssue.objects.filter(parser='scans').delete()
|
2022-09-22 22:49:40 +01: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
|
2022-09-22 22:49:40 +01:00
|
|
|
# Not all folders with files in them are wallets.
|
|
|
|
# they are if they are /2010/2010#33
|
|
|
|
# or /1996-1999NotKHbook/
|
|
|
|
# but not if they are /2010/1010#33/therion or /1998/
|
2021-05-03 20:36:29 +01:00
|
|
|
print(' - ', end=' ')
|
2022-09-22 22:49:40 +01:00
|
|
|
scans_path = Path(settings.SCANS_ROOT)
|
|
|
|
seen = []
|
|
|
|
for p in scans_path.rglob('*'):
|
|
|
|
if p.is_file():
|
|
|
|
if p.suffix.lower() not in valids and p.name.lower() not in validnames:
|
|
|
|
# print(f"'{p}'", end='\n')
|
|
|
|
pass
|
|
|
|
elif p.parent == scans_path: # skip files directly in /surveyscans/
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if p.parent.parent.parent.parent == scans_path:
|
|
|
|
# print(f"too deep {p}", end='\n')
|
|
|
|
fpath = p.parent.parent
|
|
|
|
walletname = p.parent.parent.name # wallet is one level higher
|
|
|
|
else:
|
|
|
|
fpath = p.parent
|
|
|
|
walletname = p.parent.name
|
|
|
|
|
|
|
|
# UNFINISHED
|
|
|
|
tag = p.parent
|
|
|
|
if len(walletname)>4:
|
|
|
|
if walletname[4] == "#":
|
|
|
|
tag = p.parent.parent
|
|
|
|
|
|
|
|
if tag not in seen:
|
|
|
|
print(f"{tag.name}", end=' ')
|
|
|
|
seen.append(tag)
|
|
|
|
#wallet = Wallet(fpath=fpath, walletname=walletname)
|
|
|
|
|
|
|
|
|
|
|
|
print('\n UNFINISHED \n\n--- ')
|
|
|
|
for topfolder, fpath, fisdir in GetListDir(settings.SCANS_ROOT):
|
2021-05-03 20:36:29 +01:00
|
|
|
if not fisdir:
|
|
|
|
continue
|
2022-09-22 20:41:42 +01:00
|
|
|
|
2022-09-22 22:49:40 +01:00
|
|
|
# do the year folders
|
|
|
|
if re.match(r"\d\d\d\d$", topfolder):
|
|
|
|
print(f"{topfolder}", end=' ')
|
|
|
|
for walletname, fpath, fisdir in GetListDir(fpath):
|
|
|
|
if fisdir:
|
|
|
|
wallet = Wallet(fpath=fpath, walletname=walletname)
|
|
|
|
# this is where we should record the year explicitly
|
|
|
|
# line 347 of view/uploads.py and needs refactoring for loading contentsjson
|
|
|
|
CheckEmptyDate(wallet)
|
|
|
|
CheckEmptyPeople(wallet)
|
|
|
|
wallet.save()
|
|
|
|
LoadListScansFile(wallet)
|
|
|
|
else:
|
|
|
|
# but We *should* load all the scans, even for nonstandard names.
|
|
|
|
print(f'\n - IGNORE {walletname} - {fpath}')
|
2022-08-16 18:02:28 +01:00
|
|
|
|
|
|
|
# but we also need to check if JSON exists, even if there are no uploaded scan files
|
|
|
|
contents_path = Path(settings.DRAWINGS_DATA, "walletjson")
|
|
|
|
for yeardir in contents_path.iterdir():
|
|
|
|
if yeardir.is_dir():
|
|
|
|
for walletpath in yeardir.iterdir():
|
|
|
|
if Path(walletpath, contentsjson).is_file():
|
|
|
|
walletname = walletpath.name
|
|
|
|
wallet, created = Wallet.objects.update_or_create(walletname=walletname)
|
2022-09-20 00:36:40 +01:00
|
|
|
# should now also load the json and use it ! check &ref is correct or missing too
|
2022-08-16 18:02:28 +01:00
|
|
|
if created:
|
|
|
|
print(f"\n{walletname} created: only JSON, no actual uploaded scan files.", end=' ')
|
|
|
|
CheckEmptyDate(wallet)
|
|
|
|
CheckEmptyPeople(wallet)
|
|
|
|
wallet.save()
|
|
|
|
|
2021-05-03 20:36:29 +01:00
|
|
|
|
|
|
|
print("", flush=True)
|