Salome HOME
Copyright update 2022
[tools/configuration.git] / config / sconfig / salome_config.py
index 610540634433dba82927714aac5782e525d5e14f..a5faa746f4cffc7df7d56218f9fccfed96e10ada 100644 (file)
@@ -1,4 +1,22 @@
 #  -*- coding: iso-8859-1 -*-
+# Copyright (C) 2016-2022  OPEN CASCADE
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
 
 """
 Manage SALOME configuration.
@@ -61,6 +79,7 @@ __all__ = [
     "commentAttr",
     "versionAttr",
     "urlAttr",
+    "sha1Attr",
     "supportedTags",
     "supportedAttributes",
     "tagAttributes",
@@ -126,6 +145,12 @@ def urlAttr():
     """
     return "url"
 
+def sha1Attr():
+    """
+    Return XML attribute for sha1 parameter (string).
+    """
+    return "sha1"
+
 def supportedTags():
     """
     Return list of all supported XML tags (list of strings).
@@ -136,7 +161,7 @@ def supportedAttributes():
     """
     Return list of all supported XML attributes (list of strings).
     """
-    return [nameAttr(), commentAttr(), versionAttr(), urlAttr()]
+    return [nameAttr(), commentAttr(), versionAttr(), urlAttr(), sha1Attr()]
     
 def tagAttributes(tag, force = False):
     """
@@ -173,6 +198,7 @@ def tagAttributes(tag, force = False):
         pass
         attrs[urlAttr()]      = False
         attrs[commentAttr()]  = False
+        attrs[sha1Attr()]     = False
     return attrs
 
 def tagChildren(tag):
@@ -243,21 +269,22 @@ class CfgTool(object):
         """
         self.enc = "utf-8"
         self.cfgFile = cfgFile if cfgFile else defaultConfFile()
+        self.parents = {}
         try:
             self.tree = ET.parse(self.cfgFile).getroot()
             self._checkConfig()
             pass
-        except IOError, e:
+        except IOError as e:
             self.tree = self._new()
             pass
-        except exceptionClass, e:
+        except exceptionClass as e:
             if e.code == 3: # no element found, it's OK
                 self.tree = self._new()
             else:
                 raise Exception("bad XML file %s: %s" % (self.cfgFile, str(e)))
             pass
-        except Exception, e:
-            raise Exception("unkwnown error: %s" % str(e))
+        except Exception as e:
+            raise Exception("unknown error: %s" % str(e))
         pass
     
     def encoding(self):
@@ -323,7 +350,7 @@ class CfgTool(object):
         tag = path[-1][0]
         params = {}
         # process keyword arguments
-        for param, value in kwargs.items():
+        for param, value in list(kwargs.items()):
             if param not in tagAttributes(tag):
                 raise Exception("unsupported parameter %s for target %s" % (param, target))
             params[param] = value
@@ -344,7 +371,7 @@ class CfgTool(object):
             pass
         # create / modify target
         elem = self._findPath(path, True)
-        for param, value in params.items():
+        for param, value in list(params.items()):
             elem.set(param, value)
             pass
         self._write()
@@ -468,7 +495,7 @@ class CfgTool(object):
         Return value is new XML element (xml.etree.ElementTree.Element).
         """
         child = ET.SubElement(elem, tag)
-        child._parent = elem # set parent!!!
+        self.parents[child] = elem # set parent!!!
         return child
 
     def _processTarget(self, target):
@@ -528,7 +555,7 @@ class CfgTool(object):
         elem = self.tree
         for tag, name in path[1:]:
             if name:
-                children = filter(lambda i: i.tag == tag and i.get(nameAttr()) == name, elem.getchildren())
+                children = [i for i in elem.getchildren() if i.tag == tag and i.get(nameAttr()) == name]
                 if len(children) > 1:
                     raise Exception("error parsing target path: more than one child element found")
                 elif len(children) == 1:
@@ -542,7 +569,7 @@ class CfgTool(object):
                     return None
                 pass
             else:
-                children = filter(lambda i: i.tag == tag, elem.getchildren())
+                children = [i for i in elem.getchildren() if i.tag == tag]
                 if len(children) > 1:
                     raise Exception("error parsing target path: more than one child element found")
                 elif len(children) == 1:
@@ -571,14 +598,14 @@ class CfgTool(object):
             _name = _obj.tag
             attrs = tagAttributes(_obj.tag, True)
             if nameAttr() in attrs and attrs[nameAttr()]:
-                if nameAttr() not in _obj.keys(): _name += " [unnamed]"
+                if nameAttr() not in list(_obj.keys()): _name += " [unnamed]"
                 else: _name += " [%s]" % _obj.get(nameAttr())
                 pass
             return _name
         path = []
         while elem is not None:
             path.append(_mkname(elem))
-            elem = elem._parent if hasattr(elem, "_parent") else None
+            elem = self.parents.get(elem)
             pass
         path.reverse()
         return pathSeparator().join(path)
@@ -596,7 +623,7 @@ class CfgTool(object):
         """
         result = []
         result += [i.get(nameAttr()) for i in \
-                       filter(lambda i: i.tag == param and i.get(nameAttr()), elem.getchildren())]
+                       [i for i in elem.getchildren() if i.tag == param and i.get(nameAttr())]]
         for c in elem.getchildren():
             result += self._children(c, param)
             pass
@@ -619,7 +646,7 @@ class CfgTool(object):
                 et.write(f, self.encoding())
                 pass
             pass
-        except IOError, e:
+        except IOError as e:
             raise Exception("can't write to %s: %s" % (self.cfgFile, e.strerror))
         pass
 
@@ -661,15 +688,15 @@ class CfgTool(object):
             return
         indent = "  "
         # dump element
-        print "%s%s" % (indent * level, elem.tag)
+        print("%s%s" % (indent * level, elem.tag))
         attrs = tagAttributes(elem.tag, True)
         format = "%" + "-%ds" % max([len(i) for i in supportedAttributes()]) + " : %s"
         for a in attrs:
-            if a in elem.attrib.keys():
-                print indent*(level+1) + format % (a, elem.get(a))
+            if a in list(elem.attrib.keys()):
+                print(indent*(level+1) + format % (a, elem.get(a)))
                 pass
             pass
-        print
+        print()
         # dump all childrens recursively
         for c in elem.getchildren():
             self._dump(c, level+1)
@@ -702,7 +729,7 @@ class CfgTool(object):
             errors.append("bad XML element: %s" % elem.tag)
         else:
             # check attributes
-            attrs = elem.keys()
+            attrs = list(elem.keys())
             for attr in attrs:
                 if attr not in tagAttributes(elem.tag, True):
                     errors.append("unsupported attribute '%s' for XML element '%s'" % (attr, elem.tag))
@@ -711,7 +738,7 @@ class CfgTool(object):
             # check all childrens recursively
             children = elem.getchildren()
             for child in children:
-                child._parent = elem # set parent!!!
+                self.parents[child] = elem # set parent!!!
                 self._checkTag(child, elem.tag, errors)
                 pass
             pass
@@ -729,14 +756,14 @@ class CfgTool(object):
         attrs = tagAttributes(elem.tag, True)
         # check mandatory attributes
         for attr in attrs:
-            if attrs[attr] and (attr not in elem.keys() or not elem.get(attr).strip()):
+            if attrs[attr] and (attr not in list(elem.keys()) or not elem.get(attr).strip()):
                 errors.append("mandatory parameter '%s' of object '%s' is not set" % (attr, self._path(elem)))
                 pass
             pass
         # specific check for particular XML element
         try:
             self._checkObject(elem)
-        except Exception, e:
+        except Exception as e:
             errors.append("%s : %s" % (self._path(elem), str(e)))
         # check all childrens recursively
         for c in elem.getchildren():