From ddb90b3a39974512c6bb9597a97d7c069a0c2d47 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Mon, 24 Feb 2025 12:25:35 +0000 Subject: tidy survex file writing --- core/views/survex.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'core/views/survex.py') diff --git a/core/views/survex.py b/core/views/survex.py index 72d44f1..ccf1bd0 100644 --- a/core/views/survex.py +++ b/core/views/survex.py @@ -143,7 +143,7 @@ def get_survexfile(filename): print(f"Number of SurvexFile objects found: {len(refs)}") for s in refs: print (s.path, s.primary, s.cave) - print(type(survexfile), filename) + # print(type(survexfile), filename) return survexfile class SvxForm(forms.Form): @@ -208,6 +208,9 @@ class SvxForm(forms.Form): def SaveCode(self, rcode, editor): fname = SVXPATH / (self.data["filename"] + ".svx") if not fname.is_file(): + # this is a new survex file being created from the template + if not fname.parent.is_dir(): + fname.parent.mkdir(parents=True, exist_ok=True) if re.search(r"\[|\]", rcode): errmsg = "Error: remove all []s from the text.\nEverything inside [] are only template guidance.\n\n" errmsg += "All [] must be edited out and replaced with real data before you can save this file.\n" @@ -218,29 +221,22 @@ class SvxForm(forms.Form): if mbeginend.group(1) != mbeginend.group(2): return "Error: mismatching begin/end labels" - # Make this create new survex folders if needed try: - fout = open(fname, "w", encoding="utf8", newline="\n") - except FileNotFoundError: - pth = os.path.dirname(self.data["filename"]) - newpath = SVXPATH / pth - if not os.path.exists(newpath): - os.makedirs(newpath) - fout = open(fname, "w", encoding="utf8", newline="\n") + # we only store survex files in Unix line-ending style, even if the code is running on Windows + with open(fname, "w", encoding="utf8", newline="\n") as fout: + # HTML forms standard behaviour is to insert CRLF whatever you say. So fix that: + fout.write(rcode.replace("\r", "")) + fout.write("\n") + fout.close() except PermissionError: return ( "CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file. Ask a nerd to fix this." ) - # HTML forms standard behaviour is to insert CRLF whatever you say. So fix that: - fout.write(rcode.replace("\r", "")) - fout.write("\n") - fout.close() - comment = f"Online edit: {self.data['filename']}.svx" add_commit(fname, comment, editor) - msg = f"SAVED and committed to git (if there were differences)\nEdited by:{editor}" + msg = f"SAVED (and committed to git if there were differences)\nEdited by:{editor}" # should only call this is something changed if parse_one_file(self.data["filename"]): return msg -- cgit v1.2.3