总线通信架构整改完成
This commit is contained in:
		@@ -4,8 +4,9 @@ import atexit
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
base_path = Path(__file__).resolve().parent.parent.parent
 | 
			
		||||
db_file = base_path / "assets/database/toolbox.db"
 | 
			
		||||
lock = Lock()
 | 
			
		||||
account = None
 | 
			
		||||
config = None
 | 
			
		||||
code_dict = [4, 11, 4, 31, 22, 12, 19, 23, 7, 16, 7, 23, 1, 8, 7, 18, 27, 32, 28, 25, 7, 32, 9, 15, 2, 32, 0, 12, 26, 15, 14, 17]
 | 
			
		||||
username, password = "", ""
 | 
			
		||||
avatar = f"{base_path}/assets/media/avatar.jpg"
 | 
			
		||||
@@ -13,7 +14,7 @@ proverb = "佛曰:Time will say~"
 | 
			
		||||
bg = f"{base_path}/assets/media/bg.jpg"
 | 
			
		||||
win_width, win_height = 1100, 500
 | 
			
		||||
conn, cursor = None, None
 | 
			
		||||
listW_items = ["实用工具", "效率提升", "财务分析"]
 | 
			
		||||
listW_items = {"实用工具": "w10_practical", "效率提升": "w20_efficiency", "财务分析": "w30_financial"}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def delete_files_in_directory(directory):
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,19 @@
 | 
			
		||||
from functools import wraps
 | 
			
		||||
from codes.common import db_operation
 | 
			
		||||
from inspect import getfile
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
 | 
			
		||||
def handle_exception(module, stop: bool = False):
 | 
			
		||||
 | 
			
		||||
def handle_exception(stop: bool = False):
 | 
			
		||||
    def exceptions(func):
 | 
			
		||||
        module = Path(getfile(func)).stem
 | 
			
		||||
        func_name = func.__name__
 | 
			
		||||
        @wraps(func)
 | 
			
		||||
        def wrapper(*args, **kwargs):
 | 
			
		||||
            try:
 | 
			
		||||
                return func(*args, **kwargs)
 | 
			
		||||
            except Exception as e:
 | 
			
		||||
                db_operation.db_write_logs(e, module, "exception")
 | 
			
		||||
                db_operation.db_write_logs(str(e), "@".join([func_name, module]), "exception")
 | 
			
		||||
                if stop:
 | 
			
		||||
                    raise e
 | 
			
		||||
        return wrapper
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								toolbox/codes/common/signal_bus.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								toolbox/codes/common/signal_bus.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
from PySide6.QtCore import QObject, Signal
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SignalBus(QObject):
 | 
			
		||||
    instance = None
 | 
			
		||||
 | 
			
		||||
    def __new__(cls):
 | 
			
		||||
        if cls.instance is None:
 | 
			
		||||
            cls.instance = super(SignalBus, cls).__new__(cls)
 | 
			
		||||
        return cls.instance
 | 
			
		||||
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
 | 
			
		||||
    current_stacked_page = Signal(str)         # 获取当前页面的page_id
 | 
			
		||||
    init_stacked_page = Signal(str)            # 设置打开侧边栏后的初始页面
 | 
			
		||||
    qa_stacked_page_switch = Signal(str)       # 切换stacked widget页面
 | 
			
		||||
    home_overlay_trigger = Signal()            # 触发软件锁屏
 | 
			
		||||
    home_overlay_auth = Signal()               # 触发密码框的显示与隐藏
 | 
			
		||||
    home_overlay_close = Signal()              # 退出锁屏后的收尾信号
 | 
			
		||||
    list_widget_click = Signal(str)            # 触发点击list widget信号
 | 
			
		||||
    list_widget_on_off = Signal(bool)          # 主动控制是否显示list widget组件
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
signal_bus = SignalBus()
 | 
			
		||||
		Reference in New Issue
	
	Block a user