diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2023-09-01 20:31:19 +0300 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2023-09-01 20:31:19 +0300 |
commit | 98412c140d9cadc3009febe8f903f79b4ad8769b (patch) | |
tree | 2763c4392f3dd0211722bfe57e409dd5715ece88 /core/utils.py | |
parent | 1cf02afec9ef1f6af74849cf2879d0e37215b8df (diff) | |
download | troggle-98412c140d9cadc3009febe8f903f79b4ad8769b.tar.gz troggle-98412c140d9cadc3009febe8f903f79b4ad8769b.tar.bz2 troggle-98412c140d9cadc3009febe8f903f79b4ad8769b.zip |
more robust tripid labelling
Diffstat (limited to 'core/utils.py')
-rw-r--r-- | core/utils.py | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/core/utils.py b/core/utils.py index a24a1dc..2aba924 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,14 +1,15 @@ +import hashlib import logging +import os import random import resource +import string import subprocess -import os from decimal import getcontext from pathlib import Path getcontext().prec = 2 # use 2 significant figures for decimal calculations - import settings """This file declares TROG a globally visible object for caches. @@ -30,6 +31,8 @@ thread. """ TROG = {"pagecache": {"expedition": {}}, "caves": {"gcavelookup": {}, "gcavecount": {}}} +alphabet = [] +sha = hashlib.new('sha256') # This is module-level executable. This is a Bad Thing. Especially when it touches the file system. try: @@ -50,7 +53,27 @@ def chaosmonkey(n): return False # print("CHAOS strikes !", file=sys.stderr) return True - + +def unique_slug(text, n): + """This gives an almost-unique id based on the text, + 2 hex digits would seem adequate, but we might get a collision. + Not used anywhere. + """ + sha.update(text.encode('utf-8')) + return sha.hexdigest()[0:n] + +def alphabet_suffix(n): + """This is called repeatedly during initial parsing import, hence the cached list + """ + global alphabet + if not alphabet: + alphabet = list(string.ascii_lowercase) + + if n < len(alphabet): + suffix = alphabet[n] + else: + suffix = "_X_" + random.choice(string.ascii_lowercase) + random.choice(string.ascii_lowercase) + return suffix def only_commit(fname, message): """Only used to commit a survex file edited and saved in view/survex.py""" |