Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def extract_filenumber(name):
""" extract file number """
fobj = FilenameObject(filename=name)
return fobj.num
# the upper case IMAGE is used for the --help auto-documentation
args.images = expand_args(args.IMAGE)
args.images.sort()
if args.format is None or not args.format.endswith("image"):
if args.format is None:
if args.output is None:
raise argparse.ArgumentError(None, "No format specified. Use -F or -o.")
dummy_filename = args.output
else:
# format looks to be an extension
dummy_filename = "foo." + args.format
# extract file format from file name
filename = fabio.fabioutils.FilenameObject(filename=dummy_filename)
if filename.format is None or len(filename.format) == 0:
raise argparse.ArgumentError(None, "This file extension is unknown. You have also to specify a format using -F.")
elif filename.format is None or len(filename.format) > 1:
formats = [i + "image" for i in filename.format]
formats = ', '.join(formats)
raise argparse.ArgumentError(None, "This file extension correspond to different file formats: '%s'. You have to specify it using -F." % formats)
args.format = filename.format[0] + "image"
if not is_format_supported(args.format):
raise argparse.ArgumentError(None, "Format '%s' is unknown. Use -l to list all available formats." % args.format)
except argparse.ArgumentError as e:
logger.error(e.message)
logger.debug("Backtrace", exc_info=True)
return EXIT_ARGUMENT_FAILURE
imo = FabioImage()
with imo._open(actual_filename) as f:
magic_bytes = f.read(18)
except IOError:
logger.debug("Backtrace", exc_info=True)
raise
else:
imo = None
filetype = None
try:
filetype = do_magic(magic_bytes, filename)
except Exception:
logger.debug("Backtrace", exc_info=True)
try:
file_obj = FilenameObject(filename=filename)
if file_obj is None:
raise Exception("Unable to deconstruct filename")
if (file_obj.format is not None) and\
len(file_obj.format) != 1 and \
isinstance(file_obj.format, list):
# one of OXD/ADSC - should have got in previous
raise Exception("openimage failed on magic bytes & name guess")
filetype = file_obj.format
except Exception:
logger.debug("Backtrace", exc_info=True)
raise IOError("Fabio could not identify " + filename)
if filetype is None:
raise IOError("Fabio could not identify " + filename)
def next_filename(name, padding=True):
""" increment number """
fobj = FilenameObject(filename=name)
fobj.num += 1
if not padding:
fobj.digits = 0
return fobj.tostring()
def __init__(self, filename):
""" create from a filename (String)"""
if isinstance(filename, FilenameObject):
self.obj = filename
else:
self.obj = FilenameObject(filename=filename)
def last_object(self):
"""
Last image in a sequence
:return: file_object
"""
return FilenameObject(self.last())
def next_object(self):
"""
Return the next image
:return: file_object
"""
return FilenameObject(self.next())