From 0a35824b9ce42d1220faab65923f9643d6458321 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Tue, 2 Jun 2020 21:38:29 +0100 Subject: [PATCH] update svx template & fix CRLF & utf8 --- core/views_survex.py | 120 ++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 47 deletions(-) diff --git a/core/views_survex.py b/core/views_survex.py index 1e6c1bf0..b2bd6477 100644 --- a/core/views_survex.py +++ b/core/views_survex.py @@ -15,47 +15,74 @@ from parsers.people import GetPersonExpeditionNameLookup import troggle.settings as settings import parsers.survex -survextemplatefile = """; Locn: Totes Gebirge, Austria - Loser/Augst-Eck Plateau (kataster group 1623) -; Cave: +survextemplatefile = """; *** DO NOT SAVE THIS FILE WITHOUT RENAMING IT !! *** +;[Stuff in square brackets is example text to be replaced with real data, +; removing the square brackets] *begin [surveyname] -*export [connecting stations] +; stations linked into other surveys (or likely to) +*export [1 8 12 34] -*title "area title" -*date 2999.99.99 -*team Insts [Caver] -*team Insts [Caver] -*team Notes [Caver] -*instrument [set number] +; Cave: +; Area in cave/QM: +*title "" +*date [2040.07.04] +*team Insts [Fred Foo] +*team Notes [Brenda Bar] +*team Pics [Brenda Bar] +*team Tape [Albert Anyone] +*instrument [SAP #+Laser Tape/DistoX/Compass # ; Clino #] +; Calibration: [Where, readings] +*ref 2040#00 +; the #number is on the clear pocket containing the original notes -;ref.: 2009#NN +; if using a tape: +*calibrate tape +0.0 ; +ve if tape was too short, -ve if too long -*calibrate tape +0.0 ; +ve if tape was too short, -ve if too long +; Centreline data +*data normal from to length bearing gradient ignoreall +[ 1 2 5.57 034.5 -12.8 ] -*data normal from to tape compass clino -1 2 3.90 298 -20 +;----------- +;recorded station details (leave commented out) +;(NP=Nail Polish, LHW/RHW=Left/Right Hand Wall) +;Station Left Right Up Down Description +;[Red] nail varnish markings +[;1 0.8 0 5.3 1.6 ; NP on boulder. pt 23 on foo survey ] +[;2 0.3 1.2 6 1.2 ; NP '2' LHW ] +[;3 1.3 0 3.4 0.2 ; Rock on floor - not refindable ] -*data passage station left right up down ignoreall -1 [L] [R] [U] [D] comment -*end [surveyname]""" - - -def ReplaceTabs(stext): - res = [ ] - nsl = 0 - for s in re.split("(\t|\n)", stext): - if s == "\t": - res.append(" " * (4 - (nsl % 4))) - nsl = 0 - continue - if s == "\n": - nsl = 0 - else: - nsl += len(s) - res.append(s) - return "".join(res) +;LRUDs arranged into passage tubes +;new *data command for each 'passage', +;repeat stations and adjust numbers as needed +*data passage station left right up down +;[ 1 0.8 0 5.3 1.6 ] +;[ 2 0.3 1.2 6 1.2 ] +*data passage station left right up down +;[ 1 1.3 1.5 5.3 1.6 ] +;[ 3 2.4 0 3.4 0.2 ] + + +;----------- +;Question Mark List ;(leave commented-out) +; The nearest-station is the name of the survey and station which are nearest to +; the QM. The resolution-station is either '-' to indicate that the QM hasn't +; been checked; or the name of the survey and station which push that QM. If a +; QM doesn't go anywhere, set the resolution-station to be the same as the +; nearest-station. Include any relevant details of how to find or push the QM in +; the textual description. +;Serial number grade(A/B/C/X) nearest-station resolution-station description +;[ QM1 A surveyname.3 - description of QM ] +;[ QM2 B surveyname.5 - description of QM ] + +;------------ +;Cave description ;(leave commented-out) +;freeform text describing this section of the cave + +*end [surveyname] +""" class SvxForm(forms.Form): @@ -69,9 +96,8 @@ class SvxForm(forms.Form): fname = settings.SURVEX_DATA + self.data['filename'] + ".svx" if not os.path.isfile(fname): return survextemplatefile - fin = open(fname, "rb") - svxtext = fin.read().decode("latin1") # unicode(a, "latin1") - svxtext = ReplaceTabs(svxtext).strip() + fin = open(fname, "rt") + svxtext = fin.read().encode("utf8") fin.close() return svxtext @@ -84,19 +110,19 @@ class SvxForm(forms.Form): def SaveCode(self, rcode): fname = settings.SURVEX_DATA + self.data['filename'] + ".svx" if not os.path.isfile(fname): - # only save if appears valid if re.search(r"\[|\]", rcode): - return "Error: clean up all []s from the text" + return "Error: remove all []s from the text. They are only template guidance." mbeginend = re.search(r"(?s)\*begin\s+(\w+).*?\*end\s+(\w+)", rcode) if not mbeginend: return "Error: no begin/end block here" if mbeginend.group(1) != mbeginend.group(2): - return "Error: mismatching beginend" + return "Error: mismatching begin/end labels" - fout = open(fname, "w") - res = fout.write(rcode.encode("latin1")) + fout = open(fname, "wt") + # javascript seems to insert CRLF on WSL1. + res = fout.write(rcode.replace("\r","")) fout.close() - return "SAVED" + return "SAVED ." def Process(self): print("....\n\n\n....Processing\n\n\n") @@ -104,7 +130,7 @@ class SvxForm(forms.Form): os.chdir(os.path.split(settings.SURVEX_DATA + self.data['filename'])[0]) os.system(settings.CAVERN + " --log " + settings.SURVEX_DATA + self.data['filename'] + ".svx") os.chdir(cwd) - fin = open(settings.SURVEX_DATA + self.data['filename'] + ".log", "rb") + fin = open(settings.SURVEX_DATA + self.data['filename'] + ".log", "rt") log = fin.read() fin.close() log = re.sub("(?s).*?(Survey contains)", "\\1", log) @@ -179,7 +205,7 @@ def svx(request, survex_file): return render_to_response('svxfile.html', vmap) def svxraw(request, survex_file): - svx = open(os.path.join(settings.SURVEX_DATA, survex_file+".svx"), "rb") + svx = open(os.path.join(settings.SURVEX_DATA, survex_file+".svx"), "rt",encoding='utf8') return HttpResponse(svx, content_type="text") @@ -194,20 +220,20 @@ def process(survex_file): def threed(request, survex_file): process(survex_file) try: - threed = open(settings.SURVEX_DATA + survex_file + ".3d", "rb") + threed = open(settings.SURVEX_DATA + survex_file + ".3d", "rt",encoding='utf8') return HttpResponse(threed, content_type="model/3d") except: - log = open(settings.SURVEX_DATA + survex_file + ".log", "rb") + log = open(settings.SURVEX_DATA + survex_file + ".log", "rt",encoding='utf8') return HttpResponse(log, content_type="text") def log(request, survex_file): process(survex_file) - log = open(settings.SURVEX_DATA + survex_file + ".log", "rb") + log = open(settings.SURVEX_DATA + survex_file + ".log", "rt",encoding='utf8') return HttpResponse(log, content_type="text") def err(request, survex_file): process(survex_file) - err = open(settings.SURVEX_DATA + survex_file + ".err", "rb") + err = open(settings.SURVEX_DATA + survex_file + ".err", "rt",encoding='utf8') return HttpResponse(err, content_type="text")