Skip to content

Commit

Permalink
comment rename support
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Oct 3, 2022
1 parent 96c7eeb commit 987e027
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ mcd_root/

`!!qb back [<slot>]` 回档为槽位 `<slot>` 的存档。

`!!qb del [<slot>]` 删除槽位 `<slot>` 的存档。
`!!qb del <slot>` 删除槽位 `<slot>` 的存档。默认为槽位 1

`!!qb rename <slot> <comment>` 修改槽位 `<slot>` 的注释,即重命名这一槽位

`!!qb confirm` 在执行 `back` 后使用,再次确认是否进行回档

Expand Down Expand Up @@ -203,6 +205,7 @@ mcd_root/
"make": 1,
"back": 2,
"del": 2,
"rename": 2,
"confirm": 1,
"abort": 1,
"reload": 2,
Expand Down
5 changes: 4 additions & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ mcd_root/

`!!qb back [<slot>]` Restore the world to slot 1. When `<slot>` parameter is set it will restore to slot `<slot>`

`!!qb del [<slot>]` Delete the world in slot `<slot>`
`!!qb del <slot>` Delete the world in slot `<slot>`

`!!qb rename <slot> <comment>` Modify the comment of slot `<slot>`, aka rename the slot

`!!qb confirm` Use after execute `back` to confirm restore execution

Expand Down Expand Up @@ -204,6 +206,7 @@ Default:
"make": 1,
"back": 2,
"del": 2,
"rename": 2,
"confirm": 1,
"abort": 1,
"reload": 2,
Expand Down
6 changes: 4 additions & 2 deletions lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ quick_backup_multi:
§d[Format]§r
§7{0}§r Display help message
§7{0} make §e[<comment>]§r Make a §abackup§r to slot §61§r. §e<comment>§r is an optional comment message
§7{0} back §6[<slot>]§r §cRestore§r the world to slot §6<slot>§r
§7{0} del §6[<slot>]§r §cDelete§r the world in slot §6<slot>§r
§7{0} back §6[<slot>]§r §cRestore§r the world to slot §6<slot>§r. Default: slot §61§r
§7{0} del §6<slot>§r §cDelete§r the world in slot §6<slot>§r
§7{0} rename §6<slot>§r §e<comment>§r §bModify§r the comment of slot §6<slot>§r, aka rename the slot
§7{0} confirm§r Use after execute back to confirm §crestore§r execution
§7{0} abort§r Abort backup §crestoring§r
§7{0} list§r Display slot information
Expand All @@ -27,6 +28,7 @@ quick_backup_multi:
delete: §aDeleting slot§r
create: §aBacking up§r
restore: §cRestoring§r
rename: §9Renaming§r

delete_backup:
success: §aSlot §6{0}§r delete success§r
Expand Down
6 changes: 4 additions & 2 deletions lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ quick_backup_multi:
§d【格式说明】§r
§7{0}§r 显示帮助信息
§7{0} make §e[<cmt>]§r 创建一个储存至槽位§61§r的§a备份§r。§e<cmt>§r为可选注释
§7{0} back §6[<slot>]§r §c回档§r为槽位§6<slot>§r的存档
§7{0} del §6[<slot>]§r §c删除§r槽位§6<slot>§r的存档
§7{0} back §6[<slot>]§r §c回档§r为槽位§6<slot>§r的存档。默认为槽位§61§r
§7{0} del §6<slot>§r §c删除§r槽位§6<slot>§r的存档
§7{0} rename §6<slot>§r §e<cmt>§r §b修改§r槽位§6<slot>§r的注释,即重命名这一槽位
§7{0} confirm§r 再次确认是否进行§c回档§r
§7{0} abort§r 在任何时候键入此指令可中断§c回档§r
§7{0} list§r 显示各槽位的存档信息
Expand All @@ -27,6 +28,7 @@ quick_backup_multi:
delete: §a删除槽位§r
create: §a备份§r
restore: §c回档§r
rename: §9重命名§r

delete_backup:
success: §a删除槽位§6{0}§r完成§r
Expand Down
75 changes: 54 additions & 21 deletions quick_backup_multi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import shutil
import time
from threading import Lock
from typing import Optional, Any, Callable
from typing import Optional, Any, Callable, Tuple

from mcdreforged.api.all import *

Expand Down Expand Up @@ -89,8 +89,8 @@ def get_slot_count():
return len(config.slots)


def get_slot_folder(slot: int):
return os.path.join(config.backup_path, f"slot{slot}")
def get_slot_path(slot: int):
return os.path.join(config.backup_path, 'slot{}'.format(slot))


def get_slot_info(slot: int):
Expand All @@ -100,7 +100,7 @@ def get_slot_info(slot: int):
:rtype: dict or None
"""
try:
with open(os.path.join(get_slot_folder(slot), 'info.json'), encoding='utf8') as f:
with open(os.path.join(get_slot_path(slot), 'info.json'), encoding='utf8') as f:
info = json.load(f)
except:
info = None
Expand Down Expand Up @@ -144,10 +144,10 @@ def mkdir(path: str):

mkdir(config.backup_path)
for i in range(get_slot_count()):
mkdir(get_slot_folder(i + 1))
mkdir(get_slot_path(i + 1))


def slot_check(source: CommandSource, slot: int):
def slot_check(source: CommandSource, slot: int) -> Optional[Tuple[int, dict]]:
if not 1 <= slot <= get_slot_count():
print_message(source, tr('unknown_slot', 1, get_slot_count()))
return None
Expand All @@ -159,6 +159,21 @@ def slot_check(source: CommandSource, slot: int):
return slot, slot_info


def create_slot_info(comment: Optional[str]) -> dict:
slot_info = {
'time': format_time(),
'time_stamp': time.time()
}
if comment is not None:
slot_info['comment'] = comment
return slot_info


def write_slot_info(slot_path: str, slot_info: dict):
with open(os.path.join(slot_path, 'info.json'), 'w', encoding='utf8') as f:
json.dump(slot_info, f, indent=4, ensure_ascii=False)


def single_op(name: RTextBase):
def wrapper(func: Callable):
@functools.wraps(func)
Expand All @@ -183,7 +198,23 @@ def delete_backup(source: CommandSource, slot: int):
if slot_check(source, slot) is None:
return
try:
shutil.rmtree(get_slot_folder(slot))
shutil.rmtree(get_slot_path(slot))
except Exception as e:
print_message(source, tr('delete_backup.fail', slot, e), tell=False)
else:
print_message(source, tr('delete_backup.success', slot), tell=False)


@new_thread('QBM - rename')
@single_op(tr('operations.rename'))
def rename_backup(source: CommandSource, slot: int, comment: str):
ret = slot_check(source, slot)
if ret is None:
return
try:
slot, slot_info = ret
slot_info['comment'] = comment
write_slot_info(get_slot_path(slot), slot_info)
except Exception as e:
print_message(source, tr('delete_backup.fail', slot, e), tell=False)
else:
Expand Down Expand Up @@ -223,12 +254,12 @@ def clean_up_slot_1():
if target_slot_idx is not None:
slot_info = get_slot_info(target_slot_idx)

folder = get_slot_folder(target_slot_idx)
folder = get_slot_path(target_slot_idx)
if os.path.isdir(folder):
shutil.rmtree(folder)
for i in reversed(range(1, target_slot_idx)): # n-1, n-2, ..., 1
os.rename(get_slot_folder(i), get_slot_folder(i + 1))
os.mkdir(get_slot_folder(1))
os.rename(get_slot_path(i), get_slot_path(i + 1))
os.mkdir(get_slot_path(1))

server_inst.logger.info('Slot {} ({}) is deleted to provide spaces for the incoming backup'.format(target_slot_idx, format_slot_info(info_dict=slot_info)))

Expand Down Expand Up @@ -267,19 +298,14 @@ def _create_backup(source: CommandSource, comment: Optional[str]):
print_message(source, tr('create_backup.abort.no_slot'), tell=False)
return

slot_path = get_slot_folder(1)
slot_path = get_slot_path(1)

# copy worlds to backup slot
copy_worlds(config.server_path, slot_path)

# create info.json
slot_info = {
'time': format_time(),
'time_stamp': time.time()
}
if comment is not None:
slot_info['comment'] = comment
with open(os.path.join(slot_path, 'info.json'), 'w', encoding='utf8') as f:
json.dump(slot_info, f, indent=4, ensure_ascii=False)
slot_info = create_slot_info(comment)
write_slot_info(slot_path, slot_info)

# done
end_time = time.time()
Expand Down Expand Up @@ -356,7 +382,7 @@ def _do_restore_backup(source: CommandSource, slot: int):
f.write('Overwrite time: {}\n'.format(format_time()))
f.write('Confirmed by: {}'.format(source))

slot_folder = get_slot_folder(slot)
slot_folder = get_slot_path(slot)
server_inst.logger.info('Deleting world')
remove_worlds(config.server_path)
server_inst.logger.info('Restore backup ' + slot_folder)
Expand Down Expand Up @@ -399,7 +425,7 @@ def format_dir_size(size: int):
slot_idx = i + 1
slot_info = format_slot_info(slot_number=slot_idx)
if size_display:
dir_size = get_dir_size(get_slot_folder(slot_idx))
dir_size = get_dir_size(get_slot_path(slot_idx))
else:
dir_size = 0
backup_size += dir_size
Expand Down Expand Up @@ -489,6 +515,13 @@ def get_slot_node():
get_literal_node('del').
then(get_slot_node().runs(lambda src, ctx: delete_backup(src, ctx['slot'])))
).
then(
get_literal_node('rename').
then(
get_slot_node().
then(GreedyText('comment').runs(lambda src, ctx: rename_backup(src, ctx['slot'], ctx['comment'])))
)
).
then(get_literal_node('confirm').runs(confirm_restore)).
then(get_literal_node('abort').runs(trigger_abort)).
then(get_literal_node('list').runs(lambda src: list_backup(src))).
Expand Down
1 change: 1 addition & 0 deletions quick_backup_multi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Configure(Serializable):
'make': 1,
'back': 2,
'del': 2,
'rename': 2,
'confirm': 1,
'abort': 1,
'reload': 2,
Expand Down

0 comments on commit 987e027

Please sign in to comment.