<commit_before>'''
Tools used to format, edit, and print XML in a RAVEN-like way
talbpaul, 2016-05
'''

from __future__ import division, print_function, unicode_literals, absolute_import
import warnings
warnings.simplefilter('default',DeprecationWarning)

import numpy as np
import xml.etree.ElementTree as ET
import xml.dom.minidom as pxml

def prettify(tree):
  """
    Script for turning XML tree into something mostly RAVEN-preferred.  Does not align attributes as some devs like (yet).
    The output can be written directly to a file, as file('whatever.who','w').writelines(prettify(mytree))
    @ In, tree, xml.etree.ElementTree object, the tree form of an input file
    @Out, towrite, string, the entire contents of the desired file to write, including newlines
  """
  #make the first pass at pretty.  This will insert way too many newlines, because of how we maintain XML format.
  pretty = pxml.parseString(ET.tostring(tree.getroot())).toprettyxml(indent='  ')
  #loop over each "line" and toss empty ones, but for ending main nodes, insert a newline after.
  towrite=''
  for line in pretty.split('\n'):
    if line.strip()=='':continue
    towrite += line.rstrip()+'\n'
    if line.startswith('  </'): towrite+='\n'
  return towrite

def newNode(tag,text='',attrib={}):
  """
    Creates a new node with the desired tag, text, and attributes more simply than can be done natively.
    @ In, tag, string, the name of the node
    @ In, text, string, optional, the text of the node
    @ In, attrib, dict{string:string}, attribute:value pairs
    @ Out, el, xml.etree.ElementTree.Element, new node
  """
  el = ET.Element(tag,attrib=attrib)
  el.text = text
  return el

def newTree(name):
  """
    Creates a new tree with named node as its root
    @ In, name, string, name of root node
    @ Out, tree, xml.etree.ElementTree.ElementTree, tree
  """
  tree = ET.ElementTree(element=newNode(name))
  return tree

def findPath(root,path):
  """
    Navigates path to find a particular element
    @ In, root, xml.etree.ElementTree.Element, the node to start searching along
    @ In, path, string, |-seperated xml path (as "Simulation|RunInfo|JobName")<commit_msg>No more cramped if conditionals on xmlUtils.py and also camelCasing toWrite variable.<commit_after>'''
Tools used to format, edit, and print XML in a RAVEN-like way
talbpaul, 2016-05
'''

from __future__ import division, print_function, unicode_literals, absolute_import
import warnings
warnings.simplefilter('default',DeprecationWarning)

import numpy as np
import xml.etree.ElementTree as ET
import xml.dom.minidom as pxml

def prettify(tree):
  """
    Script for turning XML tree into something mostly RAVEN-preferred.  Does not align attributes as some devs like (yet).
    The output can be written directly to a file, as file('whatever.who','w').writelines(prettify(mytree))
    @ In, tree, xml.etree.ElementTree object, the tree form of an input file
    @Out, towrite, string, the entire contents of the desired file to write, including newlines
  """
  #make the first pass at pretty.  This will insert way too many newlines, because of how we maintain XML format.
  pretty = pxml.parseString(ET.tostring(tree.getroot())).toprettyxml(indent='  ')
  #loop over each "line" and toss empty ones, but for ending main nodes, insert a newline after.
  toWrite=''
  for line in pretty.split('\n'):
    if line.strip()=='':
      continue
    toWrite += line.rstrip()+'\n'
    if line.startswith('  </'):
      toWrite+='\n'
  return toWrite

def newNode(tag,text='',attrib={}):
  """
    Creates a new node with the desired tag, text, and attributes more simply than can be done natively.
    @ In, tag, string, the name of the node
    @ In, text, string, optional, the text of the node
    @ In, attrib, dict{string:string}, attribute:value pairs
    @ Out, el, xml.etree.ElementTree.Element, new node
  """
  el = ET.Element(tag,attrib=attrib)
  el.text = text
  return el

def newTree(name):
  """
    Creates a new tree with named node as its root
    @ In, name, string, name of root node
    @ Out, tree, xml.etree.ElementTree.ElementTree, tree
  """
  tree = ET.ElementTree(element=newNode(name))
  return tree

def findPath(root,path):
  """
    Navigates path to find a particular element
    @ In, root, xml.etree.ElementTree.Element, the node to start searching along
    @ In, path, string, |-seperated xml path (as "Simulation|RunInfo|JobName")
{'fraction_non_alphanumeric': 0.10727969348659004, 'fraction_numerical': 0.002704530087897228, 'mean_word_length': 3.9365962180200222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 21}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '488856', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1373, 'n_words': 515}