def _openimage(filename): """ determine which format for a filename and return appropriate class which can be used for opening the image """ try: imo = fabioimage() byts = imo._open(filename).read(18) filetype = do_magic(byts) # print filetype if filetype == "marccd" and filename.find("mccd") == -1: # Cannot see a way around this. Need to find something # to distinguish mccd from regular tif... filetype = "tif" #UNUSED filenumber = getnum(filename) except IOError: # File probably does not exist raise except: try: file_obj = deconstruct_filename(filename) if file_obj == None: raise Exception if len(file_obj.format) != 1 and \ type(file_obj.format) != type(["list"]): # one of OXD/ ADSC - should have got in previous raise Exception("openimage failed on magic bytes & name guess") filetype = file_obj.format #UNUSED filenumber = file_obj.num except: #import traceback #traceback.print_exc() raise Exception("Fabio could not identify " + filename) klass_name = "".join(filetype) + 'image' # print "looking for %s in" % klass_name # for i in sys.modules: # if klass_name in i: # print "%s\t%s" % (i, sys.modules[i]) module = sys.modules.get("fabio." + klass_name, None) # if hasattr(__init__, klass_name): # module = getattr(__init__, klass_name) if module is not None: if hasattr(module, klass_name): klass = getattr(module, klass_name) # print klass else: raise Exception("Module %s has no image class" % module) else: raise Exception("Filetype not known %s %s" % (filename, klass_name)) obj = klass() # skip the read for read header return obj
and return appropriate class which can be used for opening the image """ try: imo = fabioimage() byts = imo._open(filename).read(18) filetype = do_magic(byts) if filetype == "marccd" and filename.find("mccd") == -1: # Cannot see a way around this. Need to find something # to distinguish mccd from regular tif... filetype = "tif" except IOError, error: logger.error("%s: File probably does not exist", error) raise error except: try: file_obj = deconstruct_filename(filename) if file_obj == None: raise Exception("Unable to deconstruct filename") if (file_obj.format is not None) and\ len(file_obj.format) != 1 and \ type(file_obj.format) != type(["list"]): # one of OXD/ ADSC - should have got in previous raise Exception("openimage failed on magic bytes & name guess") filetype = file_obj.format #UNUSED filenumber = file_obj.num except Exception, error: logger.error(error) import traceback traceback.print_exc() raise Exception("Fabio could not identify " + filename) klass_name = "".join(filetype) + 'image'