3 # Copyright (C) 2010-2013 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 from . import ElementTree as etree
25 class xmlLogFile(object):
26 '''Class to manage writing in salomeTools xml log file
28 def __init__(self, filePath, rootname, attrib = {}):
31 :param filePath str: The path to the file where to write the log file
32 :param rootname str: The name of the root node of the xml file
33 :param attrib dict: the dictionary that contains the attributes and value of the root node
35 # Initialize the filePath and ensure that the directory that contain the file exists (make it if necessary)
36 self.logFile = filePath
37 src.ensure_path_exists(os.path.dirname(filePath))
38 # Initialize the field that contain the xml in memory
39 self.xmlroot = etree.Element(rootname, attrib = attrib)
41 def write_tree(self, stylesheet=None):
42 '''Write the xml tree in the log file path. Add the stylesheet if asked.
44 :param stylesheet str: The stylesheet to apply to the xml file
46 f = open(self.logFile, 'w')
47 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
49 f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)
50 f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
53 def add_simple_node(self, node_name, text=None, attrib={}):
54 '''Add a node with some attibutes and text to the root node.
56 :param node_name str: the name of the node to add
57 :param text str: the text of the node
58 :param attrib dict: the dictionary containing the attribute of the new node
60 n = etree.Element(node_name, attrib=attrib)
62 self.xmlroot.append(n)
65 def append_node(self, node_name, text):
66 '''Append a new text to the node that has node_name as name
68 :param node_name str: The name of the node on which append text
69 :param text str: The text to append
71 # find the corresponding node
72 for field in self.xmlroot:
73 if field.tag == node_name:
77 class readXmlFile(object):
78 '''Class to manage reading of an xml log file
80 def __init__(self, filePath):
83 :param filePath str: The xml file to be read
85 self.filePath = filePath
86 etree_inst = etree.parse(filePath)
87 self.xmlroot = etree_inst.parse(filePath)
89 def get_attrib_text(self, attribname):
90 '''Parse the root nodes and get all node that have the attribname. Return the list of [(value of attribname, text), ...]
93 # Loop on all root nodes
94 for field in self.xmlroot:
95 if attribname in field.attrib:
96 lres.append((field.attrib[attribname], field.text))
99 def get_node_text(self, node):
100 '''Get the text of the first node that has name that corresponds to the parameter node
102 :param node str: the name of the node from which get the text
104 # Loop on all root nodes
105 for field in self.xmlroot:
106 if field.tag == node:
111 def update_hat_xml(logDir, application=None):
112 '''Create the xml file in logDir that contain all the xml file and have a name like YYYYMMDD_HHMMSS_namecmd.xml
114 :param logDir str: the directory to parse
115 :param application str: the name of the application if there is any
117 # Create an instance of xmlLogFile class to create hat.xml file
118 xmlHatFilePath = os.path.join(logDir, 'hat.xml')
119 # If there is an application, add the attribute to the root node
121 xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : application})
123 xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : "NO"})
125 # parse the log directory to find all the command logs, then add it to the xml file
126 for fileName in os.listdir(logDir):
127 # YYYYMMDD_HHMMSS_namecmd.xml
128 sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$"
129 oExpr = re.compile(sExpr)
130 if oExpr.search(fileName):
131 # get date and hour and format it
132 date_hour_cmd = fileName.split('_')
133 date_not_formated = date_hour_cmd[0]
134 date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
135 hour_not_formated = date_hour_cmd[1]
136 hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
137 cmd = date_hour_cmd[2][:-len('.xml')]
138 # add a node to the hat.xml file
139 xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd})
141 # Write the file on the hard drive
142 xmlHat.write_tree('hat.xsl')