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 = {}):
29 self.logFile = filePath
30 src.ensure_path_exists(os.path.dirname(filePath))
31 self.xmlroot = etree.Element(rootname, attrib = attrib)
33 def write_tree(self, stylesheet=None):
34 f = open(self.logFile, 'w')
35 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
37 f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)
38 f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
41 def add_simple_node(self, node_name, text=None, attrib={}):
42 n = etree.Element(node_name, attrib=attrib)
44 self.xmlroot.append(n)
47 def append_node(self, node_name, text):
48 for field in self.xmlroot:
49 if field.tag == node_name:
52 class readXmlFile(object):
53 '''Class to manage reading of an xml log file
55 def __init__(self, filePath):
56 self.filePath = filePath
57 etree_inst = etree.parse(filePath)
58 self.xmlroot = etree_inst.parse(filePath)
60 def get_attrib_text(self, attribname):
62 for field in self.xmlroot:
63 if attribname in field.attrib:
64 lres.append((field.attrib[attribname], field.text))
67 def get_node_text(self, node):
68 for field in self.xmlroot:
74 def update_hat_xml(logDir, application=None):
75 xmlHatFilePath = os.path.join(logDir, 'hat.xml')
77 xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : application})
79 xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : "NO"})
81 for fileName in os.listdir(logDir):
82 sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$"
83 oExpr = re.compile(sExpr)
84 if oExpr.search(fileName):
85 date_hour_cmd = fileName.split('_')
86 date_not_formated = date_hour_cmd[0]
87 date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
88 hour_not_formated = date_hour_cmd[1]
89 hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
90 cmd = date_hour_cmd[2][:-len('.xml')]
92 xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd})
94 xmlHat.write_tree('hat.xsl')