diff --git a/src/__pycache__/tree.cpython-38.pyc b/src/__pycache__/tree.cpython-38.pyc index 4ab3fe6fc8f7f10472332d66aa510a4131f981ff..23e841455d8e7a0e2918a37c0e063d821848d671 100644 Binary files a/src/__pycache__/tree.cpython-38.pyc and b/src/__pycache__/tree.cpython-38.pyc differ diff --git a/src/tree.py b/src/tree.py index c36a946fc07501220698b0eb9c55b06af15dbce6..9e6a5cf92c84709d1994acde622d49c591613017 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)