added more exceptions for better error handling

This commit is contained in:
k4yt3x 2019-07-27 17:52:31 -04:00
parent 4639555626
commit 47e787c80c
3 changed files with 26 additions and 10 deletions

View File

@ -11,3 +11,18 @@ Last Modified: July 27, 2019
class ArgumentError(Exception): class ArgumentError(Exception):
def __init__(self, message): def __init__(self, message):
super().__init__(message) super().__init__(message)
class StreamNotFoundError(Exception):
def __init__(self, message):
super().__init__(message)
class UnrecognizedDriverError(Exception):
def __init__(self, message):
super().__init__(message)
class UnsupportedPixelError(Exception):
def __init__(self, message):
super().__init__(message)

View File

@ -157,7 +157,7 @@ class Upscaler:
# initialize waifu2x driver # initialize waifu2x driver
drivers = ['waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan'] drivers = ['waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan']
if self.waifu2x_driver not in drivers: if self.waifu2x_driver not in drivers:
raise Exception(f'Unrecognized waifu2x driver: {self.waifu2x_driver}') raise UnrecognizedDriverError(f'Unrecognized waifu2x driver: {self.waifu2x_driver}')
# it's easier to do multi-threading with waifu2x_converter # it's easier to do multi-threading with waifu2x_converter
# the number of threads can be passed directly to waifu2x_converter # the number of threads can be passed directly to waifu2x_converter
@ -314,7 +314,7 @@ class Upscaler:
# exit if no video stream found # exit if no video stream found
if video_stream_index is None: if video_stream_index is None:
Avalon.error('Aborting: No video stream found') Avalon.error('Aborting: No video stream found')
exit(1) raise StreamNotFoundError('no video stream found')
# get average frame rate of video stream # get average frame rate of video stream
framerate = float(Fraction(video_info['streams'][video_stream_index]['avg_frame_rate'])) framerate = float(Fraction(video_info['streams'][video_stream_index]['avg_frame_rate']))
@ -325,9 +325,9 @@ class Upscaler:
try: try:
self.bit_depth = pixel_formats[fm.pixel_format] self.bit_depth = pixel_formats[fm.pixel_format]
except KeyError as e: except KeyError:
Avalon.error(f'Unsupported pixel format: {fm.pixel_format}') Avalon.error(f'Unsupported pixel format: {fm.pixel_format}')
raise e raise UnsupportedPixelError(f'unsupported pixel format {fm.pixel_format}')
Avalon.info(f'Framerate: {framerate}') Avalon.info(f'Framerate: {framerate}')

View File

@ -43,6 +43,7 @@ smooth and edges sharp.
""" """
# local imports # local imports
from exceptions import *
from upscaler import Upscaler from upscaler import Upscaler
# built-in imports # built-in imports
@ -254,22 +255,22 @@ if args.version:
# arguments sanity check # arguments sanity check
if not args.input: if not args.input:
Avalon.error('You must specify input video file/directory path') Avalon.error('You must specify input video file/directory path')
exit(1) raise ArgumentError('input video path not specified')
if not args.output: if not args.output:
Avalon.error('You must specify output video file/directory path') Avalon.error('You must specify output video file/directory path')
exit(1) raise ArgumentError('output video path not specified')
if (args.driver == 'waifu2x_converter' or args.driver == 'waifu2x_ncnn_vulkan') and args.width and args.height: if (args.driver == 'waifu2x_converter' or args.driver == 'waifu2x_ncnn_vulkan') and args.width and args.height:
Avalon.error('Waifu2x Converter CPP/NCNN accepts only scaling ratio') Avalon.error('Waifu2x Converter CPP/NCNN accepts only scaling ratio')
exit(1) raise ArgumentError('waifu2x-converter supports only scaling ratio')
if args.driver == 'waifu2x_ncnn_vulkan' and (args.ratio > 2 or not args.ratio.is_integer()): if args.driver == 'waifu2x_ncnn_vulkan' and (args.ratio > 2 or not args.ratio.is_integer()):
Avalon.error('Scaling ratio must be 1 or 2 for waifu2x_ncnn_vulkan') Avalon.error('Scaling ratio must be 1 or 2 for waifu2x_ncnn_vulkan')
exit(1) raise ArgumentError('scaling ratio must be 1 or 2 for waifu2x_ncnn_vulkan')
if (args.width or args.height) and args.ratio: if (args.width or args.height) and args.ratio:
Avalon.error('You can only specify either scaling ratio or output width and height') Avalon.error('You can only specify either scaling ratio or output width and height')
exit(1) raise ArgumentError('both scaling ration and width/height specified')
if (args.width and not args.height) or (not args.width and args.height): if (args.width and not args.height) or (not args.width and args.height):
Avalon.error('You must specify both width and height') Avalon.error('You must specify both width and height')
exit(1) raise ArgumentError('only one of width or height is specified')
# check available memory # check available memory
check_memory() check_memory()