mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-17 16:17:02 +08:00
feat: add check and new function
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
import copy
|
||||||
import sys
|
import sys
|
||||||
from functools import singledispatch
|
from functools import singledispatch
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -34,8 +35,9 @@ def save_data(blocks: list[dict[str, Any]]):
|
|||||||
for block in blocks:
|
for block in blocks:
|
||||||
block_path: Path = data_path / (block["name"] + ".toml")
|
block_path: Path = data_path / (block["name"] + ".toml")
|
||||||
if not block_path.is_file():
|
if not block_path.is_file():
|
||||||
logger.error(f"Block: {block_path} is not Exists or a File")
|
logger.warning(
|
||||||
continue
|
f"Block: {block_path} is not Exists and Will Create A New One"
|
||||||
|
)
|
||||||
block_path.write_text(pytomlpp.dumps(block))
|
block_path.write_text(pytomlpp.dumps(block))
|
||||||
|
|
||||||
|
|
||||||
@@ -108,6 +110,37 @@ def _(name: str):
|
|||||||
logger.error(f"Block Name: {name} Not Find")
|
logger.error(f"Block Name: {name} Not Find")
|
||||||
|
|
||||||
|
|
||||||
|
def change_key(block: dict[str, Any], key: str, value: str):
|
||||||
|
if type(block[key]) is str:
|
||||||
|
block[key] = value
|
||||||
|
elif type(block[key]) is int:
|
||||||
|
try:
|
||||||
|
v = int(value)
|
||||||
|
block[key] = v
|
||||||
|
except ValueError:
|
||||||
|
logger.error("The Value Is Not A Int")
|
||||||
|
return False
|
||||||
|
elif type(block[key]) is bool:
|
||||||
|
if value.lower() == "true" or value.lower() == "t":
|
||||||
|
block[key] = True
|
||||||
|
elif value.lower() == "false" or value.lower() == "f":
|
||||||
|
block[key] = False
|
||||||
|
else:
|
||||||
|
logger.error("The Value Is Not A Bool")
|
||||||
|
return False
|
||||||
|
elif type(block[key]) is float:
|
||||||
|
try:
|
||||||
|
v = float(value)
|
||||||
|
block[key] = v
|
||||||
|
except ValueError:
|
||||||
|
logger.error("The Value Is Not A Float")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
logger.error("Unkown Key Type")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def handle_change(block: dict[str, Any]) -> dict[str, Any]:
|
def handle_change(block: dict[str, Any]) -> dict[str, Any]:
|
||||||
print("Please Input Block Key, Input exit or e to Exit")
|
print("Please Input Block Key, Input exit or e to Exit")
|
||||||
while True:
|
while True:
|
||||||
@@ -118,43 +151,19 @@ def handle_change(block: dict[str, Any]) -> dict[str, Any]:
|
|||||||
logger.error("The Key Is Not Exists!")
|
logger.error("The Key Is Not Exists!")
|
||||||
continue
|
continue
|
||||||
value = input("Value: ")
|
value = input("Value: ")
|
||||||
|
old_name = block[key]
|
||||||
if isinstance(block[key], str):
|
if change_key(block, key, value):
|
||||||
if value.lower() == "exit" or value.lower() == "e":
|
print("Change Success")
|
||||||
print(f"The Value Is {value}, Do You Want to Exit or Write (Y/N)")
|
if key == "name":
|
||||||
ans = input()
|
old_path: Path = data_path / (old_name + ".toml")
|
||||||
if ans.lower() == "y":
|
try:
|
||||||
break
|
old_path.unlink()
|
||||||
elif ans.lower() != "n":
|
except FileNotFoundError:
|
||||||
logger.error(f"Unknow {ans}")
|
logger.warning(
|
||||||
continue
|
f"Name Change But Old File {old_name}.toml is Not Exists!"
|
||||||
block[key] = value
|
)
|
||||||
elif isinstance(block[key], int):
|
|
||||||
try:
|
|
||||||
v = int(value)
|
|
||||||
block[key] = v
|
|
||||||
except ValueError:
|
|
||||||
logger.error("The Value Is Not A Int")
|
|
||||||
continue
|
|
||||||
elif isinstance(block[key], bool):
|
|
||||||
if value.lower() == "true":
|
|
||||||
block[key] = True
|
|
||||||
elif value.lower() == "false":
|
|
||||||
block[key] = False
|
|
||||||
else:
|
|
||||||
logger.error("The Value Is Not A Bool")
|
|
||||||
continue
|
|
||||||
elif isinstance(block[key], float):
|
|
||||||
try:
|
|
||||||
v = float(value)
|
|
||||||
block[key] = v
|
|
||||||
except ValueError:
|
|
||||||
logger.error("The Value Is Not A Float")
|
|
||||||
continue
|
|
||||||
else:
|
else:
|
||||||
logger.error("Unkown Key Type")
|
print("Change Fail")
|
||||||
continue
|
|
||||||
print("Change Success")
|
|
||||||
pprint(block)
|
pprint(block)
|
||||||
return block
|
return block
|
||||||
|
|
||||||
@@ -229,16 +238,88 @@ def check_path():
|
|||||||
|
|
||||||
logger.info(f"Work Path {work_path.resolve()}")
|
logger.info(f"Work Path {work_path.resolve()}")
|
||||||
logger.info(f"Script Dir {sys.path[0]}")
|
logger.info(f"Script Dir {sys.path[0]}")
|
||||||
|
data_exists = True
|
||||||
if not data_path.exists():
|
if not data_path.exists():
|
||||||
logger.error(f"Blocks Data Path {data_path} not Exists!")
|
logger.error(f"Blocks Data Path {data_path} not Exists!")
|
||||||
|
data_exists = False
|
||||||
else:
|
else:
|
||||||
logger.info(f"Blocks Data Path {data_path}")
|
logger.info(f"Blocks Data Path {data_path}")
|
||||||
|
texture_exists = True
|
||||||
if not texture_path.exists():
|
if not texture_path.exists():
|
||||||
logger.error(f"Blocks Texture Path {texture_path} not Exists!")
|
logger.error(f"Blocks Texture Path {texture_path} not Exists!")
|
||||||
|
texture_exists = False
|
||||||
else:
|
else:
|
||||||
logger.info(f"Blocks Texture Path {texture_path}")
|
logger.info(f"Blocks Texture Path {texture_path}")
|
||||||
|
return data_exists and texture_exists
|
||||||
|
|
||||||
|
|
||||||
|
def check_integrity():
|
||||||
|
find_error = False
|
||||||
|
errors = 0
|
||||||
|
if check_path():
|
||||||
|
blocks = collect_blocks()
|
||||||
|
template_path = data_path / "template.toml"
|
||||||
|
if not template_path.is_file():
|
||||||
|
logger.error("Template.toml is not Exists!")
|
||||||
|
find_error = True
|
||||||
|
errors += 1
|
||||||
|
return
|
||||||
|
template_block = pytomlpp.loads(template_path.read_text(encoding="utf-8"))
|
||||||
|
n = len(blocks)
|
||||||
|
for i in range(n):
|
||||||
|
if "id" not in blocks[i]:
|
||||||
|
logger.error(f"Id: {i} not Exists!")
|
||||||
|
find_error = True
|
||||||
|
errors += 1
|
||||||
|
continue
|
||||||
|
if blocks[i]["id"] != i:
|
||||||
|
logger.error(
|
||||||
|
f"Id Error, Block {blocks[i].get('name', 'Unknow')} Id Should Be {i} Instead of {blocks[i]['id']}"
|
||||||
|
)
|
||||||
|
find_error = True
|
||||||
|
errors += 1
|
||||||
|
for key, value in template_block.items():
|
||||||
|
if key not in blocks[i]:
|
||||||
|
logger.error(
|
||||||
|
f"Key Error, Block {blocks[i].get('name', 'Unknow')} Key {key} not Exists!"
|
||||||
|
)
|
||||||
|
find_error = True
|
||||||
|
errors += 1
|
||||||
|
continue
|
||||||
|
if type(blocks[i][key]) is not type(value):
|
||||||
|
logger.error(
|
||||||
|
f"Value Type Error, Block {blocks[i].get(key, 'Unknow')} The Type Should Be {type(value)}, Instead of {type(blocks[i][key])}"
|
||||||
|
)
|
||||||
|
find_error = True
|
||||||
|
errors += 1
|
||||||
|
if find_error:
|
||||||
|
logger.error(f"Find {errors} Errors")
|
||||||
|
else:
|
||||||
|
logger.info("No Error")
|
||||||
|
|
||||||
|
|
||||||
|
def add_new_block():
|
||||||
|
blocks = collect_blocks()
|
||||||
|
template_path = data_path / "template.toml"
|
||||||
|
if not template_path.is_file():
|
||||||
|
logger.error("Template.toml is not Exists, Can't Create A New Block!")
|
||||||
|
return
|
||||||
|
template_block = pytomlpp.loads(template_path.read_text(encoding="utf-8"))
|
||||||
|
new_block = copy.deepcopy(template_block)
|
||||||
|
num = len(blocks)
|
||||||
|
logger.info(f"New Block Id is {num}")
|
||||||
|
new_block["id"] = num
|
||||||
|
for key in template_block:
|
||||||
|
if key == "id":
|
||||||
|
continue
|
||||||
|
nvalue = input(f"Input {key}: ")
|
||||||
|
if not change_key(new_block, key, nvalue):
|
||||||
|
logger.error(f"Add Key {key} Value {nvalue} Fail")
|
||||||
|
return
|
||||||
|
new_block_path: Path = data_path / (new_block["name"] + ".toml")
|
||||||
|
new_block_path.write_text(pytomlpp.dumps(new_block))
|
||||||
|
logger.info("Successfully Add New Block!")
|
||||||
|
pprint(new_block)
|
||||||
|
|
||||||
|
|
||||||
def handle_args(args: argparse.Namespace):
|
def handle_args(args: argparse.Namespace):
|
||||||
@@ -269,6 +350,10 @@ def handle_args(args: argparse.Namespace):
|
|||||||
change_data(id)
|
change_data(id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
change_data(args.change)
|
change_data(args.change)
|
||||||
|
if args.check:
|
||||||
|
check_integrity()
|
||||||
|
if args.new:
|
||||||
|
add_new_block()
|
||||||
|
|
||||||
|
|
||||||
def init_parser(parser: argparse.ArgumentParser):
|
def init_parser(parser: argparse.ArgumentParser):
|
||||||
@@ -295,6 +380,10 @@ def init_parser(parser: argparse.ArgumentParser):
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-c", "--change", nargs="?", const="EMPTY", help="Change Block Data"
|
"-c", "--change", nargs="?", const="EMPTY", help="Change Block Data"
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-C", "--check", action="store_true", help="Check The Block Data Integrity"
|
||||||
|
)
|
||||||
|
parser.add_argument("-n", "--new", action="store_true", help="Add A New Block")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user