Skip to content

Commit

Permalink
Have words as a python file to avoid import errors in components depe…
Browse files Browse the repository at this point in the history
…nding on this one
  • Loading branch information
acasajus committed Jan 15, 2025
1 parent 864b5d1 commit 3ce7317
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
13 changes: 5 additions & 8 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@
from flask_wtf import FlaskForm
from unidecode import unidecode

from .config import WORDS_FILE_PATH, ALLOWED_REDIRECT_DOMAINS
from .words import safe_words
from .config import ALLOWED_REDIRECT_DOMAINS
from .log import LOG

with open(WORDS_FILE_PATH) as f:
LOG.d("load words file: %s", WORDS_FILE_PATH)
_words = f.read().split()


def random_word():
return secrets.choice(_words)
return secrets.choice(safe_words)


def word_exist(word):
return word in _words
return word in safe_words


def random_words(words: int = 2, numbers: int = 0):
"""Generate a random words. Used to generate user-facing string, for ex email addresses"""
# nb_words = random.randint(2, 3)
fields = [secrets.choice(_words) for i in range(words)]
fields = [secrets.choice(safe_words) for i in range(words)]

if numbers > 0:
digits = [n for n in range(10)]
Expand Down
9 changes: 9 additions & 0 deletions app/words.py

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions scripts/regen-words.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
import os
import sys
import json

from app import config
from app.log import LOG

rootDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(rootDir)

LOG.i(f"Reading {config.WORDS_FILE_PATH} file")
words = [
word.strip()
for word in open(config.WORDS_FILE_PATH, "r").readlines()
if word.strip()
]

destFile = os.path.join(rootDir, "app", "words.py")
LOG.i(f"Writing {destFile}")

serialized_words = json.dumps(words)
with open(destFile, "wb") as fd:
fd.write(
f"""#
#This file is auto-generated. Please run {sys.argv[0]} to re-generate it
#
import json
safe_words = json.loads('{serialized_words}')
""".encode("utf-8")
)

0 comments on commit 3ce7317

Please sign in to comment.