2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-02-08 13:18:15 +00:00

tidy survex file writing

This commit is contained in:
2025-02-24 12:25:35 +00:00
parent 3118b717a0
commit ddb90b3a39

View File

@@ -143,7 +143,7 @@ def get_survexfile(filename):
print(f"Number of SurvexFile objects found: {len(refs)}") print(f"Number of SurvexFile objects found: {len(refs)}")
for s in refs: for s in refs:
print (s.path, s.primary, s.cave) print (s.path, s.primary, s.cave)
print(type(survexfile), filename) # print(type(survexfile), filename)
return survexfile return survexfile
class SvxForm(forms.Form): class SvxForm(forms.Form):
@@ -208,6 +208,9 @@ class SvxForm(forms.Form):
def SaveCode(self, rcode, editor): def SaveCode(self, rcode, editor):
fname = SVXPATH / (self.data["filename"] + ".svx") fname = SVXPATH / (self.data["filename"] + ".svx")
if not fname.is_file(): 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): if re.search(r"\[|\]", rcode):
errmsg = "Error: remove all []s from the text.\nEverything inside [] are only template guidance.\n\n" 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" 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): if mbeginend.group(1) != mbeginend.group(2):
return "Error: mismatching begin/end labels" return "Error: mismatching begin/end labels"
# Make this create new survex folders if needed
try: try:
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
except FileNotFoundError: with open(fname, "w", encoding="utf8", newline="\n") as fout:
pth = os.path.dirname(self.data["filename"]) # HTML forms standard behaviour is to insert CRLF whatever you say. So fix that:
newpath = SVXPATH / pth fout.write(rcode.replace("\r", ""))
if not os.path.exists(newpath): fout.write("\n")
os.makedirs(newpath) fout.close()
fout = open(fname, "w", encoding="utf8", newline="\n")
except PermissionError: except PermissionError:
return ( return (
"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file. Ask a nerd to fix this." "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" comment = f"Online edit: {self.data['filename']}.svx"
add_commit(fname, comment, editor) 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 # should only call this is something changed
if parse_one_file(self.data["filename"]): if parse_one_file(self.data["filename"]):
return msg return msg