2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 16:17:12 +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)}")
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