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 def update_hat_xml(logDir):
53 xmlHatFilePath = os.path.join(logDir, 'hat.xml')
54 xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist")
56 for fileName in os.listdir(logDir):
57 sExpr = "^[0-9]{8}_+[0-9]{6}_+[A-Za-z0-9]*.xml$"
58 oExpr = re.compile(sExpr)
59 if oExpr.search(fileName):
60 date_hour_cmd = fileName.split('_')
61 date_not_formated = date_hour_cmd[0]
62 date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
63 hour_not_formated = date_hour_cmd[1]
64 hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
65 cmd = date_hour_cmd[2][:-len('.xml')]
67 xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd})
69 xmlHat.write_tree('hat.xsl')