def parse(node: QTreeWidgetItem, data: DataDict): """Parse file to tree format.""" node.takeChildren() file_name = getpath(node) suffix_text = file_suffix(file_name) if node.text(2): code = int(node.text(2)) else: code = data.new_num() node.setText(2, str(code)) if node not in LOADED_FILES and suffix_text in _SUPPORTED_FILE_SUFFIX: LOADED_FILES.append(node) if suffix_text == 'md': # Markdown node.setIcon(0, file_icon("markdown")) parse_markdown(file_name, node, code, data) elif suffix_text == 'py': # Python script node.setIcon(0, file_icon("python")) parse_text(file_name, code, data) elif suffix_text == 'html': # TODO: Need to parse HTML (reveal.js index.html) node.setIcon(0, file_icon("html")) parse_text(file_name, code, data) elif suffix_text == 'kmol': # Kmol project node.setIcon(0, file_icon("kmol")) _parse_tree(node, data) else: # Text files and Python scripts. node.setIcon(0, file_icon("txt")) parse_text(file_name, code, data) print("Loaded: {}".format(node.text(1)))
def new_pointer(node: QTreeWidgetItem): """Give a new pointer code for node.""" code = self.data.new_num() self.data[code] = self.data[int(node.text(2))] node.setText(2, str(code)) for i in range(node.childCount()): new_pointer(node.child(i))
def _parse_tree(root_node: QTreeWidgetItem, data: DataDict): """Parse in to tree widget.""" try: with open(root_node.text(1), encoding='utf-8') as f: yaml_script = f.read() except FileNotFoundError: return yml_data: YMLData = yaml.load(yaml_script, Loader=yaml.FullLoader) parse_list: List[QTreeWidgetItem] = [] root_node.setText(2, str(yml_data['description'])) data.update(yml_data['data']) def add_node(node_dict: NodeDict) -> QTreeWidgetItem: """Add node in to tree widget.""" name: str = node_dict['name'] code_int: int = node_dict['code'] path: str = node_dict['path'] node = QTreeItem(name, path, str(code_int)) if name.startswith('@'): node.setIcon(0, file_icon("python")) data.add_macro(name[1:], code_int) suffix_text = file_suffix(path) if suffix_text: parse_list.append(node) elif path: node.setIcon(0, file_icon("directory")) subs: List[NodeDict] = node_dict['sub'] for sub in subs: node.addChild(add_node(sub)) return node child_node_dicts: List[NodeDict] = yml_data['node'] for child_node_dict in child_node_dicts: root_node.addChild(add_node(child_node_dict)) for node_item in parse_list: parse(node_item, data) data.save_all()