Better handling of FileNotFoundError

This commit is contained in:
Fabrice Quenneville 2020-11-22 15:01:20 -05:00
parent d1ba9c9571
commit e2315c3e7d
2 changed files with 32 additions and 19 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
**/__pycache__ **/__pycache__
test.sh test.sh
renamelist.txt renamelist.txt
.vscode

View File

@ -12,7 +12,7 @@ class Video():
path = str() path = str()
filename_origin = str() filename_origin = str()
filesize = int() filesize = int(0)
filename_new = str() filename_new = str()
filename_tmp = str() filename_tmp = str()
useful = bool() useful = bool()
@ -31,21 +31,26 @@ class Video():
self.path = str(filepath)[:str(filepath).rindex("/") + 1] self.path = str(filepath)[:str(filepath).rindex("/") + 1]
self.filename_origin = str(filepath)[str(filepath).rindex("/") + 1:] self.filename_origin = str(filepath)[str(filepath).rindex("/") + 1:]
# Marking useful is user manually set it. if not os.path.exists(filepath):
self.useful = useful self.error = f"FileNotFoundError: [Errno 2] No such file or directory: '{filepath}'"
self.useful = useful
#Gathering information on the video else:
self.filesize = self.detect_filesize(filepath) # Marking useful is user manually set it.
self.error = self.detect_fferror(filepath) self.useful = useful
self.codec = self.detect_codec(filepath)
try: #Gathering information on the video
self.width, self.height = self.detect_resolution(filepath) self.filesize = self.detect_filesize(filepath)
self.definition = self.detect_definition( self.error = self.detect_fferror(filepath)
width = self.width, self.codec = self.detect_codec(filepath)
height = self.height ) try:
except: self.width, self.height = self.detect_resolution(filepath)
self.width, self.height = False, False self.definition = self.detect_definition(
self.definition = False width = self.width,
height = self.height )
except:
self.width, self.height = False, False
self.definition = False
if self.error and verbose: if self.error and verbose:
print(f"{BColors.FAIL}There seams to be an error with \"{filepath}\"{BColors.ENDC}") print(f"{BColors.FAIL}There seams to be an error with \"{filepath}\"{BColors.ENDC}")
@ -54,10 +59,13 @@ class Video():
def __str__(self): def __str__(self):
'''Returns a short formated string about the video''' '''Returns a short formated string about the video'''
if type(self.error) is str and "FileNotFoundError" in self.error:
return self.error
text = f"{self.codec} - " text = f"{self.codec} - "
# If the first character of the definition is not a number (ie UHD and not 720p) upper it # If the first character of the definition is not a number (ie UHD and not 720p) upper it
if self.definition[0] and not self.definition[0].isnumeric(): if self.definition and self.definition[0] and not self.definition[0].isnumeric():
text += f"{self.definition.upper()}: ({self.width}x{self.height}) - " text += f"{self.definition.upper()}: ({self.width}x{self.height}) - "
else: else:
text += f"{self.definition}: ({self.width}x{self.height}) - " text += f"{self.definition}: ({self.width}x{self.height}) - "
@ -85,11 +93,15 @@ class Video():
def fprint(self): def fprint(self):
'''Returns a long formated string about the video ''' '''Returns a long formated string about the video '''
if type(self.error) is str and "FileNotFoundError" in self.error:
return self.error
text = f"{self.path + self.filename_origin}\n" text = f"{self.path + self.filename_origin}\n"
#text += f" Useful: {self.useful}\n" #text += f" Useful: {self.useful}\n"
# If the first character of the definition is not a number (ie UHD and not 720p) upper it # If the first character of the definition is not a number (ie UHD and not 720p) upper it
if self.definition[0] and not self.definition[0].isnumeric(): if self.definition and self.definition[0] and not self.definition[0].isnumeric():
text += f" Definition: {self.definition.upper()}: ({self.width}x{self.height})\n" text += f" Definition: {self.definition.upper()}: ({self.width}x{self.height})\n"
else: else:
text += f" Definition: {self.definition}: ({self.width}x{self.height})\n" text += f" Definition: {self.definition}: ({self.width}x{self.height})\n"