提交 a31cdcee 编写于 作者: F feilong

fix exercise_id gen bug

上级 ceaae444
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "b6c1d63886f5439e8f7be9e1aa956ba6"
}
\ No newline at end of file
此差异已折叠。
...@@ -14,6 +14,7 @@ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') ...@@ -14,6 +14,7 @@ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) logger.addHandler(handler)
def load_json(p): def load_json(p):
with open(p, 'r') as f: with open(p, 'r') as f:
return json.loads(f.read()) return json.loads(f.read())
...@@ -50,6 +51,8 @@ def parse_no_name(d): ...@@ -50,6 +51,8 @@ def parse_no_name(d):
no = int(m.group(1)) no = int(m.group(1))
dir_name = m.group(2) dir_name = m.group(2)
except: except:
logger.error('parser no name failed')
logger.error(d)
sys.exit(0) sys.exit(0)
return no, dir_name return no, dir_name
...@@ -99,13 +102,17 @@ class TreeWalker: ...@@ -99,13 +102,17 @@ class TreeWalker:
for index, chapter in enumerate(level_node["children"]): for index, chapter in enumerate(level_node["children"]):
chapter_title = list(chapter.keys())[0] chapter_title = list(chapter.keys())[0]
chapter_node = list(chapter.values())[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) self.load_sections(chapter_path, chapter_node)
for index, section_node in enumerate(chapter_node["children"]): for index, section_node in enumerate(chapter_node["children"]):
section_title = list(section_node.keys())[0] 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"{section_node['origin_num']}.{section_title}")
if os.path.isdir(full_path): if os.path.isdir(full_path):
self.ensure_exercises(full_path) self.ensure_exercises(full_path)
else:
pass
tree_path = os.path.join(self.root, "tree.json") tree_path = os.path.join(self.root, "tree.json")
dump_json(tree_path, self.tree, exist_ok=True, override=True) dump_json(tree_path, self.tree, exist_ok=True, override=True)
...@@ -158,6 +165,8 @@ class TreeWalker: ...@@ -158,6 +165,8 @@ class TreeWalker:
sections.append((num, section)) sections.append((num, section))
sections.sort(key=lambda item: item[0]) sections.sort(key=lambda item: item[0])
for item in sections:
item[1]['origin_num'] = item[0]
chapter_node["children"] = [item[1] for item in sections] chapter_node["children"] = [item[1] for item in sections]
return chapter_node return chapter_node
...@@ -217,8 +226,8 @@ class TreeWalker: ...@@ -217,8 +226,8 @@ class TreeWalker:
config = { config = {
"node_id": self.gen_node_id(), "node_id": self.gen_node_id(),
"keywords": [], "keywords": [],
"children":[], "children": [],
"export":[] "export": []
} }
dump_json(config_path, config, exist_ok=True, override=True) dump_json(config_path, config, exist_ok=True, override=True)
else: else:
...@@ -241,7 +250,7 @@ class TreeWalker: ...@@ -241,7 +250,7 @@ class TreeWalker:
def extract_node_env(self, path): def extract_node_env(self, path):
try: try:
_, dir = os.path.split(path) _, dir = os.path.split(path)
self.logger.info(path) # self.logger.info(path)
number, title = dir.split(".", 1) number, title = dir.split(".", 1)
return int(number), title return int(number), title
except Exception as error: except Exception as error:
...@@ -281,5 +290,4 @@ class TreeWalker: ...@@ -281,5 +290,4 @@ class TreeWalker:
exercise = load_json(full_name) exercise = load_json(full_name)
if "exercise_id" not in exercise: if "exercise_id" not in exercise:
exercise["exercise_id"] = uuid.uuid4().hex exercise["exercise_id"] = uuid.uuid4().hex
dump_json(full_name, exercise) dump_json(full_name, exercise, True, True)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册