summaryrefslogtreecommitdiffstats
path: root/core/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'core/utils.py')
-rw-r--r--core/utils.py29
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"""