101 lines
3.1 KiB
Python
101 lines
3.1 KiB
Python
|
|
#!/opt/homebrew/bin/python3
|
|||
|
|
# -*- coding:utf-8 -*-
|
|||
|
|
|
|||
|
|
import json
|
|||
|
|
import os
|
|||
|
|
from datetime import datetime
|
|||
|
|
from PyQt6.QtCore import *
|
|||
|
|
from models import Project, Session
|
|||
|
|
from logs import log
|
|||
|
|
from PyQt6.QtGui import *
|
|||
|
|
|
|||
|
|
class ProjectBackend(QObject):
|
|||
|
|
def __init__(self):
|
|||
|
|
super().__init__()
|
|||
|
|
self.basePath = ''
|
|||
|
|
self.projectName = ''
|
|||
|
|
self.projectPath = ''
|
|||
|
|
self.projectPath_log = ''
|
|||
|
|
self.projectPath_screenshot = ''
|
|||
|
|
self.projectPath_tsdb = ''
|
|||
|
|
|
|||
|
|
def setConfig(self,conf):
|
|||
|
|
self.basePath = conf['base_path']
|
|||
|
|
if self.projectName:
|
|||
|
|
self.createProjectPath(self.projectName)
|
|||
|
|
|
|||
|
|
def setProjectPath(self, name):
|
|||
|
|
self.projectName = name
|
|||
|
|
self.projectPath = os.path.join(self.basePath, self.projectName)
|
|||
|
|
self.projectPath_log = os.path.join(self.projectPath, 'log')
|
|||
|
|
self.projectPath_screenshot = os.path.join(self.projectPath, 'screenshot')
|
|||
|
|
self.projectPath_tsdb = os.path.join(self.projectPath, 'tsdb')
|
|||
|
|
self.projectLog = f'{os.path.join(self.projectPath_log, self.projectName)}.txt'
|
|||
|
|
return self.projectPath
|
|||
|
|
|
|||
|
|
def createProjectPath(self, name):
|
|||
|
|
path = self.setProjectPath(name)
|
|||
|
|
os.makedirs(self.projectPath_log, 0o755, True)
|
|||
|
|
os.makedirs(self.projectPath_screenshot, 0o755, True)
|
|||
|
|
os.makedirs(self.projectPath_tsdb, 0o755, True)
|
|||
|
|
return path
|
|||
|
|
|
|||
|
|
def writeLogFile(self, t, data):
|
|||
|
|
try:
|
|||
|
|
with open(self.projectLog, 'a') as file:
|
|||
|
|
file.write(f"[{t}] : {data}{os.linesep}")
|
|||
|
|
return True, None
|
|||
|
|
except Exception as e:
|
|||
|
|
log.error(e)
|
|||
|
|
return False, None
|
|||
|
|
|
|||
|
|
# 打开日志文件夹
|
|||
|
|
def open_log_folder(self):
|
|||
|
|
try:
|
|||
|
|
url = QUrl.fromLocalFile(self.projectPath_log)
|
|||
|
|
QDesktopServices.openUrl(url)
|
|||
|
|
return True, None
|
|||
|
|
except Exception as e:
|
|||
|
|
log.error(e)
|
|||
|
|
return False, None
|
|||
|
|
|
|||
|
|
# 添加工程
|
|||
|
|
def add_project(self, json_str):
|
|||
|
|
json_dict = json.loads(json_str)
|
|||
|
|
name = json_dict['name']
|
|||
|
|
ok, data = Session.queryByName(Project, name)
|
|||
|
|
if ok:
|
|||
|
|
return False, None
|
|||
|
|
|
|||
|
|
json_dict['path'] = self.createProjectPath(name)
|
|||
|
|
json_dict['date'] = str(datetime.now())[:-7]
|
|||
|
|
return Session.addByClass(Project, json_dict)
|
|||
|
|
|
|||
|
|
# 更新工程str
|
|||
|
|
def update_project(self, json_str):
|
|||
|
|
json_dict = json.loads(json_str)
|
|||
|
|
json_dict['last_date'] = str(datetime.now())[:-7]
|
|||
|
|
id = json_dict.get('id')
|
|||
|
|
return Session.updateById(Project, id, json_dict)
|
|||
|
|
|
|||
|
|
# 删除工程
|
|||
|
|
def delete_project(self, id):
|
|||
|
|
return Session.deleteById(Project, id)
|
|||
|
|
|
|||
|
|
# 获取所有工程
|
|||
|
|
def get_projects(self):
|
|||
|
|
return Session.queryByAll(Project)
|
|||
|
|
|
|||
|
|
# 获取某个工程
|
|||
|
|
def get_project(self, id):
|
|||
|
|
return Session.queryById(Project, id)
|
|||
|
|
|
|||
|
|
# 切换工程
|
|||
|
|
def selected_project(self, name):
|
|||
|
|
try:
|
|||
|
|
return True, self.createProjectPath(name)
|
|||
|
|
except Exception as e:
|
|||
|
|
log.error(e)
|
|||
|
|
return False, None
|
|||
|
|
|
|||
|
|
projectBackend = ProjectBackend()
|