diff --git a/data/tree.json b/data/tree.json index 44adf43e42d78b364f2e49668ac45ef27bf2877d..7ec8c1b6539d05624797a819b6c9be0049ba18cf 100644 --- a/data/tree.json +++ b/data/tree.json @@ -2212,4 +2212,4 @@ } ] } -} \ No newline at end of file +} diff --git a/src/tree.py b/src/tree.py index 61b5b8950df71658adfa6243961eab5ca45a8da2..b770efcbed27862e46454c98fa75f23c00da153b 100644 --- a/src/tree.py +++ b/src/tree.py @@ -14,7 +14,6 @@ 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()) @@ -100,13 +99,11 @@ class TreeWalker: 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}.{section_title}") if os.path.isdir(full_path): self.ensure_exercises(full_path) @@ -122,7 +119,8 @@ class TreeWalker: level_path = os.path.join(self.root, level) num, config = self.load_level_node(level_path) levels.append((num, config)) - levels.sort(key=lambda item: item[0]) + + levels = self.resort_children(self.root, levels) root_node["children"] = [item[1] for item in levels] return root_node @@ -148,7 +146,7 @@ class TreeWalker: num, chapter = self.load_chapter_node(full_name) chapters.append((num, chapter)) - chapters.sort(key=lambda item: item[0]) + chapters = self.resort_children(base, chapters) level_node["children"] = [item[1] for item in chapters] return level_node @@ -160,10 +158,21 @@ class TreeWalker: num, section = self.load_section_node(full_name) sections.append((num, section)) - sections.sort(key=lambda item: item[0]) + sections = self.resort_children(base, sections) chapter_node["children"] = [item[1] for item in sections] return chapter_node + def resort_children(self, base, children): + children.sort(key=lambda item: item[0]) + 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}") + if origin != posted: + self.logger.info(f"rename [{origin}] to [{posted}]") + os.rename(origin, posted) + return children + def ensure_chapters(self): for subdir in os.listdir(self.root): self.ensure_level_config(subdir) @@ -220,8 +229,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: @@ -285,3 +294,4 @@ class TreeWalker: if "exercise_id" not in exercise: exercise["exercise_id"] = uuid.uuid4().hex dump_json(full_name, exercise) +