提交 68889b1e 编写于 作者: F feilong

重构脚本

上级 60703b7b
{
"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
...@@ -4,7 +4,24 @@ from src.doc import DocWalker ...@@ -4,7 +4,24 @@ from src.doc import DocWalker
from src.img import ImgWalker from src.img import ImgWalker
if __name__ == '__main__': 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() walker.walk()
# doc = DocWalker('doc') # doc = DocWalker('doc')
......
...@@ -6,12 +6,6 @@ import subprocess ...@@ -6,12 +6,6 @@ import subprocess
import sys import sys
import uuid import uuid
import re import re
import git
def load_json(p):
with open(p, 'r') as f:
return json.loads(f.read())
id_set = set() id_set = set()
...@@ -21,11 +15,9 @@ handler = logging.StreamHandler(sys.stdout) ...@@ -21,11 +15,9 @@ handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) 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([ ret = subprocess.Popen([
"git", "log", md_file "git", "log", md_file
], stdout=subprocess.PIPE) ], stdout=subprocess.PIPE)
...@@ -36,7 +28,11 @@ def user_name(md_file): ...@@ -36,7 +28,11 @@ def user_name(md_file):
author_lines.append(line.split(' ')[1]) author_lines.append(line.split(' ')[1])
author_nick_name = author_lines[-1] author_nick_name = author_lines[-1]
return author_dict.get(author_nick_name, "") 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): def dump_json(p, j, exist_ok=False, override=False):
...@@ -90,7 +86,18 @@ def check_export(base, cfg): ...@@ -90,7 +86,18 @@ def check_export(base, cfg):
class TreeWalker: 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.name = tree_name
self.root = root self.root = root
self.title = tree_name if title is None else title self.title = tree_name if title is None else title
...@@ -369,7 +376,7 @@ class TreeWalker: ...@@ -369,7 +376,7 @@ class TreeWalker:
if "source" not in meta: if "source" not in meta:
meta["source"] = source meta["source"] = source
if "author" not in meta: if "author" not in meta:
meta["author"] = user_name(md_file) meta["author"] = user_name(md_file, self.authors)
if "type" not in meta: if "type" not in meta:
meta["type"] = "code_options" meta["type"] = "code_options"
except: except:
...@@ -377,7 +384,7 @@ class TreeWalker: ...@@ -377,7 +384,7 @@ class TreeWalker:
if meta is None: if meta is None:
meta = { meta = {
"type": "code_options", "type": "code_options",
"author": user_name(md_file), "author": user_name(md_file, self.authors),
"source": source, "source": source,
"notebook_enable": self.default_notebook(), "notebook_enable": self.default_notebook(),
"exercise_id": uuid.uuid4().hex "exercise_id": uuid.uuid4().hex
...@@ -385,13 +392,17 @@ class TreeWalker: ...@@ -385,13 +392,17 @@ class TreeWalker:
dump_json(meta_path, meta, True, True) dump_json(meta_path, meta, True, True)
def default_notebook(self): def default_notebook(self):
if self.enable_notebook is not None:
return self.enable_notebook
if self.name in ["python", "java", "c"]: if self.name in ["python", "java", "c"]:
return True return True
else: else:
return False return False
def check_section_keywords(self, full_path): def check_section_keywords(self, full_path):
if self.ignore_keywords:
return
config = self.ensure_section_config(full_path) config = self.ensure_section_config(full_path)
# if not config.get("keywords", []): if not config.get("keywords", []):
# self.logger.error(f"节点 [{full_path}] 的关键字为空,请修改配置文件写入关键字") self.logger.error(f"节点 [{full_path}] 的关键字为空,请修改配置文件写入关键字")
# sys.exit(1) sys.exit(1)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册