提交 2d20396b 编写于 作者: M Mars Liu

fixed odd orders

上级 2888dde7
......@@ -14,6 +14,7 @@ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def load_json(p):
with open(p, 'r') as f:
return json.loads(f.read())
......@@ -28,7 +29,7 @@ def dump_json(p, j, exist_ok=False, override=False):
logger.error(f"{p} already exist")
sys.exit(0)
with open(p, 'w+') as f:
with open(p, 'w+', encoding="utf8") as f:
f.write(json.dumps(j, indent=2, ensure_ascii=False))
......@@ -94,23 +95,39 @@ class TreeWalker:
for index, level in enumerate(root_node["children"]):
level_title = list(level.keys())[0]
level_node = list(level.values())[0]
level_path = os.path.join(self.root, f"{index+1}.{level_title}")
level_path = os.path.join(self.root, f"{index + 1}.{level_title}")
self.load_chapters(level_path, level_node)
for index, chapter in enumerate(level_node["children"]):
chapter_title = list(chapter.keys())[0]
chapter_node = list(chapter.values())[0]
chapter_path = os.path.join(level_path, f"{index+1}.{chapter_title}")
chapter_path = os.path.join(level_path, f"{index + 1}.{chapter_title}")
self.load_sections(chapter_path, chapter_node)
for index, section_node in enumerate(chapter_node["children"]):
section_title = list(section_node.keys())[0]
full_path = os.path.join(chapter_path, f"{index}.{section_title}")
full_path = os.path.join(chapter_path, f"{index + 1}.{section_title}")
if os.path.isdir(full_path):
self.ensure_exercises(full_path)
# TODO 四级知识点的处理仅为 Java 技能树的临时处理而设定,未来java技能树上线前会删掉这部分代码,将四级节点
# 合并到三级节点
for idx, [num, sub_section_title] in self.sort_dir_list([p for p in os.listdir(full_path)
if os.path.isdir(os.path.join(full_path, p))]):
order = idx + 1
ensure_path = os.path.join(full_path, f"{order}.{sub_section_title}")
forth_full_path = os.path.join(full_path, f"{num}.{sub_section_title}")
if ensure_path != forth_full_path:
os.rename(forth_full_path, ensure_path)
if os.path.isdir(ensure_path):
self.ensure_exercises(forth_full_path)
tree_path = os.path.join(self.root, "tree.json")
dump_json(tree_path, self.tree, exist_ok=True, override=True)
return self.tree
def sort_dir_list(self, dirs):
result = [self.extract_node_env(dir) for dir in dirs]
result.sort(key=lambda item: item[0])
return result
def load_levels(self, root_node):
levels = []
for level in os.listdir(self.root):
......@@ -167,7 +184,7 @@ class TreeWalker:
for index, [number, element] in enumerate(children):
title = list(element.keys())[0]
origin = os.path.join(base, f"{number}.{title}")
posted = os.path.join(base, f"{index+1}.{title}")
posted = os.path.join(base, f"{index + 1}.{title}")
if origin != posted:
self.logger.info(f"rename [{origin}] to [{posted}]")
os.rename(origin, posted)
......@@ -229,8 +246,8 @@ class TreeWalker:
config = {
"node_id": self.gen_node_id(),
"keywords": [],
"children":[],
"export":[]
"children": [],
"export": []
}
dump_json(config_path, config, exist_ok=True, override=True)
else:
......@@ -258,7 +275,8 @@ class TreeWalker:
return int(number), title
except Exception as error:
self.logger.error(f"目录 [{path}] 解析失败,结构不合法,可能是缺少序号")
sys.exit(1)
# sys.exit(1)
raise error
def load_chapter_node(self, full_name):
config = self.ensure_chapter_config(full_name)
......@@ -290,8 +308,8 @@ class TreeWalker:
config = self.ensure_section_config(section_path)
for e in config.get("export", []):
full_name = os.path.join(section_path, e)
logger.info(full_name)
exercise = load_json(full_name)
if "exercise_id" not in exercise:
exercise["exercise_id"] = uuid.uuid4().hex
dump_json(full_name, exercise)
dump_json(full_name, exercise, exist_ok=True, override=True)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册