Salome HOME
7b383c7cf5c870b9670e313f0097edd572d7cd4c
[tools/sat.git] / src / xmlManager.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import os
20 import re
21
22 import src
23 from . import ElementTree as etree
24
25 class xmlLogFile(object):
26     '''Class to manage writing in salomeTools xml log file
27     '''
28     def __init__(self, filePath, rootname, attrib = {}):
29         '''Initialization
30         
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
34         '''
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)
40     
41     def write_tree(self, stylesheet=None):
42         '''Write the xml tree in the log file path. Add the stylesheet if asked.
43         
44         :param stylesheet str: The stylesheet to apply to the xml file
45         '''
46         f = open(self.logFile, 'w')
47         f.write("<?xml version='1.0' encoding='utf-8'?>\n")
48         if stylesheet:
49             f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)    
50         f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
51         f.close()  
52         
53     def add_simple_node(self, node_name, text=None, attrib={}):
54         '''Add a node with some attibutes and text to the root node.
55         
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
59         '''
60         n = etree.Element(node_name, attrib=attrib)
61         n.text = text
62         self.xmlroot.append(n)
63         return n
64     
65     def append_node_text(self, node_name, text):
66         '''Append a new text to the node that has node_name as name
67         
68         :param node_name str: The name of the node on which append text
69         :param text str: The text to append
70         '''
71         # find the corresponding node
72         for field in self.xmlroot:
73             if field.tag == node_name:
74                 # append the text
75                 field.text += text
76
77     def append_node_attrib(self, node_name, attrib):
78         '''Append a new attributes to the node that has node_name as name
79         
80         :param node_name str: The name of the node on which append text
81         :param attrib dixt: The attrib to append
82         '''
83         self.xmlroot.find(node_name).attrib.update(attrib)
84
85 class readXmlFile(object):
86     '''Class to manage reading of an xml log file
87     '''
88     def __init__(self, filePath):
89         '''Initialization
90         
91         :param filePath str: The xml file to be read
92         '''
93         self.filePath = filePath
94         etree_inst = etree.parse(filePath)
95         self.xmlroot = etree_inst.parse(filePath)
96
97     def getRootAttrib(self):
98         '''Get the attibutes of the self.xmlroot
99         
100         :return: The attributes of the root node
101         :rtype: dict
102         '''
103         return self.xmlroot.attrib
104     
105     def get_attrib(self, node_name):
106         '''Get the attibutes of the node node_name in self.xmlroot
107         
108         :param node_name str: the name of the node
109         :return: the attibutes of the node node_name in self.xmlroot
110         :rtype: dict
111         '''
112         return self.xmlroot.find(node_name).attrib
113     
114     def get_node_text(self, node):
115         '''Get the text of the first node that has name that corresponds to the parameter node
116         
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
119         :rtype: str
120         '''
121         # Loop on all root nodes
122         for field in self.xmlroot:
123             if field.tag == node:
124                 return field.text
125         return ''
126
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.
129     
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
134     
135     :return: True if cmd is not in notShownCommands and the application in the log file corresponds to application
136     :rtype: boolean
137     '''
138     # When the command is not in notShownCommands, no need to go further. Do not show
139     if cmd in notShownCommands:
140         return False, None
141  
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
151         
152         return True, appliLog
153     
154     if application == 'None':
155             return True, None    
156         
157     return False, None
158     
159
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
162     
163     :param logDir str: the directory to parse
164     :param application str: the name of the application if there is any
165     '''
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:
184             if showLog:
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})
187     
188     # Write the file on the hard drive
189     xmlHat.write_tree('hat.xsl')