Method to generate_tabs_content

This commit is contained in:
cuissedemouche 2022-12-13 20:35:40 +00:00
parent 7cc07c2657
commit 1df8f015c7
2 changed files with 99 additions and 56 deletions

View File

@ -5,6 +5,11 @@ class FileCmds:
def __init__(self, filePath): def __init__(self, filePath):
super().__init__() super().__init__()
self.filePath = filePath self.filePath = filePath
self.trackCount = self.track_count()
self.basicInfo = self.generate_basic()
self.advancedInfo = self.generate_advanced()
self.tabsList = self.generate_tabs_list()
self.tabsContent = self.generate_tabs_content()
def execute_bash(self, cmd): def execute_bash(self, cmd):
@ -12,7 +17,7 @@ class FileCmds:
Execute a bash command Execute a bash command
:type cmd: str :type cmd: str
:return: output :return: str
''' '''
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, error = process.communicate() output, error = process.communicate()
@ -20,34 +25,17 @@ class FileCmds:
def file_exist(self): def file_exist(self):
'''
Check if the file exists
:rtype: bool
'''
if os.path.exists(self.filePath): if os.path.exists(self.filePath):
return True return True
else: else:
print("The file doesn't exist") print("The file doesn't exist")
return False return False
def get_mkv_infos(self):
'''
Process MKV infos into a dictionary
:rtype: list or None
'''
process = self.execute_bash(f"mediainfo --Output=JSON {quote(self.filePath)}")
mkvInfo = json.loads(process)["media"]["track"]
return mkvInfo
# def get_video_track(self):
# '''
# Get the video track infos
# :rtype: dict or None
# '''
# for track in self.mkvInfo:
# if track["@type"] == "Video":
# return track
# print("The file does not contain a video track")
# return(None)
def media_inform(self, inform): def media_inform(self, inform):
''' '''
@ -61,11 +49,11 @@ class FileCmds:
return output return output
def tracksCount(self): def track_count(self):
''' '''
Return the number of track of each type Return the number of track of each type
:rtype: dict :rtype: dict[str,int]
''' '''
trackTypes = [ trackTypes = [
"Video", "Video",
@ -81,10 +69,11 @@ class FileCmds:
count = self.media_inform(f"General;%{trackType}Count%").replace('\n', '') count = self.media_inform(f"General;%{trackType}Count%").replace('\n', '')
if not count: if not count:
count = 0 count = 0
output[trackType] = count output[trackType] = int(count)
return output return output
def generate_basic(self): def generate_basic(self):
''' '''
Return the basic tab Return the basic tab
@ -98,17 +87,13 @@ class FileCmds:
informs = [informGeneral] informs = [informGeneral]
count = self.tracksCount() if not self.trackCount["Video"] == 0:
if not count["Video"] == 0:
informs.append(informVideo) informs.append(informVideo)
count = self.tracksCount()
if not count["Audio"] == 0: if not self.trackCount["Audio"] == 0:
informs.append(informAudio) informs.append(informAudio)
count = self.tracksCount()
if not count["Text"] == 0: if not self.trackCount["Text"] == 0:
informs.append(informText) informs.append(informText)
output = "" output = ""
@ -116,7 +101,7 @@ class FileCmds:
for inform in informs: for inform in informs:
output += self.media_inform(inform) output += self.media_inform(inform)
if not count["Menu"] == 0: if not self.trackCount["Menu"] == 0:
output += "M: Menu\n\n" output += "M: Menu\n\n"
output += "\n" output += "\n"
@ -132,3 +117,66 @@ class FileCmds:
''' '''
output = self.execute_bash(f"mediainfo --Full {quote(self.filePath)}").decode('utf-8') output = self.execute_bash(f"mediainfo --Full {quote(self.filePath)}").decode('utf-8')
return output return output
def generate_tabs_list(self):
'''
Return the list of item for the QListWidget
:rtype: list
'''
tabsStr = ""
tabs = ["Basic", "Advanced"]
informGeneral = r"General;General (%Format%)\n"
informs = [informGeneral]
if self.trackCount["Video"] == 1:
informVideo = r'Video;Video (%Format%)\n'
informs.append(informVideo)
elif self.trackCount["Video"] > 1:
informVideo = r'Video;Video #%StreamKindPos% (%Format%)\n'
informs.append(informVideo)
if self.trackCount["Audio"] == 1:
informAudio = r'Audio;Audio (%Format%)\n'
informs.append(informAudio)
elif self.trackCount["Audio"] > 1:
informAudio = r'Audio;Audio #%StreamKindPos% (%Format%)\n'
informs.append(informAudio)
if self.trackCount["Text"] == 1:
informText = r'Text;Text (%Format%)\n'
informs.append(informText)
elif self.trackCount["Text"] > 1:
informText = r'Text;Text #%StreamKindPos% (%Format%)\n'
informs.append(informText)
for inform in informs:
tabsStr += self.media_inform(inform)
if not self.trackCount["Menu"] == 0:
tabsStr += "Menu"
tabs.extend(list(filter(None, tabsStr.split("\n"))))
return tabs
def generate_tabs_content(self):
'''
Return the content of the tabs
:rtype: dict[str:str]
'''
tracksList = self.advancedInfo.split("\n\n")
del tracksList[-1]
tabs = {}
tabs["Basic"] = self.basicInfo
tabs["Advanced"] = self.advancedInfo
for i in range(len(tracksList)):
tabs.update({self.tabsList[i+2]: tracksList[i]})
return tabs

View File

@ -1,19 +1,27 @@
from PySide6.QtWidgets import QWidget from PySide6.QtWidgets import QWidget
from ui_main_window import Ui_MainWindow from ui_main_window import Ui_MainWindow
from file_cmds import FileCmds from file_cmds import FileCmds
from PySide6.QtGui import QFont
class MainWindow(QWidget, Ui_MainWindow): class MainWindow(QWidget, Ui_MainWindow):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.setupUi(self) self.setupUi(self)
self.setWindowTitle("mediainfoSama") self.windowTitle = "mediainfoSama"
self.setWindowTitle(self.windowTitle)
self.setAcceptDrops(True) self.setAcceptDrops(True)
self.setupConnections() self.setupConnections()
#TEMP to remove
#Set font to Mononoki system wise
self.media_detail_plain_text_edit.setFont(QFont("Mononoki Nerd Font", 10))
def setupConnections(self): def setupConnections(self):
pass pass
def dragEnterEvent(self, event): def dragEnterEvent(self, event):
event.accept() event.accept()
@ -25,29 +33,16 @@ class MainWindow(QWidget, Ui_MainWindow):
def dropEvent(self, event): def dropEvent(self, event):
self.filePath = event.mimeData().urls()[0].toLocalFile() self.filePath = event.mimeData().urls()[0].toLocalFile()
self.fileCmds = FileCmds(self.filePath) self.fileCmds = FileCmds(self.filePath)
self.fileCmds.file_exist()
if self.fileCmds.file_exist() == False: if self.fileCmds.file_exist() == False:
return return
self.populate() self.setWindowTitle(f"{self.filePath} - {self.windowTitle}")
self.populate_tabs()
print("YEAH") def populate_tabs(self, path = ''):
# videoTrack = self.mkv_infos.get_video_track()
# if videoTrack == None:
# return
def populate(self, path = ''):
self.media_tracks_list_widget.clear() self.media_tracks_list_widget.clear()
informGeneral = r"General;G: %Format%, %FileSize_String%, %Duration_String%, %BitRate_String%\n" self.media_tracks_list_widget.addItems(self.fileCmds.tabsList)
informVideo = r"Video;V: %Format%, %Format_Profile%, %StreamSize_String%, %Width%x%Height%, %FrameRate% FPS, %BitRate_String%\n" text = self.fileCmds.tabsContent["Basic"]
informAudio = r"Audio;A: %Language_String%, %Format%, %StreamSize_String%, %BitRate_String%, %Channel(s)_String%, %SamplingRate_String%\n"
informText = r"Text;T: %Language_String%, %Format%\n"
text = self.fileCmds.generate_basic()
self.media_detail_plain_text_edit.setPlainText(text) self.media_detail_plain_text_edit.setPlainText(text)
print(self.fileCmds.tracksCount())