Skip to content

Commit

Permalink
Modify | File IO Performance
Browse files Browse the repository at this point in the history
  • Loading branch information
LxHTT committed Mar 9, 2024
1 parent 6b9acd9 commit 0a001e8
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 190 deletions.
8 changes: 3 additions & 5 deletions Adapters/Plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from Adapters.BasePlugin import BasePlugin, BasePluginLoader, BasePluginManager
from MCSL2Lib.Resources.icons import * # noqa: F401 F403
from MCSL2Lib.Widgets.pluginWidget import singlePluginWidget, PluginSwitchButton
from MCSL2Lib.utils import openLocalFile
from MCSL2Lib.utils import openLocalFile, readFile
from MCSL2Lib.variables import GlobalMCSL2Variables


Expand Down Expand Up @@ -77,8 +77,7 @@ def load(cls, pluginName: str) -> Plugin | None:
importedPlugin = importedPlugin.__getattribute__(pluginName)
try:
importedPlugin.pluginName = pluginName
with open(f"Plugins//{pluginName}//config.json", "r", encoding="utf-8") as f:
importedPluginConfig: dict = loads(f.read())
importedPluginConfig: dict = loads(readFile(f"Plugins//{pluginName}//config.json"))
importedPlugin.version = importedPluginConfig.get("version")
importedPlugin.description = importedPluginConfig.get("description")
importedPlugin.author = importedPluginConfig.get("author")
Expand All @@ -97,8 +96,7 @@ def load(cls, pluginName: str) -> Plugin | None:
def getInfo(cls, pluginName: str) -> PluginType:
pluginType = PluginType()
pluginType.pluginName = pluginName
with open(f"Plugins//{pluginName}//config.json", "r", encoding="utf-8") as f:
importedPluginConfig: dict = loads(f.read())
importedPluginConfig: dict = loads(readFile(f"Plugins//{pluginName}//config.json"))
pluginType.version = importedPluginConfig.get("version")
pluginType.description = importedPluginConfig.get("description")
pluginType.author = importedPluginConfig.get("author")
Expand Down
11 changes: 4 additions & 7 deletions MCSL2Lib/Pages/configEditorPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
)

from MCSL2Lib.ProgramControllers.interfaceController import EraseStackedWidget

from MCSL2Lib.utils import readFile, writeFile
from MCSL2Lib.variables import ServerVariables


Expand Down Expand Up @@ -193,8 +193,7 @@ def createConfigEditor(self, selected, deselected):
return
else: # Add New Tab
try:
with open(filePath, "r", encoding="utf-8") as f:
text = f.read()
text = readFile(filePath)
except Exception as e:
InfoBar.info(
title="抱歉",
Expand Down Expand Up @@ -230,11 +229,9 @@ def createConfigEditor(self, selected, deselected):
) # 新建标签页不触发currentChanged,这里手动触发

def saveConfig(self, filePath: str, auto: bool = True):
with open(filePath, "r", encoding="utf-8") as f:
tmpText = f.read()
tmpText = readFile(filePath)
if (newText := self.editorDict[filePath].toPlainText()) != tmpText:
with open(filePath, "w+", encoding="utf-8") as nf:
nf.write(newText)
writeFile(filePath, newText)
InfoBar.info(
title="提示",
content=f"已{'自动' if auto else ''}保存 {osp.basename(filePath)}",
Expand Down
42 changes: 10 additions & 32 deletions MCSL2Lib/Pages/configurePage.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
ServerVariables,
SettingsVariables,
)
from MCSL2Lib.utils import MCSL2Logger
from MCSL2Lib.utils import MCSL2Logger, readFile, writeFile

configureServerVariables = ConfigureServerVariables()
settingsVariables = SettingsVariables()
Expand Down Expand Up @@ -1778,19 +1778,9 @@ def saveNewServer(self):

# 写入全局配置
try:
with open(
r"MCSL2/MCSL2_ServerList.json", "r", encoding="utf-8"
) as globalServerListFile:
# old
globalServerList = loads(globalServerListFile.read())
globalServerListFile.close()

with open(
r"MCSL2/MCSL2_ServerList.json", "w+", encoding="utf-8"
) as newGlobalServerListFile:
# 添加新的
globalServerList["MCSLServerList"].append(serverConfig)
newGlobalServerListFile.write(dumps(globalServerList, indent=4))
globalServerList = loads(readFile(r"MCSL2/MCSL2_ServerList.json"))
globalServerList["MCSLServerList"].append(serverConfig)
writeFile(r"MCSL2/MCSL2_ServerList.json", dumps(globalServerList, indent=4))
exitCode = 0
except Exception as e:
exitCode = 1
Expand All @@ -1799,12 +1789,10 @@ def saveNewServer(self):
# 写入单独配置
try:
if not cfg.get(cfg.onlySaveGlobalServerConfig):
with open(
writeFile(
f"Servers//{configureServerVariables.serverName}//MCSL2ServerConfig.json",
"w+",
encoding="utf-8",
) as serverListFile:
serverListFile.write(dumps(serverConfig, indent=4))
dumps(serverConfig, indent=4),
)
else:
InfoBar.info(
title=self.tr("功能提醒"),
Expand Down Expand Up @@ -1913,19 +1901,9 @@ def addNewServerRollback(self):
# 删除文件夹
rmtree(serverDir)
# 删除全局配置
with open(
r"MCSL2/MCSL2_ServerList.json", "r", encoding="utf-8"
) as globalServerListFile:
# old
globalServerList = loads(globalServerListFile.read())
globalServerListFile.close()

with open(
r"MCSL2/MCSL2_ServerList.json", "w+", encoding="utf-8"
) as newGlobalServerListFile:
# 删除新的
globalServerList["MCSLServerList"].pop()
newGlobalServerListFile.write(dumps(globalServerList, indent=4))
globalServerList = loads(readFile(r"MCSL2/MCSL2_ServerList.json"))
globalServerList["MCSLServerList"].pop()
writeFile(r"MCSL2/MCSL2_ServerList.json", dumps(globalServerList, indent=4))

@pyqtSlot(bool)
def afterInstallingForge(self, installFinished, args=...):
Expand Down
38 changes: 10 additions & 28 deletions MCSL2Lib/Pages/serverManagerPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@
from MCSL2Lib.singleton import Singleton

# from MCSL2Lib.Controllers.interfaceController import ChildStackedWidget
from MCSL2Lib.utils import openLocalFile, readGlobalServerConfig
from MCSL2Lib.utils import openLocalFile, readGlobalServerConfig, MCSL2Logger, readFile, writeFile
from MCSL2Lib.variables import GlobalMCSL2Variables, EditServerVariables
from MCSL2Lib.utils import MCSL2Logger


editServerVariables = EditServerVariables()
Expand Down Expand Up @@ -1265,19 +1264,10 @@ def saveEditedServer(self):

# 写入全局配置
try:
with open(
r"MCSL2/MCSL2_ServerList.json", "r", encoding="utf-8"
) as globalServerListFile:
# old
globalServerList = loads(globalServerListFile.read())

with open(
r"MCSL2/MCSL2_ServerList.json", "w+", encoding="utf-8"
) as newGlobalServerListFile:
# 添加新的
globalServerList["MCSLServerList"].pop(self.serverIndex)
globalServerList["MCSLServerList"].insert(0, serverConfig)
newGlobalServerListFile.write(dumps(globalServerList, indent=4))
globalServerList = readFile(r"MCSL2/MCSL2_ServerList.json")
globalServerList["MCSLServerList"].pop(self.serverIndex)
globalServerList["MCSLServerList"].insert(0, serverConfig)
writeFile(r"MCSL2/MCSL2_ServerList.json", dumps(globalServerList, indent=4))
exitCode = 0
except Exception as e:
exitCode = 1
Expand All @@ -1286,12 +1276,10 @@ def saveEditedServer(self):
# 写入单独配置
try:
if not cfg.get(cfg.onlySaveGlobalServerConfig):
with open(
writeFile(
f"Servers//{editServerVariables.serverName}//MCSL2ServerConfig.json",
"w+",
encoding="utf-8",
) as serverListFile:
serverListFile.write(dumps(serverConfig, indent=4))
dumps(serverConfig, indent=4),
)
else:
InfoBar.info(
title=self.tr("功能提醒"),
Expand Down Expand Up @@ -1448,15 +1436,9 @@ def run(self):
exit1Msg = ""
# 删配置
try:
with open(
r"MCSL2/MCSL2_ServerList.json", "r", encoding="utf-8"
) as RglobalServerListFile:
globalServerList = loads(RglobalServerListFile.read())
globalServerList = loads(readFile(r"MCSL2/MCSL2_ServerList.json"))
globalServerList["MCSLServerList"].pop(self.index)
with open(
r"MCSL2/MCSL2_ServerList.json", "w+", encoding="utf-8"
) as WglobalServerConfigFile:
WglobalServerConfigFile.write(dumps(globalServerList, indent=4))
writeFile(r"MCSL2/MCSL2_ServerList.json", dumps(globalServerList, indent=4))
except Exception as e:
self.exitCode.emit(1)
exit1Msg += f"\n{e}"
Expand Down
85 changes: 39 additions & 46 deletions MCSL2Lib/ProgramControllers/aria2ClientController.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

from MCSL2Lib.ProgramControllers.settingsController import cfg
from MCSL2Lib.ProgramControllers.networkController import MCSLNetworkSession
from MCSL2Lib.utils import workingThreads
from MCSL2Lib.utils import MCSL2Logger
from MCSL2Lib.utils import readFile, workingThreads, MCSL2Logger, writeFile, readBytesFile


class Aria2Controller:
Expand Down Expand Up @@ -586,39 +585,38 @@ def files(self):


def initializeAria2Configuration(running: bool = False):
with open(r"MCSL2/Aria2/aria2.conf", "w+", encoding="utf-8") as Aria2ConfigFile:
Aria2ConfigFile.write(
"file-allocation=none\n"
"continue=true\n"
"always-resume=false\n"
"no-file-allocation-limit=10M\n"
"max-concurrent-downloads=5\n"
"remote-time=true\n"
"min-split-size=5M\n"
f"split={cfg.get(cfg.aria2Thread)}\n"
"disable-ipv6=false\n"
"enable-http-pipelining=false\n"
"max-connection-per-server=16\n"
"enable-rpc=true\n"
"rpc-allow-origin-all=true\n"
"rpc-listen-all=true\n"
"event-poll=select\n"
"rpc-listen-port=6800\n"
"force-save=false\n"
"check-certificate=false\n"
"dir=MCSL2/Downloads\n"
"input-file=MCSL2/Aria2/aria2.session\n"
"save-session=MCSL2/Aria2/aria2.session\n"
"max-overall-download-limit=0\n"
"max-download-limit=0\n"
"http-accept-gzip=true\n"
"async-dns-server=119.29.29.29,223.5.5.5\n"
"console-log-level=error\n"
f"user-agent={MCSLNetworkSession.MCSLNetworkHeaders['User-Agent']}"
)
writeFile(
r"MCSL2/Aria2/aria2.conf",
"file-allocation=none\n"
"continue=true\n"
"always-resume=false\n"
"no-file-allocation-limit=10M\n"
"max-concurrent-downloads=5\n"
"remote-time=true\n"
"min-split-size=5M\n"
f"split={cfg.get(cfg.aria2Thread)}\n"
"disable-ipv6=false\n"
"enable-http-pipelining=false\n"
"max-connection-per-server=16\n"
"enable-rpc=true\n"
"rpc-allow-origin-all=true\n"
"rpc-listen-all=true\n"
"event-poll=select\n"
"rpc-listen-port=6800\n"
"force-save=false\n"
"check-certificate=false\n"
"dir=MCSL2/Downloads\n"
"input-file=MCSL2/Aria2/aria2.session\n"
"save-session=MCSL2/Aria2/aria2.session\n"
"max-overall-download-limit=0\n"
"max-download-limit=0\n"
"http-accept-gzip=true\n"
"async-dns-server=119.29.29.29,223.5.5.5\n"
"console-log-level=error\n"
f"user-agent={MCSLNetworkSession.MCSLNetworkHeaders['User-Agent']}",
)
if not running:
with open(r"MCSL2/Aria2/aria2.session", "w+", encoding="utf-8") as Aria2SessionFile:
Aria2SessionFile.write("")
writeFile("MCSL2/Aria2/aria2.session", "")


entries = {}
Expand Down Expand Up @@ -824,13 +822,11 @@ def fileExisted(self):
mkdir(osp.join("MCSL2", "Downloads"))
if not osp.exists(DL_EntryManager.file):
MCSL2Logger.info(f"创建下载记录文件: {DL_EntryManager.file}")
with open(DL_EntryManager.file, "w", encoding="utf-8") as f:
f.write("{}")
writeFile(DL_EntryManager.file, "{}")

def read(self, check=True, autoDelete=True):
self.fileExisted()
with open(DL_EntryManager.file, "r") as f:
self.entries = json.load(f)
self.entries = json.loads(readFile(DL_EntryManager.file))
for coreName, coreData in self.entries.copy().items():
if check and not self.checkCoreEntry(coreName, coreData["md5"], autoDelete):
MCSL2Logger.info(f"删除不完整的核心文件记录: {coreName}")
Expand Down Expand Up @@ -861,8 +857,7 @@ def addCoreEntry(self, coreName: str, extraData: dict):
"""
coreFileName = osp.join(self.path, coreName)
# 计算md5
with open(coreFileName, "rb") as f:
md5 = hashlib.md5(f.read()).hexdigest()
md5 = hashlib.md5(readBytesFile(coreFileName)).hexdigest()
extraData.update({"md5": md5})
self.addEntry(coreName, extraData)

Expand Down Expand Up @@ -890,8 +885,7 @@ def flush(self):
"""
self.fileExisted()
self.mutex.lock()
with open(self.file, "w", encoding="utf-8") as f:
json.dump(self.entries, f, indent=4, ensure_ascii=False, sort_keys=True)
writeFile(self.file, json.dumps(self.entries, indent=4, ensure_ascii=False, sort_keys=True))
self.mutex.unlock()

def checkCoreEntry(self, coreName: str, originMd5: str, autoDelete=False):
Expand All @@ -904,8 +898,7 @@ def checkCoreEntry(self, coreName: str, originMd5: str, autoDelete=False):
coreFileName = osp.join(self.path, coreName)
if osp.exists(coreFileName):
# 计算md5
with open(coreFileName, "rb") as f:
fileMd5 = hashlib.md5(f.read()).hexdigest()
fileMd5 = hashlib.md5(readBytesFile(coreFileName)).hexdigest()
if fileMd5 == originMd5:
return True
if autoDelete:
Expand Down Expand Up @@ -965,8 +958,8 @@ def GetEntries(self, check=True, autoDelete=True):
rv = entries_snapshot.copy()

# # 检查记录一致性
# with open(self.file, "r") as f:
# fileRecordedEntries = json.load(f)
# f = readFile(self.file)
# fileRecordedEntries = json.load(f)
#
# if fileRecordedEntries != entries_snapshot: # 如果数据不一致则重新读取,并更新记录.用于加速结果的生成 # noqa: E501
# print("记录不一致,重新计算各条目完整性,并更新本地记录")
Expand Down
16 changes: 8 additions & 8 deletions MCSL2Lib/ProgramControllers/javaDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from re import search

from PyQt5.QtCore import QThread, pyqtSignal, QProcess
from MCSL2Lib.utils import MCSL2Logger
from MCSL2Lib.utils import MCSL2Logger, readFile, writeFile


foundJava = []
Expand Down Expand Up @@ -199,20 +199,20 @@ def loadJavaList():

if not osp.exists("MCSL2/MCSL2_DetectedJava.json"):
return []
with open("MCSL2/MCSL2_DetectedJava.json", "r", encoding="utf-8") as f:
foundedJava = json.load(f)
return [Java(e["Path"], e["Version"]) for e in foundedJava["java"]]
foundedJava = json.loads(readFile("MCSL2/MCSL2_DetectedJava.json"))
return [Java(e["Path"], e["Version"]) for e in foundedJava["java"]]


def saveJavaList(list_: list):
with open("MCSL2/MCSL2_DetectedJava.json", "w", encoding="utf-8") as f:
json.dump(
writeFile(
"MCSL2/MCSL2_DetectedJava.json",
json.dumps(
{"java": [j.json for j in list_]},
f,
ensure_ascii=False,
sort_keys=True,
indent=4,
)
),
)


def sortJavaList(list_: list, reverse=False):
Expand Down
Loading

0 comments on commit 0a001e8

Please sign in to comment.