From 9891475ab06f80bb75044567b43504a6f002d0da Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Wed, 29 Dec 2021 12:21:49 +0800 Subject: [PATCH] sync tree.py script --- src/tree.py | 92 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 22 deletions(-) diff --git a/src/tree.py b/src/tree.py index c31754d..7a18dd2 100644 --- a/src/tree.py +++ b/src/tree.py @@ -2,10 +2,11 @@ import json import logging import os import re +import subprocess import sys import uuid import re -import git + id_set = set() logger = logging.getLogger(__name__) @@ -14,10 +15,25 @@ handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) -repo = git.Repo(".") -def user_name(): - return repo.config_reader().get_value("user", "name") +def search_author(author_dict, username): + for key in author_dict: + names = author_dict[key] + if username in names: + return key + return username + +def user_name(md_file, author_dict): + ret = subprocess.Popen([ + "git", "log", md_file + ], stdout=subprocess.PIPE) + lines = list(map(lambda l: l.decode(), ret.stdout.readlines())) + author_lines = [] + for line in lines: + if line.startswith('Author'): + author_lines.append(line.split(' ')[1]) + author_nick_name = author_lines[-1] + return search_author(author_dict, author_nick_name) def load_json(p): with open(p, 'r') as f: @@ -75,7 +91,18 @@ def check_export(base, cfg): class TreeWalker: - def __init__(self, root, tree_name, title=None, log=None): + def __init__( + self, root, + tree_name, + title=None, + log=None, + authors=None, + enable_notebook=None, + ignore_keywords=False + ): + self.ignore_keywords = ignore_keywords + self.authors = authors if authors else {} + self.enable_notebook = enable_notebook self.name = tree_name self.root = root self.title = tree_name if title is None else title @@ -87,7 +114,9 @@ class TreeWalker: root_node = { "node_id": root["node_id"], "keywords": root["keywords"], - "children": [] + "children": [], + "keywords_must": root["keywords_must"], + "keywords_forbid": root["keywords_forbid"] } self.tree[root["tree_name"]] = root_node self.load_levels(root_node) @@ -100,11 +129,13 @@ 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 + 1}.{section_title}") + full_path = os.path.join( + chapter_path, f"{index + 1}.{section_title}") if os.path.isdir(full_path): self.check_section_keywords(full_path) self.ensure_exercises(full_path) @@ -140,6 +171,8 @@ class TreeWalker: "node_id": config["node_id"], "keywords": config["keywords"], "children": [], + "keywords_must": config["keywords_must"], + "keywords_forbid": config["keywords_forbid"] } } @@ -191,6 +224,8 @@ class TreeWalker: "tree_name": self.name, "keywords": [], "node_id": self.gen_node_id(), + "keywords_must": [], + "keywords_forbid": [] } dump_json(config_path, config, exist_ok=True, override=True) else: @@ -220,7 +255,9 @@ class TreeWalker: if not os.path.exists(config_path): config = { "node_id": self.gen_node_id(), - "keywords": [] + "keywords": [], + "keywords_must": [], + "keywords_forbid": [] } dump_json(config_path, config, exist_ok=True, override=True) else: @@ -286,6 +323,8 @@ class TreeWalker: "node_id": config["node_id"], "keywords": config["keywords"], "children": [], + "keywords_must": config["keywords_must"], + "keywords_forbid": config["keywords_forbid"] } } return num, result @@ -297,7 +336,9 @@ class TreeWalker: name: { "node_id": config["node_id"], "keywords": config["keywords"], - "children": config.get("children", []) + "children": config.get("children", []), + "keywords_must": config["keywords_must"], + "keywords_forbid": config["keywords_forbid"] } } # if "children" in config: @@ -314,7 +355,8 @@ class TreeWalker: continue mfile = base + ".json" meta_path = os.path.join(section_path, mfile) - self.ensure_exercises_meta(meta_path, source) + md_file = os.path.join(section_path, e) + self.ensure_exercises_meta(meta_path, source, md_file) export = config.get("export", []) if mfile not in export and self.name != "algorithm": export.append(mfile) @@ -322,7 +364,8 @@ class TreeWalker: config["export"] = export if flag: - dump_json(os.path.join(section_path, "config.json"), config, True, True) + dump_json(os.path.join(section_path, "config.json"), + config, True, True) for e in config.get("export", []): full_name = os.path.join(section_path, e) @@ -334,7 +377,7 @@ class TreeWalker: else: id_set.add(exercise["exercise_id"]) - def ensure_exercises_meta(self, meta_path, source): + def ensure_exercises_meta(self, meta_path, source, md_file): _, mfile = os.path.split(meta_path) meta = None if os.path.exists(meta_path): @@ -349,26 +392,31 @@ class TreeWalker: if "source" not in meta: meta["source"] = source if "author" not in meta: - meta["author"] = user_name() + meta["author"] = user_name(md_file, self.authors) if "type" not in meta: meta["type"] = "code_options" - if meta is None: - meta = { - "type": "code_options", - "author": user_name(), - "source": source, - "notebook_enable": self.default_notebook(), - "exercise_id": uuid.uuid4().hex - } + + if meta is None: + meta = { + "type": "code_options", + "author": user_name(md_file, self.authors), + "source": source, + "notebook_enable": self.default_notebook(), + "exercise_id": uuid.uuid4().hex + } dump_json(meta_path, meta, True, True) def default_notebook(self): + if self.enable_notebook is not None: + return self.enable_notebook if self.name in ["python", "java", "c"]: return True else: return False def check_section_keywords(self, full_path): + if self.ignore_keywords: + return config = self.ensure_section_config(full_path) if not config.get("keywords", []): self.logger.error(f"节点 [{full_path}] 的关键字为空,请修改配置文件写入关键字") -- GitLab