diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2025-02-11 19:28:20 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2025-02-11 19:28:20 +0000 |
commit | d05b6b9b5fe8987a681ab80f408c66636731c31c (patch) | |
tree | 2760a4a618a4c752892731a5a35332c9240a71b9 /core/utils.py | |
parent | 096c3be4e51c121af9486d6c9a2667d029d20f12 (diff) | |
download | troggle-d05b6b9b5fe8987a681ab80f408c66636731c31c.tar.gz troggle-d05b6b9b5fe8987a681ab80f408c66636731c31c.tar.bz2 troggle-d05b6b9b5fe8987a681ab80f408c66636731c31c.zip |
fix many glitches for unusual JPGs
Diffstat (limited to 'core/utils.py')
-rw-r--r-- | core/utils.py | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/core/utils.py b/core/utils.py index 3129ffe..5e03971 100644 --- a/core/utils.py +++ b/core/utils.py @@ -97,10 +97,19 @@ def chaosmonkey(n): 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. + Deterministic """ sha.update(text.encode('utf-8')) return sha.hexdigest()[0:n] + +def random_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. + Random + """ + text = text + alphabet_suffix(3) + 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 @@ -230,10 +239,10 @@ def get_cookie(request): so having a blank is best. """ # NO_COOKIE_DEFAULT = 'Unset Cookie <hohlenforscher@potatohut.expo>' - print(f"-- Getting cookie...") + # print(f"-- Getting cookie...") editor_id = request.COOKIES.get('editor_id', "") # if no cookie, then default string "" editor = git_string(editor_id) # belt and braces, should have been validity checked on saving already - print(f"-- Cookie to be used: {editor=}") + # print(f"-- Cookie to be used: {editor=}") return editor def git_string(author_string): @@ -248,7 +257,7 @@ def git_string(author_string): return "" if author_regex.match(author_string): - print(f"++ Is valid git-compatible author string: '{author_string}'") + # print(f"++ Is valid git-compatible author string: '{author_string}'") return author_string else: editor = author_string.replace("@","_at_") @@ -267,7 +276,7 @@ def git_add(filename, cwd, commands=[]): # what is the purpose of this 'git diff' ? To prevent merge conflicts happening I guess, # so we do not have to reverse a 'git add' - print(f"git diff {filename} in {cwd}") + # print(f"git diff {filename} in {cwd}") cmd_diff = [git, "diff", filename] commands.append(cmd_diff) cp_diff = subprocess.run(cmd_diff, cwd=cwd, capture_output=True, text=True) @@ -349,11 +358,9 @@ def add_commit(fname, message, editor): def write_binary_file(filepath, content): print(f"write_binary_file: {filepath}") - write_files([(filepath, content, "")]) - -def write_files(files): - for filepath, content, encoding in files: - filename = filepath.name + write_files([(filepath, content, "")]) # null encoding does "wb" + +def ensure_dir_exists(filepath): if filepath.is_dir(): raise OSError( f"CANNOT write this file {filepath} as this is an existing DIRECTORY." @@ -369,6 +376,11 @@ def write_files(files): raise OSError( f"CANNOT make the directory for {filepath}. Ask a nerd to fix this: {e}" ) + +def write_files(files): + for filepath, content, encoding in files: + filename = filepath.name + ensure_dir_exists(filepath) if encoding: mode = "w" kwargs = {"encoding": encoding} @@ -377,7 +389,7 @@ def write_files(files): kwargs = {} try: with open(filepath, mode, **kwargs) as f: - print(f"WRITING {filepath} ") + # print(f"WRITING {filepath} ") f.write(content) except PermissionError as e: raise PermissionError( |