Salome HOME
9c84cc18f0fedf5f7749340ae19781536dbb9dba
[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 get_attrib(self, node_name):
98         '''Get the attibutes of the node node_name in self.xmlroot
99         
100         :param node_name str: the name of the node
101         '''
102         return self.xmlroot.find(node_name).attrib
103     
104     def get_node_text(self, node):
105         '''Get the text of the first node that has name that corresponds to the parameter node
106         
107         :param node str: the name of the node from which get the text
108         '''
109         # Loop on all root nodes
110         for field in self.xmlroot:
111             if field.tag == node:
112                 return field.text
113         return ''
114
115
116 def update_hat_xml(logDir, application=None):
117     '''Create the xml file in logDir that contain all the xml file and have a name like YYYYMMDD_HHMMSS_namecmd.xml
118     
119     :param logDir str: the directory to parse
120     :param application str: the name of the application if there is any
121     '''
122     # Create an instance of xmlLogFile class to create hat.xml file
123     xmlHatFilePath = os.path.join(logDir, 'hat.xml')
124     # If there is an application, add the attribute to the root node
125     if application:
126         xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist", {"application" : application})
127     else:
128         xmlHat = xmlLogFile(xmlHatFilePath,  "LOGlist", {"application" : "NO"})
129     
130     # parse the log directory to find all the command logs, then add it to the xml file
131     for fileName in os.listdir(logDir):
132         # YYYYMMDD_HHMMSS_namecmd.xml
133         sExpr = "^[0-9]{8}_+[0-9]{6}_+.*.xml$"
134         oExpr = re.compile(sExpr)
135         if oExpr.search(fileName):
136             # get date and hour and format it
137             date_hour_cmd = fileName.split('_')
138             date_not_formated = date_hour_cmd[0]
139             date = "%s/%s/%s" % (date_not_formated[6:8], date_not_formated[4:6], date_not_formated[0:4] )
140             hour_not_formated = date_hour_cmd[1]
141             hour = "%s:%s:%s" % (hour_not_formated[0:2], hour_not_formated[2:4], hour_not_formated[4:6])
142             cmd = date_hour_cmd[2][:-len('.xml')]
143             # add a node to the hat.xml file
144             xmlHat.add_simple_node("LogCommand", text=fileName, attrib = {"date" : date, "hour" : hour, "cmd" : cmd})
145     
146     # Write the file on the hard drive
147     xmlHat.write_tree('hat.xsl')