Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
authorLaurent Bessard
Fri, 27 Sep 2013 17:49:40 +0200
changeset 2160 75349c51a34b
parent 2159 93797d4303a3
child 2161 4bea1d98db46
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file
etherlab/EthercatMaster.py
etherlab/etherlab.py
--- a/etherlab/EthercatMaster.py	Tue Sep 24 15:18:25 2013 +0200
+++ b/etherlab/EthercatMaster.py	Fri Sep 27 17:49:40 2013 +0200
@@ -219,26 +219,44 @@
     def __init__(self):
         config_filepath = self.ConfigFileName()
         config_is_saved = False
+        self.Config = None
         if os.path.isfile(config_filepath):
             config_xmlfile = open(config_filepath, 'r')
-            self.Config = etree.fromstring(
-                config_xmlfile.read(), EtherCATConfigParser)
-            config_is_saved = True
+            try:
+                self.Config, error = \
+                    EtherCATConfigParser.LoadXMLString(config_xmlfile.read())
+                if error is None:
+                    config_is_saved = True
+            except Exception, e:
+                error = e.message
             config_xmlfile.close()
-        
-        else:
+            
+            if error is not None:
+                self.GetCTRoot().logger.write_error(
+                    _("Couldn't load %s network configuration file.") % CTNName)    
+            
+        if self.Config is None:
             self.Config = EtherCATConfigParser.CreateElement("EtherCATConfig")
         
         process_filepath = self.ProcessVariablesFileName()
         process_is_saved = False
+        self.ProcessVariables = None
         if os.path.isfile(process_filepath):
             process_xmlfile = open(process_filepath, 'r')
-            self.ProcessVariables = etree.fromstring(
-                process_xmlfile.read(), ProcessVariablesParser)
-            process_is_saved = True
+            try:
+                self.ProcessVariables, error = \
+                    ProcessVariablesParser.LoadXMLString(process_xmlfile.read())
+                if error is None:
+                    process_is_saved = True
+            except Exception, e:
+                error = e.message
             process_xmlfile.close()
             
-        else:
+            if error is not None:
+                self.GetCTRoot().logger.write_error(
+                    _("Couldn't load %s network process variables file.") % CTNName)    
+            
+        if self.ProcessVariables is None:
             self.ProcessVariables = ProcessVariablesParser.CreateElement("ProcessVariables")
         
         if config_is_saved and process_is_saved:
@@ -541,7 +559,6 @@
                 type_infos = slave.getType()
                 device, module_extra_params = self.GetModuleInfos(type_infos)
         if device is not None:
-            print "Get Entries List", limits
             entries = device.GetEntriesList(limits)
             #print entries
             entries_list = entries.items()
--- a/etherlab/etherlab.py	Tue Sep 24 15:18:25 2013 +0200
+++ b/etherlab/etherlab.py	Fri Sep 27 17:49:40 2013 +0200
@@ -6,7 +6,7 @@
 
 from xmlclass import *
 
-from ConfigTreeNode import ConfigTreeNode
+from ConfigTreeNode import ConfigTreeNode, XSDSchemaErrorMessage
 from PLCControler import UndoBuffer, LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
 
 from EthercatSlave import ExtractHexDecValue, ExtractName
@@ -203,10 +203,12 @@
                 
                 xmlfile = open(filepath, 'r')
                 try:
-                    self.modules_infos = etree.fromstring(
-                        xmlfile.read(), EtherCATInfoParser)
-                except:
-                    pass
+                    self.modules_infos, error = EtherCATInfoParser.LoadXMLString(xmlfile.read())
+                    if error is not None:
+                        self.GetCTRoot().logger.write_warning(
+                            XSDSchemaErrorMessage % (filepath + error))
+                except Exception, exc:
+                    self.modules_infos, error = None, unicode(exc)
                 xmlfile.close()
                 
                 if self.modules_infos is not None:
@@ -233,8 +235,13 @@
                             raise ValueError, "Not such group \"%\"" % device_group
                         vendor_category["groups"][device_group]["devices"].append(
                             (device.getType().getcontent(), device))
-
-        return self.Library 
+                
+                else:
+                        
+                    self.GetCTRoot().logger.write_error(
+                        _("Couldn't load %s XML file:\n%s") % (filepath, error))
+                
+        return self.Library
 
     def GetModulesLibrary(self, profile_filter=None):
         if self.Library is None: