Fixed issues with colors on windows

This commit is contained in:
Fabrice Quenneville 2020-12-13 22:35:14 -05:00
parent a1cfc3a472
commit 96594806e0
6 changed files with 60 additions and 57 deletions

View File

@ -11,11 +11,13 @@
import sys import sys
from library.bcolors import BColors
from library.video import Video from library.video import Video
from library.medialibrary import MediaLibrary from library.medialibrary import MediaLibrary
from library.tools import detect_ffmpeg, user_confirm from library.tools import detect_ffmpeg, user_confirm
import colorama
colorama.init()
def main(): def main():
''' '''
MediaCurator's main function MediaCurator's main function
@ -23,14 +25,14 @@ def main():
# confirm that the command has enough parameters # confirm that the command has enough parameters
if len(sys.argv) < 2: if len(sys.argv) < 2:
print(f"{BColors.FAIL}ERROR: Command not understood, please see documentation.{BColors.ENDC}") print(f"{colorama.Fore.RED}ERROR: Command not understood, please see documentation.{colorama.Style.RESET_ALL}")
# confirm that ffmpeg in indeed installed # confirm that ffmpeg in indeed installed
ffmpeg_version = detect_ffmpeg() ffmpeg_version = detect_ffmpeg()
if not ffmpeg_version: if not ffmpeg_version:
print(f"{BColors.FAIL}No ffmpeg version detected{BColors.ENDC}") print(f"{colorama.Fore.RED}No ffmpeg version detected{colorama.Style.RESET_ALL}")
exit() exit()
print(f"{BColors.OKBLUE}ffmpeg version detected: {ffmpeg_version}{BColors.ENDC}") print(f"{colorama.Fore.BLUE}ffmpeg version detected: {ffmpeg_version}{colorama.Style.RESET_ALL}")
# Get/load command parameters # Get/load command parameters
directories = [] directories = []
@ -43,9 +45,9 @@ def main():
for arg in sys.argv: for arg in sys.argv:
# Confirm with the user that he selected to delete found files # Confirm with the user that he selected to delete found files
if "-del" in arg: if "-del" in arg:
print(f"{BColors.WARNING}WARNING: Delete option selected!{BColors.ENDC}") print(f"{colorama.Fore.YELLOW}WARNING: Delete option selected!{colorama.Style.RESET_ALL}")
if not user_confirm(f"{BColors.WARNING}Are you sure you wish to delete all found results after selected operations are succesfull ? [Y/N] ?{BColors.ENDC}"): if not user_confirm(f"Are you sure you wish to delete all found results after selected operations are succesfull ? [Y/N] ?", color="yellow"):
print(f"{BColors.OKGREEN}Exiting!{BColors.ENDC}") print(f"{colorama.Fore.GREEN}Exiting!{colorama.Style.RESET_ALL}")
exit() exit()
elif "-in:" in arg: elif "-in:" in arg:
inputs += arg[4:].split(",") inputs += arg[4:].split(",")
@ -66,7 +68,7 @@ def main():
elif len(directories) > 0: elif len(directories) > 0:
medialibrary = MediaLibrary(directories = directories, inputs = inputs, filters = filters) medialibrary = MediaLibrary(directories = directories, inputs = inputs, filters = filters)
else: else:
print(f"{BColors.FAIL}ERROR: No files or directories selected.{BColors.ENDC}") print(f"{colorama.Fore.RED}ERROR: No files or directories selected.{colorama.Style.RESET_ALL}")
# Actions # Actions
@ -80,12 +82,12 @@ def main():
if medialibrary.videos[filepath].useful: if medialibrary.videos[filepath].useful:
if "formated" in printop or "verbose" in printop: if "formated" in printop or "verbose" in printop:
if medialibrary.videos[filepath].error: if medialibrary.videos[filepath].error:
print(f"{BColors.FAIL}{medialibrary.videos[filepath].fprint()}{BColors.ENDC}") print(f"{colorama.Fore.RED}{medialibrary.videos[filepath].fprint()}{colorama.Style.RESET_ALL}")
else: else:
print(medialibrary.videos[filepath].fprint()) print(medialibrary.videos[filepath].fprint())
else: else:
if medialibrary.videos[filepath].error: if medialibrary.videos[filepath].error:
print(f"{BColors.FAIL}{medialibrary.videos[filepath]}{BColors.ENDC}") print(f"{colorama.Fore.RED}{medialibrary.videos[filepath]}{colorama.Style.RESET_ALL}")
else: else:
print(medialibrary.videos[filepath]) print(medialibrary.videos[filepath])
@ -102,12 +104,12 @@ def main():
if medialibrary.videos[filepath].useful: if medialibrary.videos[filepath].useful:
if "formated" in printop or "verbose" in printop: if "formated" in printop or "verbose" in printop:
if medialibrary.videos[filepath].error: if medialibrary.videos[filepath].error:
print(f"{BColors.FAIL}{medialibrary.videos[filepath].fprint()}{BColors.ENDC}") print(f"{colorama.Fore.RED}{medialibrary.videos[filepath].fprint()}{colorama.Style.RESET_ALL}")
else: else:
print(medialibrary.videos[filepath].fprint()) print(medialibrary.videos[filepath].fprint())
else: else:
if medialibrary.videos[filepath].error: if medialibrary.videos[filepath].error:
print(f"{BColors.FAIL}{medialibrary.videos[filepath]}{BColors.ENDC}") print(f"{colorama.Fore.RED}{medialibrary.videos[filepath]}{colorama.Style.RESET_ALL}")
else: else:
print(medialibrary.videos[filepath]) print(medialibrary.videos[filepath])
@ -131,14 +133,14 @@ def main():
vcodec = "x265" vcodec = "x265"
# Verbosing # Verbosing
print(f"{BColors.OKGREEN}****** Starting conversion {counter} of {len(keylist)}: '{BColors.OKCYAN}{medialibrary.videos[filepath].filename_origin}{BColors.OKGREEN}' from {BColors.OKCYAN}{medialibrary.videos[filepath].codec}{BColors.OKGREEN} to {BColors.OKCYAN}{vcodec}{BColors.OKGREEN}...{BColors.ENDC}") print(f"{colorama.Fore.GREEN}****** Starting conversion {counter} of {len(keylist)}: '{colorama.Fore.CYAN}{medialibrary.videos[filepath].filename_origin}{colorama.Fore.GREEN}' from {colorama.Fore.CYAN}{medialibrary.videos[filepath].codec}{colorama.Fore.GREEN} to {colorama.Fore.CYAN}{vcodec}{colorama.Fore.GREEN}...{colorama.Style.RESET_ALL}")
print(f"{BColors.OKCYAN}Original file:{BColors.ENDC}") print(f"{colorama.Fore.CYAN}Original file:{colorama.Style.RESET_ALL}")
if "formated" in printop or "verbose" in printop: if "formated" in printop or "verbose" in printop:
print(medialibrary.videos[filepath].fprint()) print(medialibrary.videos[filepath].fprint())
else: else:
print(medialibrary.videos[filepath]) print(medialibrary.videos[filepath])
print(f"{BColors.OKGREEN}Converting please wait...{BColors.ENDC}", end="\r") print(f"{colorama.Fore.GREEN}Converting please wait...{colorama.Style.RESET_ALL}", end="\r")
# Converting # Converting
if medialibrary.videos[filepath].convert(verbose = "verbose" in printop): if medialibrary.videos[filepath].convert(verbose = "verbose" in printop):
@ -151,17 +153,17 @@ def main():
medialibrary.videos[newfpath] = Video(newfpath, verbose = "verbose" in printop) medialibrary.videos[newfpath] = Video(newfpath, verbose = "verbose" in printop)
# Verbose # Verbose
print(f"{BColors.OKGREEN}Successfully converted '{medialibrary.videos[filepath].filename_origin}'{BColors.OKCYAN}({medialibrary.videos[filepath].filesize}mb){BColors.OKGREEN} to '{medialibrary.videos[newfpath].filename_origin}'{BColors.OKCYAN}({medialibrary.videos[newfpath].filesize}mb){BColors.OKGREEN}, {BColors.OKCYAN}new file:{BColors.ENDC}") print(f"{colorama.Fore.GREEN}Successfully converted '{medialibrary.videos[filepath].filename_origin}'{colorama.Fore.CYAN}({medialibrary.videos[filepath].filesize}mb){colorama.Fore.GREEN} to '{medialibrary.videos[newfpath].filename_origin}'{colorama.Fore.CYAN}({medialibrary.videos[newfpath].filesize}mb){colorama.Fore.GREEN}, {colorama.Fore.CYAN}new file:{colorama.Style.RESET_ALL}")
if "formated" in printop or "verbose" in printop: if "formated" in printop or "verbose" in printop:
if medialibrary.videos[newfpath].error: if medialibrary.videos[newfpath].error:
print(f"{BColors.FAIL}{medialibrary.videos[newfpath].fprint()}{BColors.ENDC}") print(f"{colorama.Fore.RED}{medialibrary.videos[newfpath].fprint()}{colorama.Style.RESET_ALL}")
else: else:
print(medialibrary.videos[newfpath].fprint()) print(medialibrary.videos[newfpath].fprint())
else: else:
if medialibrary.videos[newfpath].error: if medialibrary.videos[newfpath].error:
print(f"{BColors.FAIL}{medialibrary.videos[newfpath]}{BColors.ENDC}") print(f"{colorama.Fore.RED}{medialibrary.videos[newfpath]}{colorama.Style.RESET_ALL}")
else: else:
print(medialibrary.videos[newfpath]) print(medialibrary.videos[newfpath])

View File

@ -1,15 +0,0 @@
#!/usr/bin/env python3
'''Fancy colors!'''
class BColors:
''' from https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-python '''
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

View File

@ -6,10 +6,12 @@
from pathlib import Path from pathlib import Path
import sys import sys
from .bcolors import BColors
from .video import Video from .video import Video
from .tools import deletefile from .tools import deletefile
import colorama
colorama.init()
class MediaLibrary(): class MediaLibrary():
''' '''
Contains the information and methods of a video file. Contains the information and methods of a video file.
@ -55,7 +57,7 @@ class MediaLibrary():
Save them to the videos dictionary Save them to the videos dictionary
''' '''
print(f"{BColors.OKGREEN}Scanning files in {', '.join(map(str, self.directories))} for videos{BColors.ENDC}") print(f"{colorama.Fore.GREEN}Scanning files in {', '.join(map(str, self.directories))} for videos{colorama.Fore.RESET}")
videolist = [] videolist = []
for directory in self.directories: for directory in self.directories:
@ -83,7 +85,7 @@ class MediaLibrary():
videolist = [video for video in videolist_tmp if video.is_file()] videolist = [video for video in videolist_tmp if video.is_file()]
# Map it all to the videos dictionary as initiated Video objects # Map it all to the videos dictionary as initiated Video objects
print(f"{BColors.OKGREEN}Analazing {len(videolist)} videos in {', '.join(map(str, self.directories))}{BColors.ENDC}") print(f"{colorama.Fore.GREEN}Analazing {len(videolist)} videos in {', '.join(map(str, self.directories))}{colorama.Fore.RESET}")
iteration = 0 iteration = 0
for video in videolist: for video in videolist:
if verbose: if verbose:
@ -95,7 +97,7 @@ class MediaLibrary():
def filter_videos(self, verbose = False): def filter_videos(self, verbose = False):
''' Mark useless videos in the videos dictionary (default is useful) ''' ''' Mark useless videos in the videos dictionary (default is useful) '''
print(f"{BColors.OKGREEN}Filtering {len(self.videos)} videos for the requested parameters{BColors.ENDC}") print(f"{colorama.Fore.GREEN}Filtering {len(self.videos)} videos for the requested parameters{colorama.Fore.RESET}")
for filepath in self.videos: for filepath in self.videos:
@ -146,7 +148,7 @@ class MediaLibrary():
self.videos[filepath].useful = useful self.videos[filepath].useful = useful
print(f"{BColors.OKGREEN}Found {len([filepath for filepath in self.videos if self.videos[filepath].useful])} videos for the requested parameters{BColors.ENDC}") print(f"{colorama.Fore.GREEN}Found {len([filepath for filepath in self.videos if self.videos[filepath].useful])} videos for the requested parameters{colorama.Fore.RESET}")
def unwatch(self, filepath, delete = False): def unwatch(self, filepath, delete = False):
''' remove a video from the index and delete it if requested''' ''' remove a video from the index and delete it if requested'''

View File

@ -5,7 +5,9 @@
import subprocess import subprocess
import os import os
from .bcolors import BColors
import colorama
colorama.init()
def detect_ffmpeg(): def detect_ffmpeg():
@ -19,9 +21,14 @@ def detect_ffmpeg():
pass pass
return False return False
def user_confirm(question): def user_confirm(question, color=False):
'''Returns the user answer to a yes or no question''' '''Returns the user answer to a yes or no question'''
answer = input(question) if color == "yellow":
print(colorama.Fore.YELLOW, end = '')
answer = input(question)
print(colorama.Fore.RESET)
else:
answer = input(question)
if answer.lower() in ["y","yes"]: if answer.lower() in ["y","yes"]:
return True return True
elif answer.lower() in ["n","no"]: elif answer.lower() in ["n","no"]:
@ -34,8 +41,8 @@ def deletefile(filename):
try: try:
os.remove(filename) os.remove(filename)
except OSError: except OSError:
print(f"{BColors.FAIL}Error deleting {filename}{BColors.ENDC}") print(f"{colorama.Fore.RED}Error deleting {filename}{colorama.Fore.RESET}")
return False return False
print(f"{BColors.OKGREEN}Successfully deleted {filename}{BColors.ENDC}") print(f"{colorama.Fore.GREEN}Successfully deleted {filename}{colorama.Fore.RESET}")
return True return True

View File

@ -1,12 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
'''Its a video!''' '''Its a video!'''
from .bcolors import BColors
from .tools import deletefile from .tools import deletefile
import subprocess import subprocess
import os import os
import sys import sys
import colorama
colorama.init()
class Video(): class Video():
'''Contains the information and methods of a video file.''' '''Contains the information and methods of a video file.'''
@ -28,8 +30,12 @@ class Video():
''' creates and analyse a video file ''' ''' creates and analyse a video file '''
#Breaking down the full path in its components #Breaking down the full path in its components
self.path = str(filepath)[:str(filepath).rindex("/") + 1] if os.name == 'nt':
self.filename_origin = str(filepath)[str(filepath).rindex("/") + 1:] self.path = str(filepath)[:str(filepath).rindex("\\") + 1]
self.filename_origin = str(filepath)[str(filepath).rindex("\\") + 1:]
else:
self.path = str(filepath)[:str(filepath).rindex("/") + 1]
self.filename_origin = str(filepath)[str(filepath).rindex("/") + 1:]
if not os.path.exists(filepath): if not os.path.exists(filepath):
self.error = f"FileNotFoundError: [Errno 2] No such file or directory: '{filepath}'" self.error = f"FileNotFoundError: [Errno 2] No such file or directory: '{filepath}'"
@ -53,8 +59,8 @@ class Video():
self.definition = 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"{colorama.Fore.RED}There seams to be an error with \"{filepath}\"{colorama.Fore.RESET}")
print(f"{BColors.FAIL} {self.error}{BColors.ENDC}") print(f"{colorama.Fore.RED} {self.error}{colorama.Fore.RESET}")
def __str__(self): def __str__(self):
'''Returns a short formated string about the video''' '''Returns a short formated string about the video'''
@ -80,9 +86,9 @@ class Video():
if self.error: if self.error:
text += f"{BColors.FAIL}\nErrors:{BColors.ENDC}" text += f"{colorama.Fore.RED}\nErrors:{colorama.Fore.RESET}"
for err in self.error.splitlines(): for err in self.error.splitlines():
text += f"{BColors.FAIL}\n {err}{BColors.ENDC}" text += f"{colorama.Fore.RED}\n {err}{colorama.Fore.RESET}"
return text return text
@ -115,9 +121,9 @@ class Video():
text += f" size: {self.filesize} mb" text += f" size: {self.filesize} mb"
if self.error: if self.error:
text += f"{BColors.FAIL}\n Errors:{BColors.ENDC}" text += f"{colorama.Fore.RED}\n Errors:{colorama.Fore.RESET}"
for err in self.error.splitlines(): for err in self.error.splitlines():
text += f"{BColors.FAIL}\n {err}{BColors.ENDC}" text += f"{colorama.Fore.RED}\n {err}{colorama.Fore.RESET}"
return text return text
@ -164,10 +170,10 @@ class Video():
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
deletefile(self.path + self.filename_tmp) deletefile(self.path + self.filename_tmp)
self.filename_tmp = "" self.filename_tmp = ""
print(f"{BColors.FAIL}Conversion failed {e}{BColors.ENDC}") print(f"{colorama.Fore.RED}Conversion failed {e}{colorama.Fore.RESET}")
return False return False
except KeyboardInterrupt: except KeyboardInterrupt:
print(f"{BColors.WARNING}Conversion cancelled, cleaning up...{BColors.ENDC}") print(f"{colorama.Style.YELLOW}Conversion cancelled, cleaning up...{colorama.Fore.RESET}")
deletefile(self.path + self.filename_tmp) deletefile(self.path + self.filename_tmp)
self.filename_tmp = "" self.filename_tmp = ""
exit() exit()
@ -193,7 +199,7 @@ class Video():
if len(output) > 0: if len(output) > 0:
return output return output
except (subprocess.CalledProcessError, IndexError): except (subprocess.CalledProcessError, IndexError):
return f'{BColors.FAIL}There seams to be a "subprocess.CalledProcessError" error with \"{filepath}\"{BColors.ENDC}' return f'{colorama.Fore.RED}There seams to be a "subprocess.CalledProcessError" error with \"{filepath}\"{colorama.Fore.RESET}'
return False return False

View File

@ -1,2 +1,3 @@
hurry.filesize
pathlib pathlib
colorama
sphinx