]> SALOME platform Git repositories - tools/sat.git/blob - src/xmlManager.py
Salome HOME
rename all the function to be pep0008 compliant
[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 and value of the root node
33         '''
34         # Initialize the filePath and ensure that the directory that contain the file exists (make it if necessary)
35         self.logFile = filePath
36         src.ensure_path_exists(os.path.dirname(filePath))
37         # Initialize the field that contain the xml in memory
38         self.xmlroot = etree.Element(rootname, attrib = attrib)
39     
40     def write_tree(self, stylesheet=None):
41         '''Write the xml tree in the log file path. Add the stylesheet if asked.
42         
43         :param stylesheet str: The stylesheet to apply to the xml file
44         '''
45         f = open(self.logFile, 'w')
46         f.write("<?xml version='1.0' encoding='utf-8'?>\n")
47         if stylesheet:
48             f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)    
49         f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
50         f.close()  
51         
52     def add_simple_node(self, node_name, text=None, attrib={}):
53         '''Add a node with some attibutes and text to the root node.
54         
55         :param node_name str: the name of the node to add
56         :param text str: the text of the node
57         :param attrib dict: the dictionary containing the attribute of the new node
58         '''
59         n = etree.Element(node_name, attrib=attrib)
60         n.text = text
61         self.xmlroot.append(n)
62         return n
63     
64     def append_node_text(self, node_name, text):
65         '''Append a new text to the node that has node_name as name
66         
67         :param node_name str: The name of the node on which append text
68         :param text str: The text to append
69         '''
70         # find the corresponding node
71         for field in self.xmlroot:
72             if field.tag == node_name:
73                 # append the text
74                 field.text += text
75
76     def append_node_attrib(self, node_name, attrib):
77         '''Append a new attributes to the node that has node_name as name
78         
79         :param node_name str: The name of the node on which append text
80         :param attrib dixt: The attrib to append
81         '''
82         self.xmlroot.find(node_name).attrib.update(attrib)
83
84 class readXmlFile(object):
85     '''Class to manage reading of an xml log file
86     '''
87     def __init__(self, filePath):
88         '''Initialization
89         
90         :param filePath str: The xml file to be read
91         '''
92         self.filePath = filePath
93         etree_inst = etree.parse(filePath)
94         self.xmlroot = etree_inst.parse(filePath)
95
96     def getRootAttrib(self):
97         '''Get the attibutes of the self.xmlroot
98         
99         :return: The attributes of the root node
100         :rtype: dict
101         '''
102         return self.xmlroot.attrib
103     
104     def get_attrib(self, node_name):
105         '''Get the attibutes of the node node_name in self.xmlroot
106         
107         :param node_name str: the name of the node
108         :return: the attibutes of the node node_name in self.xmlroot
109         :rtype: dict
110         '''
111         attrib = self.xmlroot.find(node_name).attrib
112         # To be python 3 compatible, convert bytes to str if there are any
113         fixedAttrib = {}
114         for k in attrib.keys():
115             if isinstance(k, bytes):
116                 key = k.decode()
117             else:
118                 key = k
119             if isinstance(attrib[k], bytes):
120                 value = attrib[k].decode()
121             else:
122                 value = attrib[k]
123             fixedAttrib[key] = value
124         return fixedAttrib
125     
126     def get_node_text(self, node):
127         '''Get the text of the first node that has name that corresponds to the parameter node
128         
129         :param node str: the name of the node from which get the text
130         :return: the text of the first node that has name that corresponds to the parameter node
131         :rtype: str
132         '''
133         return self.xmlroot.find(node).text
134