总线通信架构整改完成
This commit is contained in:
		@@ -1,18 +1,23 @@
 | 
			
		||||
import sqlite3
 | 
			
		||||
import time
 | 
			
		||||
from inspect import currentframe
 | 
			
		||||
 | 
			
		||||
from codes.common import clibs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def db_init(db_file):
 | 
			
		||||
    conn = sqlite3.connect(db_file, isolation_level=None, check_same_thread=False, cached_statements=2048, timeout=10.0)
 | 
			
		||||
    cursor = conn.cursor()
 | 
			
		||||
    cursor.execute("PRAGMA journal_mode=wal")
 | 
			
		||||
    cursor.execute("PRAGMA wal_checkpoint=TRUNCATE")
 | 
			
		||||
    cursor.execute("PRAGMA synchronous=normal")
 | 
			
		||||
    cursor.execute("PRAGMA temp_store=memory")
 | 
			
		||||
    cursor.execute("PRAGMA mmap_size=30000000000")
 | 
			
		||||
    cursor.execute("PRAGMA cache_size=200000")
 | 
			
		||||
    cursor.execute(
 | 
			
		||||
def db_init():
 | 
			
		||||
    if clibs.db_file.exists():
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    clibs.conn = sqlite3.connect(clibs.db_file, isolation_level=None, check_same_thread=False, cached_statements=2048, timeout=10.0)
 | 
			
		||||
    clibs.cursor = clibs.conn.cursor()
 | 
			
		||||
    clibs.cursor.execute("PRAGMA journal_mode=wal")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA wal_checkpoint=TRUNCATE")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA synchronous=normal")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA temp_store=memory")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA mmap_size=30000000000")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA cache_size=200000")
 | 
			
		||||
    clibs.cursor.execute(
 | 
			
		||||
        """
 | 
			
		||||
        create table if not exists logs(
 | 
			
		||||
            id integer primary key autoincrement,
 | 
			
		||||
@@ -23,7 +28,7 @@ def db_init(db_file):
 | 
			
		||||
        )
 | 
			
		||||
        """
 | 
			
		||||
    )
 | 
			
		||||
    cursor.execute(
 | 
			
		||||
    clibs.cursor.execute(
 | 
			
		||||
        """
 | 
			
		||||
        create table if not exists users(
 | 
			
		||||
            id integer primary key autoincrement,
 | 
			
		||||
@@ -34,8 +39,8 @@ def db_init(db_file):
 | 
			
		||||
        )
 | 
			
		||||
        """
 | 
			
		||||
    )
 | 
			
		||||
    cursor.close()
 | 
			
		||||
    conn.close()
 | 
			
		||||
    db_write_logs("数据库初始化成功!", "login_ui")
 | 
			
		||||
    db_close()
 | 
			
		||||
 | 
			
		||||
def db_lock(func):
 | 
			
		||||
    def wrapper(*args, **kwargs):
 | 
			
		||||
@@ -49,28 +54,22 @@ def db_lock(func):
 | 
			
		||||
 | 
			
		||||
def db_backup():
 | 
			
		||||
    t = time.strftime("%Y%m%d%H%M%S", time.localtime())
 | 
			
		||||
    db_file = clibs.base_path / "assets/database/toolbox.db"
 | 
			
		||||
    db_file_backup = clibs.base_path / f"assets/database/toolbox.{t}.db"
 | 
			
		||||
    if not (db_file.exists() and db_file.is_file()):
 | 
			
		||||
        db_init(db_file)
 | 
			
		||||
    else:
 | 
			
		||||
        db_file_backup.write_bytes(db_file.read_bytes())
 | 
			
		||||
        db_dir = clibs.base_path / "assets/database"
 | 
			
		||||
        db_list = [db for db in db_dir.glob("*.db")]
 | 
			
		||||
        for db in sorted(db_list)[:-clibs.account["maximum_db_number"]]:
 | 
			
		||||
            db.unlink()
 | 
			
		||||
    db_file_backup.write_bytes(clibs.db_file.read_bytes())
 | 
			
		||||
    db_dir = clibs.base_path / "assets/database"
 | 
			
		||||
    db_list = [db for db in db_dir.glob("*.db")]
 | 
			
		||||
    for db in sorted(db_list)[:-clibs.config["maximum_db_number"]]:
 | 
			
		||||
        db.unlink()
 | 
			
		||||
 | 
			
		||||
def db_conn():
 | 
			
		||||
    db_file = clibs.base_path / "assets/database/toolbox.db"
 | 
			
		||||
    conn = sqlite3.connect(db_file, isolation_level=None, check_same_thread=False, cached_statements=2048, timeout=3.0)
 | 
			
		||||
    cursor = conn.cursor()
 | 
			
		||||
    cursor.execute("PRAGMA journal_mode=wal")
 | 
			
		||||
    cursor.execute("PRAGMA wal_checkpoint=TRUNCATE")
 | 
			
		||||
    cursor.execute("PRAGMA synchronous=normal")
 | 
			
		||||
    cursor.execute("PRAGMA temp_store=memory")
 | 
			
		||||
    cursor.execute("PRAGMA mmap_size=30000000000")
 | 
			
		||||
    cursor.execute("PRAGMA cache_size=200000")
 | 
			
		||||
    return conn, cursor
 | 
			
		||||
    clibs.conn = sqlite3.connect(clibs.db_file, isolation_level=None, check_same_thread=False, cached_statements=2048, timeout=3.0)
 | 
			
		||||
    clibs.cursor = clibs.conn.cursor()
 | 
			
		||||
    clibs.cursor.execute("PRAGMA journal_mode=wal")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA wal_checkpoint=TRUNCATE")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA synchronous=normal")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA temp_store=memory")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA mmap_size=30000000000")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA cache_size=200000")
 | 
			
		||||
 | 
			
		||||
@db_lock
 | 
			
		||||
def db_close():
 | 
			
		||||
@@ -81,9 +80,16 @@ def db_close():
 | 
			
		||||
    clibs.conn, clibs.cursor = None, None
 | 
			
		||||
 | 
			
		||||
@db_lock
 | 
			
		||||
def db_write_logs(content, module, level="info"):
 | 
			
		||||
    if level.lower() not in ["debug", "info", "warning", "error", "exception"]:
 | 
			
		||||
def db_write_logs(content, module="", level="info"):
 | 
			
		||||
    if module == "":
 | 
			
		||||
        frame = currentframe().f_back
 | 
			
		||||
        module_name = frame.f_globals["__name__"]
 | 
			
		||||
        line_no = frame.f_lineno
 | 
			
		||||
        module = f"{module_name}.{line_no}"
 | 
			
		||||
 | 
			
		||||
    if level.lower() not in ["info", "warning", "error", "exception"]:
 | 
			
		||||
        level = "unknown"
 | 
			
		||||
 | 
			
		||||
    clibs.cursor.execute(f"INSERT INTO logs (level, module, content) VALUES (?, ?, ?)", (level, module, content))
 | 
			
		||||
 | 
			
		||||
@db_lock
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user