def __init__(self, xml_filename="", output_filename="", verbose=True, parent=None): QtWidgets.QWidget.__init__(self, parent) # Initialize local attributes self.xml_filename = xml_filename self.verbose = verbose self.directory = "" self.main_element = [] self.sequence_xml = [] self.command_sequences = [] self.items = [] self.item_names = [] self.loop_variables = [] self.loop_variable_names = [] self.loop_iterator = [] self.flat_sequence = [] self.flat_sequence_xml = [] self.xml_sequence_file_path = output_filename self.da_primitives_xml = [] # A convenient list of dave actions required for parsing a <movie> tag. # Also determines the order in which commands are given self.movie_da_actions = [ daveActions.DASetDirectory(), daveActions.DASetParameters(), daveActions.DAMoveStage(), daveActions.DASetFocusLockTarget(), daveActions.DAFindSum(), daveActions.DACheckFocus(), daveActions.DASetProgression(), daveActions.DADelay(), daveActions.DAPause(), daveActions.DATakeMovie() ]
def generate(parent, xml_file, position_file, generated_file): directory = os.path.dirname(xml_file) pause = True # Load position data pos_fp = open(position_file, "r") x_pos = [] y_pos = [] while True: line = pos_fp.readline().strip() # Stop at EOF or the first blank line. if not line: break [x, y] = line.split(",") x_pos.append(float(x)) y_pos.append(float(y)) pos_fp.close() xml_in = ElementTree.parse(xml_file).getroot() # Load "header" info. x_offset = nodeToDict.gf("x_offset", [float], 0.0)(xml_in) y_offset = nodeToDict.gf("y_offset", [float], 0.0)(xml_in) delay = nodeToDict.gf("delay", [int], 0)(xml_in) # Create instances of all the supported actions # in the order in which they should occur. da_actions = [ daveActions.DAMoveStage(), daveActions.DASetFocusLockTarget(), daveActions.DAFindSum(), daveActions.DARecenterPiezo(), daveActions.DASetParameters(), daveActions.DASetProgression(), daveActions.DASetDirectory(), daveActions.DADelay(), daveActions.DAPause(), daveActions.DATakeMovie() ] # Generate output xml tree. xml_out = ElementTree.Element("sequence") first_movie = True for pass_number, pass_node in enumerate( [x for x in xml_in if (x.tag == "pass")]): # Create a new block for this pass. pass_block = ElementTree.SubElement(xml_out, "branch") pass_block.set("name", "pass " + str(pass_number)) for i in range(len(x_pos)): mx = x_pos[i] + x_offset my = y_pos[i] + y_offset for movie_number, movie_node in enumerate( [x for x in pass_node if (x.tag == "movie")]): # # Check if we need to get a filename for a power progression. # # This modifies the original node so that we don't have to keep # selecting a filename. # pnode = movie_node.find("progression") if pnode is not None: if (pnode.find("type").text == "file"): if pnode.find("filename") is None: filename = QtWidgets.QFileDialog.getOpenFileName( parent, movie_node.find("name").text + " Power File", directory, "*.power")[0] directory = os.path.dirname(filename) field = ElementTree.SubElement(pnode, "filename") field.text = filename # Create dictionary for node. movie_dict = nodeToDict.movieNodeToDict(movie_node) # Add extra information to complete the movie node. total_delay = delay if "delay" in movie_dict: total_delay += movie_dict["delay"] if (total_delay > 0): movie_dict["delay"] = total_delay if first_movie: movie_dict["pause"] = True first_movie = False if (movie_number == 0): movie_dict["stage_x"] = mx movie_dict["stage_y"] = my if "name" in movie_dict: # Create new block for this movie. movie_block = ElementTree.SubElement(pass_block, "branch") movie_block.set( "name", movie_dict["name"] + " " + str(pass_number) + " " + str(i)) movie_dict["name"] += "_" + str(pass_number) + "_" + str(i) for action in da_actions: node = action.createETree(movie_dict) if node is not None: movie_block.append(node) # Save to output XML file. out_fp = open(generated_file, "w") # # Thank you StackOverflow.. # http://stackoverflow.com/questions/17402323/use-xml-etree-elementtree-to-write-out-nicely-formatted-xml-files # rough_string = ElementTree.tostring(xml_out, 'utf-8') reparsed = minidom.parseString(rough_string) out_fp.write( reparsed.toprettyxml(indent=" ", encoding="ISO-8859-1").decode()) out_fp.close() return generated_file