diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f003f87..de3eb34 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,15 +5,9 @@ - - - - - - - - - + + + + + + + + + + + + + diff --git a/entry.py b/entry.py index baa955a..a8fd386 100644 --- a/entry.py +++ b/entry.py @@ -1,8 +1,8 @@ from PyQt5.QtWidgets import QWidget, QApplication from sys import argv +from parse.ast_load import global_ast_path, AstParse -app = QApplication(argv) -win = QWidget() -win.show() -app.exec() \ No newline at end of file +tool = AstParse(global_ast_path) +print(tool.generate_time()) +tool.peak_storylines() \ No newline at end of file diff --git a/parse/__pycache__/__init__.cpython-312.pyc b/parse/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..fd33a09 Binary files /dev/null and b/parse/__pycache__/__init__.cpython-312.pyc differ diff --git a/parse/__pycache__/ast_load.cpython-312.pyc b/parse/__pycache__/ast_load.cpython-312.pyc new file mode 100644 index 0000000..ff05651 Binary files /dev/null and b/parse/__pycache__/ast_load.cpython-312.pyc differ diff --git a/parse/ast_load.py b/parse/ast_load.py index 2d6a19d..b1dac6b 100644 --- a/parse/ast_load.py +++ b/parse/ast_load.py @@ -1,14 +1,93 @@ from lxml import etree +from typing import List -class ast_parse(): +global_ast_path = r"E:\storyline.xast" + + +def items_filter(items, proc): + values = [] + for it in items: + if proc(it): + values.append(it) + pass + pass + return values + + +class FragmentSlice(): + def __init__(self, signature: str): + self.signature = signature + self.text_sections = [] + pass + + def append_text_section(self, section: str): + self.text_sections.append(section) + pass + + +class StorySlice(): + def __init__(self, signature: str): + self.signature = signature + self.text_sections = [] + self.fragment_slices = [] + pass + + def append_text_section(self, section: str): + self.text_sections.append(section) + pass + + def append_fragment_slice(self, slice: FragmentSlice): + self.fragment_slices.append(slice) + pass + + + +class AstParse(): def __init__(self, ast_path: str): self.ast_path = ast_path - ast_file = open(ast_path, "rt") + ast_file = open(ast_path, "rb") ast_text = ast_file.read() self.ast_inst = etree.XML(ast_text) pass - def generate_time(self)-> str: - self.ast_inst.xpath + def generate_time(self) -> str: + attr_time = self.ast_inst.xpath("/ast[1]/@time")[0] + return str(attr_time) + + def peak_storylines(self) -> List[StorySlice]: + story_list = self.ast_inst.xpath("/ast/story") # 获取所有故事节点 + + story_items = [] + for story in story_list: + story_name = story.xpath("@name")[0] + story_slice = StorySlice("story:"+story_name) + story_items.append(story_slice) + + story_text_lines = story.xpath("text-section/@text") # 获取所有描述 + for line in story_text_lines: + story_slice.append_text_section(line) + pass + + fragment_items = story.xpath("fragment") # 获取所有情节定义 + for fragm in fragment_items: + fragment_name = fragm.xpath("@name") + fragm_slice = FragmentSlice("fragment:"+story_name+"#"+fragment_name) + fragm_text_lines = fragm.xpath("text-section/@text") + for line in fragm_text_lines: + fragm_slice.append_text_section(line) + pass + pass + pass + + fragm_refers = self.ast_inst.xpath("//refer") + for refer in fragm_refers: + story_name = refer.xpath("@story") + fragm_name = refer.xpath("@fragment") + story_refers = items_filter(story_items, lambda x:x.signature == f"story:{story_name}") + if(len(story_refers) > 0): + story_refer = story_refers[0] + + + return story_items