husstanden/objects/glob.py

52 lines
1.4 KiB
Python
Raw Normal View History

import os
2019-02-12 16:59:43 +01:00
import json
import shutil
2019-04-26 04:24:19 +02:00
import mysql.connector
2019-04-26 07:23:40 +02:00
import bcrypt
2019-02-12 16:59:43 +01:00
# ------------------------------------------------------------------------------
# Global variables that is None by default and gets overwritten in other modules
app = None # main.py -> Flask App
2019-04-26 04:24:19 +02:00
sql_conn = None
2019-02-12 16:59:43 +01:00
2019-05-10 09:36:45 +02:00
langs = {}
2019-02-12 16:59:43 +01:00
# ------------------------------------------------------------------------------
# Global variables that initializes on first load of module
if not os.path.isfile("config.json"):
print("`config.json` were not found! Copying default_config and continuing...")
shutil.copy("default_config.json", "config.json")
2019-02-12 16:59:43 +01:00
with open("config.json", "r") as f:
2019-04-26 04:24:19 +02:00
config = json.load(f)
2019-05-10 09:36:45 +02:00
path = "locale"
for l in os.listdir(path):
langs[l] = {}
_path = "%s/%s" % (path, l)
for f in os.listdir(_path):
_f = "%s/%s" % (_path, f)
with open(_f, "r", encoding = "UTF-8") as j:
pnd = json.load(j)
for k in pnd.keys():
if k in langs[l]:
raise Exception("Duplicate localization entries found in file %s" % _f)
langs[l] = {**langs[l], **pnd}
2019-04-26 04:24:19 +02:00
def make_sql_connection():
return mysql.connector.connect(**config["mysql"])
def get_sql_connection():
global sql_conn
if sql_conn is None or not sql_conn.is_connected():
sql_conn = make_sql_connection()
return sql_conn
2019-04-26 07:23:40 +02:00
def hash_password(password):
return bcrypt.hashpw(password.encode(), bcrypt.gensalt(10, prefix=b"2a")).decode()
def check_password(p1, p2):
return bcrypt.checkpw(p1.encode(), p2.encode())