优化loader写法

This commit is contained in:
cxykevin 2024-02-10 20:54:35 +08:00
parent 397486253b
commit b6505aef27
1 changed files with 18 additions and 25 deletions

View File

@ -57,21 +57,16 @@ def version_check(configuration: dict, pkg_name: str) -> int:
def file_check(file_json, pkg_name): def file_check(file_json, pkg_name):
try: try:
if "compatibility" in file_json: for i in file_json.get("compatibility", {}).get("file", {}).get("must", []):
if "file" in file_json["compatibility"]: if not os.path.exists(i):
if "must" in file_json["compatibility"]["file"]: log.warn(f"load moudle [{pkg_name}] failed: " +
for i in file_json["compatibility"]["file"]["must"]: f"Cannot find file: [{i}]")
if not os.path.exists(i): return 8
log.warn(f"load moudle [{pkg_name}] failed: " + for i in file_json.get("compatibility", {}).get("file", {}).get("mustnot", []):
f"Cannot find file: [{i}]") if os.path.exists(i):
return 8 log.warn(f"load moudle [{pkg_name}] failed: " +
if "mustnot" in file_json["compatibility"]["file"]: f"Find incompatible file: [{i}]")
for i in file_json["compatibility"]["file"]["mustnot"]: return 9
if os.path.exists(i):
log.warn(f"load moudle [{pkg_name}] failed: " +
f"Find incompatible file: [{i}]")
return 9
except: except:
log.warn(f"load moudle [{pkg_name}] failed: " + log.warn(f"load moudle [{pkg_name}] failed: " +
"Unknown error in file check") "Unknown error in file check")
@ -91,17 +86,15 @@ def load_package(pkg_name):
return 1 return 1
try: try:
with open(pkg_path+"/"+"manifest.json", "r", encoding="utf-8") as file: with open(pkg_path+"/"+"manifest.json", "r", encoding="utf-8") as file:
file_str = file.read() file_json = json.load(json.decoder.JSONDecodeError)
except json.decoder.JSONDecodeError:
log.warn(f"load moudle [{pkg_name}] failed: " +
"Json syntax error")
return 3
except Exception as exp: except Exception as exp:
log.warn(f"load moudle [{pkg_name}] failed: " + log.warn(f"load moudle [{pkg_name}] failed: " +
"Unknown error in read file ("+repr(exp)+")") "Unknown error in read file ("+repr(exp)+")")
return 2 return 2
try:
file_json = json.loads(file_str)
except:
log.warn(f"load moudle [{pkg_name}] failed: " +
"Json syntax error")
return 3
for i in ("version", "name", "author", "introduce"): for i in ("version", "name", "author", "introduce"):
if i not in file_json: if i not in file_json:
log.warn(f"load moudle [{pkg_name}] failed: " + log.warn(f"load moudle [{pkg_name}] failed: " +