Bugfixes for the library

* Make sure library returns a list of translated names
* Replace generic catch block to handle exceptions seperately

Bugfixes for insert_name.py

* Make errors more verbose when trying to insert duplicate names in the database

refs: #2840, #2839, #2838

Change-Id: I45e978856c9492ec8fb67ae00afc69265ab28d28
diff --git a/lib/ndn_cmmap_translators/atmos2ndn_parser/conf_file_parser.py b/lib/ndn_cmmap_translators/atmos2ndn_parser/conf_file_parser.py
index f2c2a02..ebf52e9 100644
--- a/lib/ndn_cmmap_translators/atmos2ndn_parser/conf_file_parser.py
+++ b/lib/ndn_cmmap_translators/atmos2ndn_parser/conf_file_parser.py
@@ -27,27 +27,32 @@
 import sys, traceback
 
 class ParseConf(object):
-    '''parses the name schema file and returns name mappings for translated
-    output'''
-    def __init__(self, confName=None):
-        self.confFile = confName
-        if self.confFile is None:
-            raise Exception("No configuration file given to parse config")
+    '''parses the name schema file and returns name mappings for translated output'''
+    def __init__(self, confName):
+        self.confName = confName
+        if __debug__:
+            print("Config file name: %s" %(self.confName))
+
         self.filenameMap = []
         self.ndnNameMap = []
         self.seperatorsMap = []
         self.userDefinedConfDir = {}
         self.translator = []
 
-        if __debug__:
-            print(self.confFile)
-
+        #initialize the parser
         self.parser = configparser.SafeConfigParser()
         self.parser.optionxform=str
-        self.parser.read(self.confFile)
+        self.parser.read(self.confName)
         self.fullConf = {}
 
-        #parser now contain a dictionary with the sections in conf
+        #do the mapping
+        res = self.getMappings(confName)
+        if res is False:
+          print("Error getting values from config file")
+          raise error.with_traceback(sys.exc_info()[2])
+
+
+    def _parseConf(self):
         #iterate over them and store the name components in fullConf
         try:
             for sectionName in self.parser.sections():
@@ -57,25 +62,47 @@
                 self.fullConf[sectionName] = self.conf
             if __debug__:
                 print(self.fullConf)
-        except (KeyError, TypeError):
-            raise error.with_traceback(sys.exc_info()[2])
+        except KeyError:
+            print("Key %s is not found in config file" %(name))
+            print(sys.exc_info()[2])
+        except TypeError:
+            print("TypeError while parsing config file")
+            print(sys.exc_info()[2])
+        return self.fullConf
 
-    def getMappings(self):
-        '''parses the schema file and provides name mappings'''
+    def _doParsing(self):
+        #parser now contain a dictionary with the sections in conf
+        # first elements are section and second ones are variables defined in config file
         try:
             self.filenameMap = self.fullConf['Name']['filenameMapping'].replace(" ", "").split(',')
-
             self.ndnNameMap = self.fullConf['Name']['ndnMapping'].replace(" ", "").split(',')
+
             # user defined components look like this
             #activity:cmip5, subactivity:atmos, organization:csu, ensemble:r3i1p1
             userDefinedConf = self.fullConf['Name']['userDefinedComps'].replace(" ", "").split(',')
             for item in userDefinedConf:
                 key, value = item.split(":")
                 self.userDefinedConfDir[key] = [value]
-
             self.seperatorsMap = self.fullConf['Name']['seperators'].replace(" ", "").split(',')
-
             #reads which translator to use
             self.translator = self.fullConf['Translator']['translator'].replace(" ", "")
-        except (KeyError, TypeError):
-            raise error.with_traceback(sys.exc_info()[2])
+        except KeyError:
+            print("Key %s is not found in config file" %(name))
+            print(sys.exc_info()[2])
+        except TypeError:
+            print("TypeError while parsing config file")
+            print(sys.exc_info()[2])
+
+    def getMappings(self, confName):
+        '''parses the schema file and provides name mappings'''
+        fullConf = self._parseConf()
+        #if dict is not empty
+        if fullConf:
+          res = self._doParsing()
+          if len(self.filenameMap) == 0 or len(self.ndnNameMap) == 0 or len(self.translator) == 0:
+            return False
+          else:
+            return True
+        else:
+          return False
+
diff --git a/lib/ndn_cmmap_translators/atmos2ndn_translators/cmip5_translator.py b/lib/ndn_cmmap_translators/atmos2ndn_translators/cmip5_translator.py
index 2cc3ca6..40950bd 100644
--- a/lib/ndn_cmmap_translators/atmos2ndn_translators/cmip5_translator.py
+++ b/lib/ndn_cmmap_translators/atmos2ndn_translators/cmip5_translator.py
@@ -76,7 +76,7 @@
       filenameMap = parsedConfig.filenameMap
       ndnNameMap = parsedConfig.ndnNameMap
       seperatorsMap = parsedConfig.seperatorsMap
-      confName = parsedConfig.confFile
+      confName = parsedConfig.confName
       fileCompDict = {}
       metadataCompDict = {}
 
diff --git a/lib/ndn_cmmap_translators/atmos_translator.py b/lib/ndn_cmmap_translators/atmos_translator.py
index 2c6a380..280d0d2 100644
--- a/lib/ndn_cmmap_translators/atmos_translator.py
+++ b/lib/ndn_cmmap_translators/atmos_translator.py
@@ -33,27 +33,32 @@
     #pass the config file to parser module, which will return and object
     #with all the mappings. The mappings tell us which name component
     #comes from where and what should be the order of the components
+
+    #library would throw exceptions, if any
     parsedConfig = conf_file_parser.ParseConf(configPath)
-    res = parsedConfig.getMappings()
+
 
     #do the translation
-    ndnName = translate.translate(parsedConfig, dataFilepath)
+    ndnNames = translate.translate(parsedConfig, dataFilepath)
     if __debug__:
-      print("NDN name in main: %s" %(ndnName))
+      print("NDN names in atmos_translate module: %s" %(ndnNames))
+    return ndnNames
+
 
 def main():
-    '''parse command line arguments, gives back configFilename and dataFilename
-    we then pass these to the subsequent modules.'''
+
+    '''This main is for debug only, run with the debug flag on.
+    Otherwise call argsForTranslation from a wrapper function'''
 
     userArgs = cmd_arg_parser.InputParser()
     userArgs.parse()
     configFile = userArgs.confFilepath
-    print("config file '%s', data path '%s'" %(configFile, userArgs.dataFilepath))
     if __debug__:
         print("config file '%s', data path '%s'" %(configFile, userArgs.dataFilepath))
 
     #call the translator module
-    argsForTranslation(userArgs.dataFilepath, configFile)
+    ndnNames = argsForTranslation(userArgs.dataFilepath, configFile)
+
 
 if __name__ == '__main__':
     main()
diff --git a/tools/insert_names.py b/tools/insert_names.py
index 88fadb3..3c3bb6f 100644
--- a/tools/insert_names.py
+++ b/tools/insert_names.py
@@ -91,7 +91,7 @@
     print("Inserted record %s" %(name))
     return True
   except sql.Error as err:
-    print("Error inserting name %s" %(err.msg))
+    print("Error inserting name %s, \nError:%s" %(name, err.msg))
     return False
 
 if __name__ == '__main__':
@@ -100,6 +100,10 @@
 
   #do the translation, the library should provide error messages, if any
   ndnNames = atmos_translator.argsForTranslation(datafilePath, configFilepath)
+  if ndnNames is False:
+    print("Error parsing config file, exiting")
+    sys.exit(-1)
+
   if len(ndnNames) == 0:
     print("No name returned from the translator, exiting.")
     sys.exit(-1)