From a0ad53cd3375523429e2bcb27c366faa3c5bd8bd Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Mon, 8 Nov 2021 15:20:56 +0800 Subject: [PATCH] new pipeline --- src/tree.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/tree.py b/src/tree.py index c36a946..9e6a5cf 100644 --- a/src/tree.py +++ b/src/tree.py @@ -7,9 +7,12 @@ import sys import re id_set = set() - logger = logging.getLogger(__name__) - +logger.setLevel(logging.INFO) +handler = logging.StreamHandler(sys.stdout) +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: @@ -71,11 +74,12 @@ def gen_node_id(): class TreeWalker: - def __init__(self, root, tree_name, title=None): + def __init__(self, root, tree_name, title=None, log=None): self.name = tree_name self.root = root self.title = tree_name if title is None else title self.tree = {} + self.logger = logger if log is None else log def walk(self): root = self.load_root() @@ -184,12 +188,12 @@ class TreeWalker: config = { "node_id": self.gen_node_id() } - dump_json(path, config, exist_ok=True, override=True) + dump_json(config_path, config, exist_ok=True, override=True) else: config = load_json(config_path) flag, result = self.ensure_node_id(config) if flag: - dump_json(path, config, exist_ok=True, override=True) + dump_json(config_path, config, exist_ok=True, override=True) return config def ensure_chapter_config(self, path): @@ -199,12 +203,12 @@ class TreeWalker: "node_id": self.gen_node_id(), "keywords": [] } - dump_json(path, config, exist_ok=True, override=True) + dump_json(config_path, config, exist_ok=True, override=True) else: config = load_json(config_path) flag, result = self.ensure_node_id(config) if flag: - dump_json(path, config, exist_ok=True, override=True) + dump_json(config_path, config, exist_ok=True, override=True) return config def ensure_section_config(self, path): @@ -235,9 +239,14 @@ class TreeWalker: return f"{self.name}-{uuid.uuid4().hex}" def extract_node_env(self, path): - _, dir = os.path.split(path) - number, title = dir.split(".", 1) - return int(number), title + try: + _, dir = os.path.split(path) + self.logger.info(path) + number, title = dir.split(".", 1) + return int(number), title + except Exception as error: + self.logger.error(f"目录 [{path}] 解析失败,结构不合法,可能是缺少序号") + sys.exit(1) def load_chapter_node(self, full_name): config = self.ensure_chapter_config(full_name) -- GitLab