test: lint tests and tools aswell (#2296)

* test: lint `tests` and `tools` aswell

more linting :)

* fix linting issues in tests and tools

* fixup!

* how is this working?
This commit is contained in:
dni ⚡ 2024-02-27 14:30:52 +01:00 committed by GitHub
parent e8aa498683
commit 884a1b9d6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 103 additions and 28 deletions

View file

@ -48,7 +48,7 @@ def check_db_versions(sqdb):
postgres = get_postgres_cursor()
postgres.execute("SELECT * FROM public.dbversions;")
dbpost = dict(postgres.fetchall())
dbpost = dict(postgres.fetchall()) # type: ignore
for key, value in dblite.items():
if key in dblite and key in dbpost:
@ -104,7 +104,7 @@ def insert_to_pg(query, data):
connection.close()
def migrate_core(file: str, exclude_tables: List[str] = None):
def migrate_core(file: str, exclude_tables: List[str] = []):
print(f"Migrating core: {file}")
migrate_db(file, "public", exclude_tables)
print("✅ Migrated core")
@ -118,12 +118,12 @@ def migrate_ext(file: str):
print(f"✅ Migrated ext: {schema}")
def migrate_db(file: str, schema: str, exclude_tables: List[str] = None):
def migrate_db(file: str, schema: str, exclude_tables: List[str] = []):
# first we check if this file exists:
assert os.path.isfile(file), f"{file} does not exist!"
sq = get_sqlite_cursor(file)
tables = sq.execute(
cursor = get_sqlite_cursor(file)
tables = cursor.execute(
"""
SELECT name FROM sqlite_master
WHERE type='table' AND name not like 'sqlite?_%' escape '?'
@ -139,16 +139,16 @@ def migrate_db(file: str, schema: str, exclude_tables: List[str] = None):
if exclude_tables and tableName in exclude_tables:
continue
columns = sq.execute(f"PRAGMA table_info({tableName})").fetchall()
columns = cursor.execute(f"PRAGMA table_info({tableName})").fetchall()
q = build_insert_query(schema, tableName, columns)
data = sq.execute(f"SELECT * FROM {tableName};").fetchall()
data = cursor.execute(f"SELECT * FROM {tableName};").fetchall()
if len(data) == 0:
print(f"🛑 You sneaky dev! Table {tableName} is empty!")
insert_to_pg(q, data)
sq.close()
cursor.close()
def build_insert_query(schema, tableName, columns):

View file

@ -13,16 +13,20 @@ if len(sys.argv) < 2:
sys.exit(1)
lang = sys.argv[1]
assert os.getenv("OPENAI_API_KEY"), "OPENAI_API_KEY env var not set"
def load_language(lang):
def load_language(lang: str) -> dict:
s = open(f"lnbits/static/i18n/{lang}.js", "rt").read()
prefix = "window.localisation.%s = {\n" % lang
assert s.startswith(prefix)
s = s[len(prefix) - 2 :]
return json5.loads(s)
json = json5.loads(s)
assert isinstance(json, dict)
return json
def save_language(lang, data):
def save_language(lang: str, data) -> None:
with open(f"lnbits/static/i18n/{lang}.js", "wt") as f:
f.write("window.localisation.%s = {\n" % lang)
row = 0
@ -40,7 +44,7 @@ def save_language(lang, data):
f.write("}\n")
def string_variables_match(str1, str2):
def string_variables_match(str1: str, str2: str) -> bool:
pat = re.compile(r"%\{[a-z0-9_]*\}")
m1 = re.findall(pat, str1)
m2 = re.findall(pat, str2)
@ -66,7 +70,6 @@ def translate_string(lang_from, lang_to, text):
"kr": "Korean",
"fi": "Finnish",
}[lang_to]
assert os.getenv("OPENAI_API_KEY"), "OPENAI_API_KEY env var not set"
client = OpenAI()
try:
chat_completion = client.chat.completions.create(
@ -82,6 +85,7 @@ def translate_string(lang_from, lang_to, text):
],
model="gpt-4-1106-preview", # aka GPT-4 Turbo
)
assert chat_completion.choices[0].message.content, "No response from GPT-4"
translated = chat_completion.choices[0].message.content.strip()
# return translated string only if variables were not broken
if string_variables_match(text, translated):