# HG changeset patch
# User Laurent Bessard
# Date 1380296980 -7200
# Node ID 75349c51a34b682f758d37d059f83de75da6e534
# Parent  93797d4303a3d352a52e06a7703b020342f10248
Added support for loading XML file even if not following XSD schema (but still following XML syntax), warning user of errors in XML file

diff -r 93797d4303a3 -r 75349c51a34b etherlab/EthercatMaster.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()
diff -r 93797d4303a3 -r 75349c51a34b etherlab/etherlab.py
--- 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: