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_text(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 def append_node_attrib(self, node_name, attrib):
78 '''Append a new attributes to the node that has node_name as name
80 :param node_name str: The name of the node on which append text
81 :param attrib dixt: The attrib to append
83 self.xmlroot.find(node_name).attrib.update(attrib)
85 class readXmlFile(object):
86 '''Class to manage reading of an xml log file
88 def __init__(self, filePath):
91 :param filePath str: The xml file to be read
93 self.filePath = filePath
94 etree_inst = etree.parse(filePath)
95 self.xmlroot = etree_inst.parse(filePath)
97 def getRootAttrib(self):
98 '''Get the attibutes of the self.xmlroot
100 :return: The attributes of the root node
103 return self.xmlroot.attrib
105 def get_attrib(self, node_name):
106 '''Get the attibutes of the node node_name in self.xmlroot
108 :param node_name str: the name of the node
109 :return: the attibutes of the node node_name in self.xmlroot
112 return self.xmlroot.find(node_name).attrib
114 def get_node_text(self, node):
115 '''Get the text of the first node that has name that corresponds to the parameter node
117 :param node str: the name of the node from which get the text
118 :return: the text of the first node that has name that corresponds to the parameter node
121 # Loop on all root nodes
122 for field in self.xmlroot:
123 if field.tag == node:
127 def showcommandLog(logFilePath, cmd, application, notShownCommands):
128 '''Used in updateHatXml. Determine if the log xml file logFilePath has to be shown or not in the hat log.
130 :param logFilePath str: the path to the command xml log file
131 :param cmd str: the command of the log file
132 :param application str: the application passed as parameter to the salomeTools command
133 :param notShownCommands list: the list of commands that are not shown by default
135 :return: True if cmd is not in notShownCommands and the application in the log file corresponds to application
138 # When the command is not in notShownCommands, no need to go further. Do not show
139 if cmd in notShownCommands:
142 # Get the application of the log file
143 logFileXml = readXmlFile(logFilePath)
144 if 'application' in logFileXml.xmlroot.keys():
145 appliLog = logFileXml.xmlroot.get('application')
146 # if it corresponds, then the log has to be shown
147 if appliLog == application:
148 return True, appliLog
149 elif application != 'None':
150 return False, appliLog
152 return True, appliLog
154 if application == 'None':
160 def update_hat_xml(logDir, application=None, notShownCommands = []):
161 '''Create the xml file in logDir that contain all the xml file and have a name like YYYYMMDD_HHMMSS_namecmd.xml
163 :param logDir str: the directory to parse
164 :param application str: the name of the application if there is any
166 # Create an instance of xmlLogFile class to create hat.xml file
167 xmlHatFilePath = os.path.join(logDir, 'hat.xml')
168 xmlHat = xmlLogFile(xmlHatFilePath, "LOGlist", {"application" : application})
169 # parse the log directory to find all the command logs, then add it to the xml file
170 for fileName in os.listdir(logDir):
171 # YYYYMMDD_HHMMSS_namecmd.xml
172 sExpr = "^[0-9]{8}_+[0-9]{6}_+.*\.xml$"
173 oExpr = re.compile(sExpr)
174 if oExpr.search(fileName):
175 # get date and hour and format it
176 date_hour_cmd = fileName.split('_')
177 date_not_formated = date_hour_cmd[0]
178 date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
179 hour_not_formated = date_hour_cmd[1]
180 hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
181 cmd = date_hour_cmd[2][:-len('.xml')]
182 showLog, cmdAppli = showcommandLog(os.path.join(logDir, fileName), cmd, application, notShownCommands)
183 #if cmd not in notShownCommands:
185 # add a node to the hat.xml file
186 xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd, "application" : cmdAppli})
188 # Write the file on the hard drive
189 xmlHat.write_tree('hat.xsl')