update file sourcing
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
import xml.etree.ElementTree as ET
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass
|
||||
class SourcedFiles:
|
||||
class SourcedFile:
|
||||
layer_name: str
|
||||
subfolder: Optional[str]
|
||||
files: List[str]
|
||||
|
||||
|
||||
def read_psc_file(psc_file: str) -> List[SourcedFiles]:
|
||||
def read_psc_file(psc_file: Path) -> List[SourcedFile]:
|
||||
tree = ET.parse(psc_file)
|
||||
root = tree.getroot()
|
||||
|
||||
layers = root.findall(".//Layer")
|
||||
|
||||
layer_info_list: List[SourcedFiles] = []
|
||||
layer_info_list: List[SourcedFile] = []
|
||||
for layer in layers:
|
||||
layer_name = layer.attrib.get("Name")
|
||||
subfolder = layer.attrib.get("SubFolder")
|
||||
@@ -30,6 +31,22 @@ def read_psc_file(psc_file: str) -> List[SourcedFiles]:
|
||||
script_names.append(name)
|
||||
|
||||
layer_info_list.append(
|
||||
SourcedFiles(layer_name=layer_name, subfolder=subfolder, files=script_names)
|
||||
SourcedFile(layer_name=layer_name, subfolder=subfolder, files=script_names)
|
||||
)
|
||||
return layer_info_list
|
||||
|
||||
|
||||
def get_all_psc_files(root_path: Path) -> list[Path]:
|
||||
"""Sammelt alle .tcl-Dateien rekursiv im gegebenen Verzeichnis"""
|
||||
return [path for path in root_path.rglob("*.psc")]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test = get_all_psc_files(
|
||||
Path(
|
||||
r"H:\janus-engineering-customers\KSB_Frankenthal\custom\library\machine\installed_machines\ksb_pe_grob_g550_sone\postprocessor"
|
||||
)
|
||||
)
|
||||
print(test)
|
||||
for pp in test:
|
||||
read_psc_file(pp)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
from typing import List, DefaultDict, Union
|
||||
|
||||
from tclint.syntax_tree import Visitor, Command, CommandSub, Node, Script
|
||||
|
||||
|
||||
class SymbolTable:
|
||||
"""Holds a symbol table (links symbols to nodes)."""
|
||||
|
||||
def __init__(self):
|
||||
self.proc_def: DefaultDict[str, list[Node]] = defaultdict(list)
|
||||
|
||||
def add_proc_definition(self, command: Command) -> None:
|
||||
"""Add definition of procedure"""
|
||||
# command holds the "proc" keyword, so the proc name is 1st argument
|
||||
proc_name_node = command.args[0]
|
||||
proc_name = proc_name_node.contents
|
||||
if not proc_name:
|
||||
return
|
||||
logging.debug(
|
||||
f"Definition of proc '{proc_name}' at {proc_name_node._pos_str()}"
|
||||
)
|
||||
self.proc_def[proc_name].append(proc_name_node)
|
||||
|
||||
def lookup_proc_definitions(self, symbol_text: str) -> List[Node]:
|
||||
"""Lookup definitions of the procedure pointed at by node"""
|
||||
if symbol_text is None or symbol_text not in self.proc_def:
|
||||
return []
|
||||
return self.proc_def[symbol_text]
|
||||
|
||||
|
||||
class SymbolTableBuilder(Visitor):
|
||||
"""Builds a symbol table."""
|
||||
|
||||
def __init__(self):
|
||||
self.table = SymbolTable()
|
||||
|
||||
def build(self, tree: Union[CommandSub, Script]) -> SymbolTable:
|
||||
"""Run the builder visitor through the syntax tree, building a table."""
|
||||
tree.accept(self, recurse=True)
|
||||
return self.table
|
||||
|
||||
def visit_command(self, command: Command) -> None:
|
||||
if command.routine.contents == "proc":
|
||||
self.table.add_proc_definition(command)
|
||||
|
||||
Reference in New Issue
Block a user