def returnMap(self,outcomes,name): """ This method returns a map if the ET contains symbolic sequences. This is needed since since RAVEN requires numeric values for sequences. @ In, outcomes, list, list that contains all the sequences IDs provided in the ET @ In, name, string, name of the ET @ Out, etMap, dict, dictionary containing the map """ # check if outputMap contains string ID for at least one sequence # if outputMap contains all numbers then keep the number ID allFloat = True for seq in outcomes: try: float(seq) except ValueError: allFloat = False break etMap = {} if allFloat == False: # create an integer map, and create an integer map file root = ET.Element('map') root.set('Tree', name) for seq in outcomes: etMap[seq] = outcomes.index(seq) ET.SubElement(root, "sequence", ID=str(outcomes.index(seq))).text = str(seq) fileID = name + '_mapping.xml' updatedTreeMap = ET.ElementTree(root) xmlU.prettify(updatedTreeMap) updatedTreeMap.write(fileID) else: for seq in outcomes: etMap[seq] = utils.floatConversion(seq) return etMap
def printXML(self): """ Returns string representation of tree (in XML format). @ In, None @ Out, msg, string, string representation """ xml = inputTreeToXml(self, fromNode=True) return xmlUtils.prettify(xml)
def writeFile(self): """ Writes the input file to disk. @ In, None @ Out, None """ #prettify tree pretty = xmlUtils.prettify(self.tree) #make sure file is written cleanly and anew if self.isOpen(): self.close() self.writelines(pretty,overwrite=True) self.close()
def writeWorkflow(self, templates, destination, run=False): """ todo """ # TODO use destination? # write templates inner, outer, cash = templates outer_file = os.path.abspath(os.path.join(destination, 'outer.xml')) inner_file = os.path.abspath(os.path.join(destination, 'inner.xml')) cash_file = os.path.abspath(os.path.join(destination, 'cash.xml')) print('HERON: writing files ...') msg_format = ' ... wrote "{1:15s}" to "{0}/"' with open(outer_file, 'w') as f: f.write(xmlUtils.prettify(outer)) print(msg_format.format(*os.path.split(outer_file))) with open(inner_file, 'w') as f: f.write(xmlUtils.prettify(inner)) print(msg_format.format(*os.path.split(inner_file))) with open(cash_file, 'w') as f: f.write(xmlUtils.prettify(cash)) print(msg_format.format(*os.path.split(cash_file))) # write library of info so it can be read in dispatch during inner run data = (self.__case, self.__components, self.__sources) lib_file = os.path.abspath( os.path.join(destination, self.namingTemplates['lib file'])) with open(lib_file, 'wb') as lib: pk.dump(data, lib) print(msg_format.format(*os.path.split(lib_file))) # copy "write_inner.py", which has the denoising and capacity fixing algorithms conv_src = os.path.abspath( os.path.join(self._template_path, 'write_inner.py')) conv_file = os.path.abspath(os.path.join(destination, 'write_inner.py')) shutil.copyfile(conv_src, conv_file) print(msg_format.format(*os.path.split(conv_file))) # run, if requested if run: self.runWorkflow(destination)
def writeFile(self, asString=False, **kwargs): """ Writes the input file to disk. @ In, asString, bool, optional, if indicated then return string instead of writing @ In, kwargs, dict, optional, additional arguments to pass to prettify @ Out, pretty, str, optional, only returned if asString is True """ #prettify tree pretty = xmlUtils.prettify(self.tree, **kwargs) if asString: return pretty #make sure file is written cleanly and anew if self.isOpen(): self.close() self.writelines(pretty, overwrite=True) self.close()
def writeFile(self): """ Writes the input file to disk. Because we store a list of dynamic nodes instead of a full tree, requires some special treatment @ In, None @ Out, None """ if self.isOpen(): self.close() #write the root node manually self.writelines('<'+self.rootName+' type="Dynamic">\n',overwrite=True) #write out each time step node for node in self.pivotNodes: pretty = xmlUtils.prettify(node,startingTabs=1,addRavenNewlines=False) self.writelines(pretty) self.writelines('</'+self.rootName+'>\n') self.close()
def writeWorkflow(self, template, destination, run=False): """ Writes a template to file. @ In, template, xml.etree.ElementTree.Element, file to write @ In, destination, str, path and filename to write to @ In, run, bool, optional, if True then run the workflow after writing? good idea? @ Out, errors, int, 0 if successfully wrote [and run] and nonzero if there was a problem """ pretty = xmlUtils.prettify(template) with open(destination, 'w') as f: f.write(pretty) if run: errors = self.runWorkflow(destination) else: # 0 meaning successfully wrote file errors = 0 return errors
def getNode(fname, nodepath): """ Searches file for particular node. Note that this takes the first matching node path in the file. @ In, fname, string, name of file with XML tree to search @ In, nodepath, string, "."-separated string with xml node name path, i.e., Simulation.RunInfo.Sequence @ Out, getNode, ET.Element, prettified element """ #TODO add option to include parent nodes with ellipses root = ET.parse(fname).getroot() #format nodepath nodepath = nodepath.replace('.', '/') #check if root is desired node if root.tag == nodepath: node = root docLevel = 0 else: docLevel = len(nodepath.split('/')) node = xmlUtils.findPathEllipsesParents(root, nodepath, docLevel) if node is None: raise IOError('Unable to find ' + nodepath + ' in ' + fname) return xmlUtils.prettify(node, doc=True, docLevel=docLevel)
def saveDataToFile(self, source): """ Saves the given data as database to file. @ In, source, DataObjects.DataObject, object to write to file @ Out, None """ ds, meta = source.getData() # we actually just tell the DataSet to write out as netCDF path = self.get_fullpath() # TODO set up to use dask for on-disk operations # convert metadata into writeable for key, xml in meta.items(): ds.attrs[key] = xmlUtils.prettify(xml.getRoot()) # get rid of "object" types for var in ds: if ds[var].dtype == np.dtype(object): # is it a string? if mathUtils.isAString(ds[var].values[0]): ds[var] = ds[var].astype(str) # is there existing data? Read it in and merge it, if so # -> we've already wiped the file in initializeDatabase if it's in write mode if os.path.isfile(path): exists = xr.load_dataset(path) if 'RAVEN_sample_ID' in exists: floor = int(exists['RAVEN_sample_ID'].values[-1]) + 1 new = ds['RAVEN_sample_ID'].values + floor ds = ds.assign_coords(RAVEN_sample_ID=new) # NOTE order matters! This preserves the sampling order in which data was inserted # into this database ds = xr.concat((exists, ds), 'RAVEN_sample_ID') # if this is open somewhere else, we can't write to it # TODO is there a way to check if it's writable? I can't find one ... try: ds.to_netcdf(path, engine=self._format) except PermissionError: self.raiseAnError( PermissionError, f'NetCDF file "{path}" denied RAVEN permission to write! Is it open in another program?' )
try: os.remove(fName) except OSError: later.append(fName) return later #establish test XML xmlString = '<root ratr="root_attrib"><child catr1="child attrib 1" catr2="child attrib 2"><cchild ccatr="cc_attrib">cchildtext</cchild></child></root>' inFileName = 'testXMLInput.xml' open(inFileName, 'w').write(xmlString) xmlTree = ET.parse(inFileName) toRemove = attemptFileClear(inFileName, toRemove) # test prettify pretty = utils.toString(xmlUtils.prettify(xmlTree)) prettyFileName = 'xml/testXMLPretty.xml' open(prettyFileName, 'w').writelines(pretty) gold = ''.join( line.rstrip('\n\r') for line in open( os.path.join(os.path.dirname(__file__), 'gold', prettyFileName), 'r')) test = ''.join(line.rstrip('\n\r') for line in open(prettyFileName, 'r')) if gold == test: results['pass'] += 1 toRemove = attemptFileClear(prettyFileName, toRemove) else: print('ERROR: Test of "pretty" failed! See', prettyFileName, '(below) vs gold/', prettyFileName) print('( START', prettyFileName, ')') for line in file(prettyFileName, 'r'): print(line[:-1]) #omit newline
""" try: os.remove(fName) except OSError: later.append(fName) return later #establish test XML xmlString = '<root ratr="root_attrib"><child catr1="child attrib 1" catr2="child attrib 2"><cchild ccatr="cc_attrib">cchildtext</cchild></child></root>' inFileName = 'testXMLInput.xml' open(inFileName,'w').write(xmlString) xmlTree = ET.parse(inFileName) toRemove = attemptFileClear(inFileName,toRemove) # test prettify pretty = xmlUtils.prettify(xmlTree) prettyFileName = 'xml/testXMLPretty.xml' open(prettyFileName,'w').writelines(pretty) gold = ''.join(line.rstrip('\n\r') for line in open(os.path.join(os.path.dirname(__file__),'gold',prettyFileName),'r')) test = ''.join(line.rstrip('\n\r') for line in open( prettyFileName ,'r')) if gold==test: results['pass']+=1 toRemove = attemptFileClear(prettyFileName,toRemove) else: print('ERROR: Test of "pretty" failed! See',prettyFileName,'(below) vs gold/',prettyFileName) print('( START',prettyFileName,')') for line in file(prettyFileName,'r'): print(line[:-1]) #omit newline print('( END',prettyFileName,')') results['fail']+=1