import re from defaultBlockTypes import addDefaultBlockTypes from tacsrc.block import Block from tacsrc.blockType import BlockType from tacsrc.link import Link def readTASRC(inputFile): blockTypes = {} addDefaultBlockTypes(blockTypes) blocks = {} links = [] # make sure all blocks have been declared before the links are read linklines = [] with open(inputFile, "r") as file: content = file.readlines() for l in content: # clean up line, putting only a single space in every seperation line = re.sub("(\\s+|, *)", ' ', l) parts = line.split(' ') if len(parts) == 0: continue if parts[0] == "TAC_BLOCK": if not parts[2] in blockTypes: blockTypes[parts[2]] = BlockType(parts[2]) blocks[parts[1]] = Block( parts[1], blockTypes[parts[2]], parts[3:-1] ) elif parts[0] == "TAC_LINK": linklines.append(parts) for parts in linklines: # parts[1] is the link name and is not used fromBlock = blocks[parts[2]] fromPort = int(parts[3]) toBlock = blocks[parts[4]] toPort = int(parts[5]) links.append(Link( fromBlock, fromPort, toBlock, toPort )) fromBlock.blockType.outputs.ensureLength(fromPort) toBlock.blockType.inputs.ensureLength(toPort) return blocks.values(), links, blockTypes.values()