diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2024-07-03 19:27:37 +0300 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2024-07-03 19:27:37 +0300 |
commit | 1c8c36c82fccead17af96b91720c266600805a8b (patch) | |
tree | bf30effad4cfef878ef7b42795343fa0b53ddc85 /core/utils.py | |
parent | b6ffcb63bf186e154dbf25d9dce8754c031d7a82 (diff) | |
download | troggle-1c8c36c82fccead17af96b91720c266600805a8b.tar.gz troggle-1c8c36c82fccead17af96b91720c266600805a8b.tar.bz2 troggle-1c8c36c82fccead17af96b91720c266600805a8b.zip |
loading cave aliases from file now working
Diffstat (limited to 'core/utils.py')
-rw-r--r-- | core/utils.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/core/utils.py b/core/utils.py index 6670a65..40483e3 100644 --- a/core/utils.py +++ b/core/utils.py @@ -2,6 +2,7 @@ import hashlib import logging import math import os +import re import random import resource import string @@ -43,6 +44,8 @@ sha = hashlib.new('sha256') throw = 35.0 + + # This is module-level executable. This is a Bad Thing. Especially when it touches the file system. try: logging.basicConfig(level=logging.DEBUG, filename=settings.LOGFILE, filemode="w") @@ -131,6 +134,52 @@ def current_expo(): else: return settings.EPOCH.year # this is 1970 +def parse_aliases(aliasfile): + """Reads a long text string containing pairs of strings: + (alias, target) + where the alias is an old name for a cave and the target is the current, valid name for the cave, e.g. + ("2015-mf-06", "1623-288"), + returns a list of tuple pairs + + May fail if there is more than one space after the comma separating strings + """ + + report = [] # Messages about failed lines + aliases = [] + + filepath = Path(settings.EXPOWEB) / "cave_data" / aliasfile + if not filepath.is_file(): + message = f' ** {filepath} is not a file.' + print(message) + return [(None, None)] + try: + with open(filepath, "r") as aliasfile: + for line in aliasfile: + l, sep, tail = line.partition('#') + l, sep, tail = l.partition(';') + l = l.strip().strip(',') # remove terminal comma if present + l = l.strip().strip('()') + l = l.replace("\"","") + l = l.replace("\'","") + l = l.replace(" ","") # removes all spaces + l = " ".join(l.split(',')) # subtle, splits on comma, joins with one space + if len(l) == 0: + # print(f"no parseable content: {line}") + continue + key, sep, target = l.partition(' ') + if len(key) == 0 or len(target) == 0: + message = f' ** Parsing failure for {line}' + print(message) + continue + + print(f"{key} => {target}") + aliases.append((key,target)) + except: + message = f' ** Cannot open {filepath} for text file reading even though it is a file.' + print(message) + return [(None, None)], "Fail on file reading" + return aliases, report + def only_commit(fname, message): """Only used to commit a survex file edited and saved in view/survex.py""" git = settings.GIT |