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