smerckel / dbdreader

A reader for binary data files created by Slocum ocean gliders (AUVs)
GNU General Public License v3.0
17 stars 14 forks source link

error on Exception #12

Closed s-pearce closed 2 years ago

s-pearce commented 2 years ago

Hi Lucas, I made an error by accident on trying to open a MultiDBD class call by not calling out the pattern keyword argument and instead using the pattern as the first position argument, which saw the string as a sequence and failed to open the files it thought it had. That is not an unexpected Exception, however, during the Exception, another one was raised (UnboundLocalError) for trying to access and close the dbd object before assignment (see example below). It may require a slight update to the exception handling (although it is probably a pretty obscure error if the class is called correctly).

In [54]: dbd = dbdreader.MultiDBD("01550000.*bd", cacheDir=lcachedir)
WARNING: [line 1581] File * could not be loaded
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
~\anaconda3\lib\site-packages\dbdreader-0.4.9-py3.8-win-amd64.egg\dbdreader\dbdreader.py in __update_dbd_inventory(self, cacheDir)
   1578             try:
-> 1579                 dbd=DBD(fn, cacheDir, raise_exception_if_cac_not_found=False)
   1580             except Exception as e:

~\anaconda3\lib\site-packages\dbdreader-0.4.9-py3.8-win-amd64.egg\dbdreader\dbdreader.py in __init__(self, filename, cacheDir, raise_exception_if_cac_not_found)
    570             self.cacheDir=cacheDir
--> 571         with open(filename,'br') as self.fp:
    572             self.headerInfo,parameterInfo,self.cacheFound, self.cacheID = self.__read_header(self.cacheDir)

OSError: [Errno 22] Invalid argument: '*'

During handling of the above exception, another exception occurred:

UnboundLocalError                         Traceback (most recent call last)
<ipython-input-63-4c6d6b81eea4> in <module>
----> 1 dbd = dbdreader.MultiDBD("01550000.*bd", cacheDir=lcachedir)

~\anaconda3\lib\site-packages\dbdreader-0.4.9-py3.8-win-amd64.egg\dbdreader\dbdreader.py in __init__(self, filenames, pattern, cacheDir, complemented_files_only, complement_files, banned_missions, missions, max_files, **kwds)
   1131             self.pruned_files=self.__prune_unmatched(cacheDir)
   1132
-> 1133         self.__update_dbd_inventory(cacheDir)
   1134         self.parameterNames=dict((k,self.__getParameterList(v)) \
   1135                                      for k,v in self.dbds.items())

~\anaconda3\lib\site-packages\dbdreader-0.4.9-py3.8-win-amd64.egg\dbdreader\dbdreader.py in __update_dbd_inventory(self, cacheDir)
   1582                 logger.debug('Exception was %s', e)
   1583                 filenames.remove(fn)
-> 1584                 dbd.close()
   1585                 continue
   1586             if not dbd.cacheFound:

UnboundLocalError: local variable 'dbd' referenced before assignment
smerckel commented 2 years ago

Hi s-pearce,

It is an easy mistake to make not to pass the "pattern=" with the argument list. To fix this particular error should be not too difficult, but I should think a bit of a solution that handles the situation to distinguish between filenames=[...] and pattern="..." better.

smerckel commented 2 years ago

As of version 0.4.10 this issue has been solved. The UnboundLocalError that got triggered was due to a dbd.close() being out of place. Furthermore, if a string is supplied as first argument to MultiDBD, instead of a list or tuple with filenames, it is assumed that files should be selected according to a pattern based on this string. So dbd = dbdreader.MultiDBD("01550000.*bd") should now just work.

Thanks for reporting the bug!

s-pearce commented 2 years ago

Thanks Lucas.