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