From 926d4008d749a6bf5170edf635f720a621fecd1e Mon Sep 17 00:00:00 2001 From: luxin Date: Thu, 30 Dec 2021 18:06:12 +0800 Subject: [PATCH] add keywords_must and keywords_forbid --- .../1.Shell/config.json" | 35 ++++--- .../1.Shell/helloworld.json" | 8 +- .../config.json" | 4 +- .../config.json" | 4 +- .../config.json" | 4 +- .../config.json" | 4 +- data/config.json | 4 +- data/tree.json | 36 ++++++-- src/tree.py | 91 ++++++++++++++----- 9 files changed, 138 insertions(+), 52 deletions(-) diff --git "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/config.json" "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/config.json" index be12a7f..b2376bc 100644 --- "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/config.json" +++ "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/config.json" @@ -1,17 +1,22 @@ { - "node_id": "devops-6f0f3d97e7c144baaaf1b82c7a92252f", - "keywords": [], - "children": [ - { - "运维入门": { - "keywords": [ - "运维是做什么" - ], - "children": [] - } - } - ], - "export": [ - "helloworld.json" - ] + "node_id": "devops-6f0f3d97e7c144baaaf1b82c7a92252f", + "keywords": [], + "children": [ + { + "运维入门": { + "keywords": [ + "运维是做什么" + ], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "node_id": "devops-62f278976e32425a930fc6e91902f4bc" + } + } + ], + "export": [ + "helloworld.json" + ], + "keywords_must": [], + "keywords_forbid": [] } \ No newline at end of file diff --git "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/helloworld.json" "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/helloworld.json" index 9720b07..b8f948f 100644 --- "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/helloworld.json" +++ "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/1.Shell/helloworld.json" @@ -1,5 +1,7 @@ { - "type": "code_options", - "author": "huanhuilong", - "source": "helloworld.md" + "type": "code_options", + "author": "huanhuilong", + "source": "helloworld.md", + "exercise_id": "29a90053e0da4055b8e3d1790874d776", + "notebook_enable": false } \ No newline at end of file diff --git "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/config.json" "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/config.json" index 788697f..f211627 100644 --- "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/config.json" +++ "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/1.\350\277\220\347\273\264\345\237\272\347\241\200/config.json" @@ -1,4 +1,6 @@ { "node_id": "devops-00e6b52067d54d4887631d8d0c99fa67", - "keywords": [] + "keywords": [], + "keywords_must": [], + "keywords_forbid": [] } \ No newline at end of file diff --git "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/config.json" "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/config.json" index e96234d..9025113 100644 --- "a/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/config.json" +++ "b/data/1.\350\277\220\347\273\264\345\210\235\351\230\266/config.json" @@ -1,4 +1,6 @@ { "node_id": "devops-296bbd7f39b2499bb2a30969bbeb870d", - "keywords": [] + "keywords": [], + "keywords_must": [], + "keywords_forbid": [] } \ No newline at end of file diff --git "a/data/2.\350\277\220\347\273\264\344\270\255\351\230\266/config.json" "b/data/2.\350\277\220\347\273\264\344\270\255\351\230\266/config.json" index 6d902a6..b3390c2 100644 --- "a/data/2.\350\277\220\347\273\264\344\270\255\351\230\266/config.json" +++ "b/data/2.\350\277\220\347\273\264\344\270\255\351\230\266/config.json" @@ -1,4 +1,6 @@ { "node_id": "devops-2d88d2335be74101b0ae72e6540f15a8", - "keywords": [] + "keywords": [], + "keywords_must": [], + "keywords_forbid": [] } \ No newline at end of file diff --git "a/data/3.\350\277\220\347\273\264\351\253\230\351\230\266/config.json" "b/data/3.\350\277\220\347\273\264\351\253\230\351\230\266/config.json" index 31d3db1..ce0a0f8 100644 --- "a/data/3.\350\277\220\347\273\264\351\253\230\351\230\266/config.json" +++ "b/data/3.\350\277\220\347\273\264\351\253\230\351\230\266/config.json" @@ -1,4 +1,6 @@ { "node_id": "devops-aebee081f3cb4e71975041f45a517604", - "keywords": [] + "keywords": [], + "keywords_must": [], + "keywords_forbid": [] } \ No newline at end of file diff --git a/data/config.json b/data/config.json index c6ad3c6..083b997 100644 --- a/data/config.json +++ b/data/config.json @@ -1,5 +1,7 @@ { "tree_name": "devops", "keywords": [], - "node_id": "devops-0d3595ca328c49388b409e8d5ba18a9a" + "node_id": "devops-0d3595ca328c49388b409e8d5ba18a9a", + "keywords_must": [], + "keywords_forbid": [] } \ No newline at end of file diff --git a/data/tree.json b/data/tree.json index 0b9979a..928d3ff 100644 --- a/data/tree.json +++ b/data/tree.json @@ -17,29 +17,53 @@ "Shell": { "node_id": "devops-6f0f3d97e7c144baaaf1b82c7a92252f", "keywords": [], - "children": [] + "children": [ + { + "运维入门": { + "keywords": [ + "运维是做什么" + ], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "node_id": "devops-62f278976e32425a930fc6e91902f4bc" + } + } + ], + "keywords_must": [], + "keywords_forbid": [] } } - ] + ], + "keywords_must": [], + "keywords_forbid": [] } } - ] + ], + "keywords_must": [], + "keywords_forbid": [] } }, { "运维中阶": { "node_id": "devops-2d88d2335be74101b0ae72e6540f15a8", "keywords": [], - "children": [] + "children": [], + "keywords_must": [], + "keywords_forbid": [] } }, { "运维高阶": { "node_id": "devops-aebee081f3cb4e71975041f45a517604", "keywords": [], - "children": [] + "children": [], + "keywords_must": [], + "keywords_forbid": [] } } - ] + ], + "keywords_must": [], + "keywords_forbid": [] } } \ No newline at end of file diff --git a/src/tree.py b/src/tree.py index 1b85cbf..0c58d32 100644 --- a/src/tree.py +++ b/src/tree.py @@ -2,10 +2,10 @@ 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,15 +14,31 @@ 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: + with open(p, 'r', encoding="utf-8") as f: return json.loads(f.read()) @@ -77,7 +93,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 @@ -89,7 +116,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) @@ -144,6 +173,8 @@ class TreeWalker: "node_id": config["node_id"], "keywords": config["keywords"], "children": [], + "keywords_must": config["keywords_must"], + "keywords_forbid": config["keywords_forbid"] } } @@ -195,6 +226,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: @@ -224,7 +257,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: @@ -290,6 +325,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 @@ -301,7 +338,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: @@ -318,7 +357,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) @@ -339,7 +379,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): @@ -354,27 +394,32 @@ 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}] 的关键字为空,请修改配置文件写入关键字") - # sys.exit(1) + if not config.get("keywords", []): + self.logger.error(f"节点 [{full_path}] 的关键字为空,请修改配置文件写入关键字") + sys.exit(1) -- GitLab