]> SALOME platform Git repositories - tools/sat.git/blob - src/xmlManager.py
Salome HOME
link to remote machine logs in the matrix of jobs command
[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 try: # For python2
21     import sys
22     reload(sys)  
23     sys.setdefaultencoding('utf8')
24 except:
25     pass
26
27 import src
28 from . import ElementTree as etree
29
30 class XmlLogFile(object):
31     '''Class to manage writing in salomeTools xml log file
32     '''
33     def __init__(self, filePath, rootname, attrib = {}):
34         '''Initialization
35         
36         :param filePath str: The path to the file where to write the log file
37         :param rootname str: The name of the root node of the xml file
38         :param attrib dict: the dictionary that contains the attributes 
39                             and value of the root node
40         '''
41         # Initialize the filePath and ensure that the directory 
42         # that contain the file exists (make it if necessary)
43         self.logFile = filePath
44         src.ensure_path_exists(os.path.dirname(filePath))
45         # Initialize the field that contain the xml in memory
46         self.xmlroot = etree.Element(rootname, attrib = attrib)
47     
48     def write_tree(self, stylesheet=None):
49         '''Write the xml tree in the log file path. Add the stylesheet if asked.
50         
51         :param stylesheet str: The stylesheet to apply to the xml file
52         '''
53         f = open(self.logFile, 'w')
54         f.write("<?xml version='1.0' encoding='utf-8'?>\n")
55         if stylesheet:
56             f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % 
57                     stylesheet)    
58         f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
59         f.close()  
60         
61     def add_simple_node(self, node_name, text=None, attrib={}):
62         '''Add a node with some attibutes and text to the root node.
63         
64         :param node_name str: the name of the node to add
65         :param text str: the text of the node
66         :param attrib dict: the dictionary containing the 
67                             attribute of the new node
68         '''
69         n = etree.Element(node_name, attrib=attrib)
70         n.text = text
71         self.xmlroot.append(n)
72         return n
73     
74     def append_node_text(self, node_name, text):
75         '''Append a new text to the node that has node_name as name
76         
77         :param node_name str: The name of the node on which append text
78         :param text str: The text to append
79         '''
80         # find the corresponding node
81         for field in self.xmlroot:
82             if field.tag == node_name:
83                 # append the text
84                 field.text += text
85
86     def append_node_attrib(self, node_name, attrib):
87         '''Append a new attributes to the node that has node_name as name
88         
89         :param node_name str: The name of the node on which append text
90         :param attrib dixt: The attrib to append
91         '''
92         self.xmlroot.find(node_name).attrib.update(attrib)
93
94 class ReadXmlFile(object):
95     '''Class to manage reading of an xml log file
96     '''
97     def __init__(self, filePath):
98         '''Initialization
99         
100         :param filePath str: The xml file to be read
101         '''
102         self.filePath = filePath
103         etree_inst = etree.parse(filePath)
104         self.xmlroot = etree_inst.parse(filePath)
105
106     def getRootAttrib(self):
107         '''Get the attibutes of the self.xmlroot
108         
109         :return: The attributes of the root node
110         :rtype: dict
111         '''
112         return self.xmlroot.attrib
113     
114     def get_attrib(self, node_name):
115         '''Get the attibutes of the node node_name in self.xmlroot
116         
117         :param node_name str: the name of the node
118         :return: the attibutes of the node node_name in self.xmlroot
119         :rtype: dict
120         '''
121         attrib = self.xmlroot.find(node_name).attrib
122         # To be python 3 compatible, convert bytes to str if there are any
123         fixedAttrib = {}
124         for k in attrib.keys():
125             if isinstance(k, bytes):
126                 key = k.decode()
127             else:
128                 key = k
129             if isinstance(attrib[k], bytes):
130                 value = attrib[k].decode()
131             else:
132                 value = attrib[k]
133             fixedAttrib[key] = value
134         return fixedAttrib
135     
136     def get_node_text(self, node):
137         '''Get the text of the first node that has name 
138            that corresponds to the parameter node
139         
140         :param node str: the name of the node from which get the text
141         :return: the text of the first node that has name 
142                  that corresponds to the parameter node
143         :rtype: str
144         '''
145         return self.xmlroot.find(node).text
146
147 def add_simple_node(root_node, node_name, text=None, attrib={}):
148     '''Add a node with some attibutes and text to the root node.
149
150     :param root_node etree.Element: the Etree element where to add the new node    
151     :param node_name str: the name of the node to add
152     :param text str: the text of the node
153     :param attrib dict: the dictionary containing the 
154                         attribute of the new node
155     '''
156     n = etree.Element(node_name, attrib=attrib)
157     n.text = text
158     root_node.append(n)
159     return n
160
161 def append_node_attrib(root_node, attrib):
162     '''Append a new attributes to the node that has node_name as name
163     
164     :param root_node etree.Element: the Etree element 
165                                     where to append the new attibutes
166     :param attrib dixt: The attrib to append
167     '''
168     root_node.attrib.update(attrib)