From 68889b1ea9ced9b77fe183ddfeffae8e9a536a14 Mon Sep 17 00:00:00 2001 From: feilong Date: Fri, 17 Dec 2021 19:17:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/author.json | 13 ------------- main.py | 19 ++++++++++++++++++- src/tree.py | 43 +++++++++++++++++++++++++++---------------- 3 files changed, 45 insertions(+), 30 deletions(-) delete mode 100644 assets/author.json diff --git a/assets/author.json b/assets/author.json deleted file mode 100644 index 4640a90..0000000 --- a/assets/author.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "Python小白进阶":"youcans", - "youcans": "youcans", - "AI浩":"hhhhhhhhhhwwwwwwwwww", - "hhhhhhhhhhwwwwwwwwww": "hhhhhhhhhhwwwwwwwwww", - "吴佳WuJia":"yvettewu", - "yvettewu": "yvettewu", - "幻灰龙":"huanhuilong", - "feilong":"huanhuilong", - "huanhuilong": "huanhuilong", - "xiaozhi_5638": "xiaozhi_5638", - "请叫我卷福": "xiaozhi_5638" -} \ No newline at end of file diff --git a/main.py b/main.py index 24fdf1f..c1f1a6e 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,24 @@ from src.doc import DocWalker from src.img import ImgWalker if __name__ == '__main__': - walker = TreeWalker("data", "opencv", "OpenCV") + walker = TreeWalker( + "data", "opencv", "OpenCV", + ignore_keywords=True, + enable_notebook=False, + authors={ + "Python小白进阶": "youcans", + "youcans": "youcans", + "AI浩": "hhhhhhhhhhwwwwwwwwww", + "hhhhhhhhhhwwwwwwwwww": "hhhhhhhhhhwwwwwwwwww", + "吴佳WuJia": "yvettewu", + "yvettewu": "yvettewu", + "幻灰龙": "huanhuilong", + "feilong": "huanhuilong", + "huanhuilong": "huanhuilong", + "xiaozhi_5638": "xiaozhi_5638", + "请叫我卷福": "xiaozhi_5638" + } + ) walker.walk() # doc = DocWalker('doc') diff --git a/src/tree.py b/src/tree.py index 6405cef..3891238 100644 --- a/src/tree.py +++ b/src/tree.py @@ -6,12 +6,6 @@ import subprocess import sys import uuid import re -import git - - -def load_json(p): - with open(p, 'r') as f: - return json.loads(f.read()) id_set = set() @@ -21,11 +15,9 @@ handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) -repo = git.Repo(".") -author_dict = load_json('assets/author.json') -def user_name(md_file): +def user_name(md_file, author_dict): ret = subprocess.Popen([ "git", "log", md_file ], stdout=subprocess.PIPE) @@ -36,7 +28,11 @@ def user_name(md_file): author_lines.append(line.split(' ')[1]) author_nick_name = author_lines[-1] return author_dict.get(author_nick_name, "") - # return repo.config_reader().get_value("user", "name") + + +def load_json(p): + with open(p, 'r') as f: + return json.loads(f.read()) def dump_json(p, j, exist_ok=False, override=False): @@ -90,7 +86,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 @@ -369,7 +376,7 @@ class TreeWalker: if "source" not in meta: meta["source"] = source if "author" not in meta: - meta["author"] = user_name(md_file) + meta["author"] = user_name(md_file, self.authors) if "type" not in meta: meta["type"] = "code_options" except: @@ -377,7 +384,7 @@ class TreeWalker: if meta is None: meta = { "type": "code_options", - "author": user_name(md_file), + "author": user_name(md_file, self.authors), "source": source, "notebook_enable": self.default_notebook(), "exercise_id": uuid.uuid4().hex @@ -385,13 +392,17 @@ class TreeWalker: 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}] 的关键字为空,请修改配置文件写入关键字") - # sys.exit(1) + if not config.get("keywords", []): + self.logger.error(f"节点 [{full_path}] 的关键字为空,请修改配置文件写入关键字") + sys.exit(1) -- GitLab