def convertToDaveXMLPrimitives(self, primitives_xml, flat_sequence): if self.verbose: print "---------------------------------------------------------" print "Converting to Dave Primitives" # Loop over all children for child in flat_sequence: if child.tag == "branch": # Generate block and call recursively to handle elements in blocks branch = ElementTree.SubElement(primitives_xml, "branch") branch.attrib["name"] = child.attrib["name"] self.convertToDaveXMLPrimitives(branch, child) elif child.tag == "movie": # Handle <movie> tag movie_block = ElementTree.SubElement(primitives_xml, "branch") name = child.find("name") # Determine name. if name is not None: movie_block.set("name", name.text) else: movie_block.set("name", "No Name Provided") # Determine dictionary movie_dict = nodeToDict.movieNodeToDict(child) for action in self.movie_da_actions: new_node = action.createETree(movie_dict) if new_node is not None: movie_block.append(new_node) elif child.tag == "valve_protocol": # Handle <valve_protocol> tag new_node = daveActions.DAValveProtocol().createETree( {"name": child.text}) if new_node is not None: primitives_xml.append(new_node) elif child.tag == "change_directory": # Handle change_directory tag new_node = daveActions.DASetDirectory().createETree( {"directory": child.text}) if new_node is not None: primitives_xml.append(new_node) elif child.tag == "clear_warnings": # Handle the clear_warnings tag new_node = daveActions.DAClearWarnings().createETree({}) if new_node is not None: primitives_xml.append(new_node) elif child.tag == "email": # Handle the email tag # Grab the elements of this node and create a dictionary dictionary = {"subject": None, "body": None} for subChild in child: dictionary[subChild.tag] = subChild.text # Create node new_node = daveActions.DAEmail().createETree(dictionary) if new_node is not None: primitives_xml.append(new_node) else: pass
def convertToDaveXMLPrimitives(self, primitives_xml, flat_sequence, base_dir = "", create_dir = False): if self.verbose: print "---------------------------------------------------------" print "Converting to Dave Primitives" # Loop over all children for child in flat_sequence: if child.tag == "branch": # Generate block and call recursively to handle elements in blocks branch = ElementTree.SubElement(primitives_xml, "branch") branch.attrib["name"] = child.attrib["name"] self.convertToDaveXMLPrimitives(branch, child, base_dir = base_dir, create_dir = create_dir) elif child.tag == "movie": # Handle <movie> tag movie_block = ElementTree.SubElement(primitives_xml, "branch") name = child.find("name") # Determine name. if name is not None: movie_block.set("name", name.text) else: movie_block.set("name", "No Name Provided") # Determine dictionary movie_dict = nodeToDict.movieNodeToDict(child) for action in self.movie_da_actions: new_node = action.createETree(movie_dict) if new_node is not None: movie_block.append(new_node) elif child.tag == "valve_protocol": # Handle <valve_protocol> tag new_node = daveActions.DAValveProtocol().createETree({"name": child.text}) if new_node is not None: primitives_xml.append(new_node) elif child.tag == "change_directory": # Handle change_directory tag directory_path = os.path.join(base_dir, child.text) if base_dir is not None else child.text new_node = daveActions.DASetDirectory().createETree({"directory": directory_path}) if create_dir: if create_dir == "error" and os.path.isdir(directory_path): raise Exception, "Directory already exists: %s" % directory_path try: os.makedirs(directory_path) except: pass if new_node is not None: primitives_xml.append(new_node) elif child.tag == "clear_warnings": # Handle the clear_warnings tag new_node = daveActions.DAClearWarnings().createETree({}) if new_node is not None: primitives_xml.append(new_node) else: pass
def __init__(self, xml_filename="", output_filename="", verbose=True, parent=None): QtGui.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 self.movie_da_actions = [ daveActions.DAMoveStage(), daveActions.DASetFocusLockTarget(), daveActions.DAFindSum(), daveActions.DACheckFocus(), daveActions.DARecenterPiezo(), daveActions.DASetParameters(), daveActions.DASetProgression(), daveActions.DASetDirectory(), 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 1: line = pos_fp.readline() 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 = str( QtGui.QFileDialog.getOpenFileName( parent, movie_node.find("name").text + " Power File", directory, "*.power")) 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")) out_fp.close() return generated_file