提交 83428604 编写于 作者: M Mars Liu

fxied bug in tree.py

上级 2318de26
{ {
"type": "code_options", "type": "code_options",
"author": "幻灰龙", "author": "幻灰龙",
"source": "standard.md" "source": "standard.md",
"exercise_id": "8150ef23aad248f78110f33e34754311"
} }
\ No newline at end of file
{ {
"type": "code_options", "type": "code_options",
"author": "幻灰龙", "author": "幻灰龙",
"source": "compiler.md" "source": "compiler.md",
"exercise_id": "b812499b338541d2955575e56c116da9"
} }
\ No newline at end of file
...@@ -5,7 +5,5 @@ ...@@ -5,7 +5,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [ "export": []
"vla.json"
]
} }
\ No newline at end of file
...@@ -5,7 +5,5 @@ ...@@ -5,7 +5,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [ "export": []
"pointers_2_func.json"
]
} }
\ No newline at end of file
...@@ -7,7 +7,5 @@ ...@@ -7,7 +7,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [ "export": []
"linked_list.json"
]
} }
\ No newline at end of file
{ {
"type": "code_options", "type": "code_options",
"author": "幻灰龙", "author": "幻灰龙",
"source": "bin_to_hex.md" "source": "bin_to_hex.md",
"exercise_id": "bfae271b73284a6fa5f48bcde4a7f3e0"
} }
\ No newline at end of file
{ {
"type": "code_options", "type": "code_options",
"author": "幻灰龙", "author": "幻灰龙",
"source": "bit_op.md" "source": "bit_op.md",
"exercise_id": "0952044cc5e54770b7418ed868d201a2"
} }
\ No newline at end of file
{ {
"type": "code_options", "type": "code_options",
"author": "幻灰龙", "author": "幻灰龙",
"source": "pack01.md" "source": "pack01.md",
"exercise_id": "4aa8ae84487d433e943a5a9ef6293dbd"
} }
\ No newline at end of file
{ {
"type": "code_options", "type": "code_options",
"author": "幻灰龙", "author": "幻灰龙",
"source": "pack02.md" "source": "pack02.md",
"exercise_id": "03f964d865b54fe9947e728585a27486"
} }
\ No newline at end of file
...@@ -6,7 +6,5 @@ ...@@ -6,7 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [ "export": []
"enum.json"
]
} }
\ No newline at end of file
...@@ -5,7 +5,5 @@ ...@@ -5,7 +5,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [ "export": []
"inline01.json"
]
} }
\ No newline at end of file
...@@ -6,7 +6,5 @@ ...@@ -6,7 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [ "export": []
"file_hash.json"
]
} }
\ No newline at end of file
此差异已折叠。
...@@ -37,6 +37,19 @@ def parse_no_name(d): ...@@ -37,6 +37,19 @@ def parse_no_name(d):
return no, dir_name return no, dir_name
def check_export(base, cfg):
flag = False
exports = []
for export in cfg.get('export', []):
ecfg_path = os.path.join(base, export)
if os.path.exists(ecfg_path):
exports.append(export)
else:
flag = True
if flag:
cfg["export"] = exports
return flag
def gen_tree(data_path): def gen_tree(data_path):
root = {} root = {}
...@@ -54,18 +67,21 @@ def gen_tree(data_path): ...@@ -54,18 +67,21 @@ def gen_tree(data_path):
yield no_dir, no_name yield no_dir, no_name
def ensure_id_helper(node): def ensure_id_helper(node):
flag = False
if (node.get('node_id') is None) or node.get('node_id') in id_set: if (node.get('node_id') is None) or node.get('node_id') in id_set:
node['node_id'] = gen_node_id() node['node_id'] = gen_node_id()
flag = True
id_set.add(node['node_id']) id_set.add(node['node_id'])
if 'children' in node: if 'children' in node:
for c in node["children"]: for c in node["children"]:
ensure_id_helper(list(c.values())[0]) flag = flag or ensure_id_helper(list(c.values())[0])
def ensure_node_id(cfg_path, cfg): return flag
ensure_id_helper(cfg)
dump_json(cfg_path, cfg, exist_ok=True, override=True) def ensure_node_id(cfg):
return ensure_id_helper(cfg)
def make_node(name, node_id, keywords, children=None): def make_node(name, node_id, keywords, children=None):
node = {} node = {}
...@@ -80,7 +96,8 @@ def gen_tree(data_path): ...@@ -80,7 +96,8 @@ def gen_tree(data_path):
# 根节点 # 根节点
cfg_path = os.path.join(data_path, 'config.json') cfg_path = os.path.join(data_path, 'config.json')
cfg = load_json(cfg_path) cfg = load_json(cfg_path)
ensure_node_id(cfg_path, cfg) if ensure_node_id(cfg):
dump_json(cfg_path, cfg)
tree_node = { tree_node = {
"node_id": cfg['node_id'], "node_id": cfg['node_id'],
"keywords": cfg['keywords'], "keywords": cfg['keywords'],
...@@ -92,47 +109,51 @@ def gen_tree(data_path): ...@@ -92,47 +109,51 @@ def gen_tree(data_path):
for level_no_dir, level_no_name in list_dir(data_path): for level_no_dir, level_no_name in list_dir(data_path):
print(level_no_dir) print(level_no_dir)
no, level_name = parse_no_name(level_no_name) no, level_name = parse_no_name(level_no_name)
cfg_path = os.path.join(level_no_dir, 'config.json') level_path = os.path.join(level_no_dir, 'config.json')
cfg = load_json(cfg_path) level_cfg = load_json(level_path)
ensure_node_id(cfg_path, cfg) if ensure_node_id(level_cfg) or check_export(level_no_dir, level_cfg):
dump_json(level_path, level_cfg, exist_ok=True, override=True)
level_node, level_node_children = make_node( level_node, level_node_children = make_node(
level_name, cfg['node_id'], cfg['keywords']) level_name, cfg['node_id'], cfg['keywords'])
tree_node['children'].append(level_node) tree_node['children'].append(level_node)
# 章节点 # 章节点
for chapter_no_dir, chapter_no_name in list_dir(level_no_dir): for chapter_no_dir, chapter_no_name in list_dir(level_no_dir):
no, chapter_name = parse_no_name(chapter_no_name) no, chapter_name = parse_no_name(chapter_no_name)
cfg_path = os.path.join(chapter_no_dir, 'config.json') chapter_path = os.path.join(chapter_no_dir, 'config.json')
ensure_node_id(cfg_path, cfg) chapter_cfg = load_json(chapter_path)
cfg = load_json(cfg_path) if ensure_node_id(chapter_cfg) or check_export(chapter_no_dir, chapter_cfg):
dump_json(chapter_path, chapter_cfg, exist_ok=True, override=True)
chapter_node, chapter_node_children = make_node( chapter_node, chapter_node_children = make_node(
chapter_name, cfg['node_id'], cfg['keywords']) chapter_name, chapter_cfg['node_id'], chapter_cfg['keywords'])
level_node_children.append(chapter_node) level_node_children.append(chapter_node)
# 知识点 # 知识点
for section_no_dir, section_no_name in list_dir(chapter_no_dir): for section_no_dir, section_no_name in list_dir(chapter_no_dir):
no, section_name = parse_no_name(section_no_name) no, section_name = parse_no_name(section_no_name)
cfg_path = os.path.join(section_no_dir, 'config.json') sec_path = os.path.join(section_no_dir, 'config.json')
cfg = load_json(cfg_path) sec_cfg = load_json(sec_path)
ensure_node_id(cfg_path, cfg) flag = ensure_node_id(sec_cfg) or check_export(section_no_dir, sec_cfg)
section_node, section_node_children = make_node( section_node, section_node_children = make_node(
section_name, cfg['node_id'], cfg['keywords'], cfg['children']) section_name, sec_cfg['node_id'], sec_cfg['keywords'], sec_cfg['children'])
chapter_node_children.append(section_node) chapter_node_children.append(section_node)
# 确保习题分配了习题ID # 确保习题分配了习题ID
for export in cfg['export']:
for export in sec_cfg["export"]:
ecfg_path = os.path.join(section_no_dir, export) ecfg_path = os.path.join(section_no_dir, export)
ecfg = load_json(ecfg_path) ecfg = load_json(ecfg_path)
if (ecfg.get('exercise_id') is None) or (ecfg.get('exercise_id') in id_set): if (ecfg.get('exercise_id') is None) or (ecfg.get('exercise_id') in id_set):
ecfg['exercise_id'] = uuid.uuid4().hex ecfg['exercise_id'] = uuid.uuid4().hex
dump_json(ecfg_path, ecfg, exist_ok=True, override=True) dump_json(ecfg_path, ecfg, exist_ok=True, override=True)
id_set.add(ecfg['exercise_id']) id_set.add(ecfg['exercise_id'])
if flag:
dump_json(cfg_path, cfg, exist_ok=True, override=True) dump_json(cfg_path, cfg, exist_ok=True, override=True)
# 保存技能树骨架 # 保存技能树骨架
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册