From 227316cc84df5fd7a10a04b3c9da925f4fc905ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Lachheb?= Date: Wed, 12 Jun 2019 23:27:11 +0200 Subject: [PATCH] add verrou on view, code to show mbz tags as they are downloaded --- src/Crawler_dir.py | 81 +++++++++++++-------------- src/Crawler_modif.py | 50 ++++++++--------- src/data_crawler.py | 34 +++++++----- src/model.py | 79 +++++++++++++++++++++++++- src/view.py | 129 +++++++++++++++++++++++-------------------- 5 files changed, 227 insertions(+), 146 deletions(-) diff --git a/src/Crawler_dir.py b/src/Crawler_dir.py index 3de576b..e07feb0 100644 --- a/src/Crawler_dir.py +++ b/src/Crawler_dir.py @@ -1,9 +1,8 @@ -from threading import Thread, RLock +from threading import Thread from .data_crawler import Data_Crawler from .model import Model import time -verrou = RLock() class Crawler_Dir(Thread): @@ -17,43 +16,41 @@ def __init__(self, directory, store): def run(self): """Code à exécuter pendant l'exécution du thread.""" - with verrou : - - filelist = self.data_crawler.get_filelist(self.directory) - - filelist1 = [] - filelist2 = [] - filelist3 = [] - filelist4 = [] - - i= 1 - for filen in filelist : - if i == 1: - filelist1.append(filen) - i = 2 - elif i == 2: - filelist2.append(filen) - i = 3 - elif i == 3 : - filelist3.append(filen) - i = 4 - elif i == 4 : - filelist4.append(filen) - i = 1 - - thread_mbz1 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist1,self.directory)) #Writing data - thread_mbz2 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist2,self.directory)) - thread_mbz3 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist3,self.directory)) - thread_mbz4 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist4,self.directory)) - - - thread_mbz1.start() - thread_mbz2.start() - thread_mbz3.start() - thread_mbz4.start() - - - thread_mbz1.join() - thread_mbz2.join() - thread_mbz3.join() - thread_mbz4.join() + filelist = self.data_crawler.get_filelist(self.directory) + + filelist1 = [] + filelist2 = [] + filelist3 = [] + filelist4 = [] + + i= 1 + for filen in filelist : + if i == 1: + filelist1.append(filen) + i = 2 + elif i == 2: + filelist2.append(filen) + i = 3 + elif i == 3 : + filelist3.append(filen) + i = 4 + elif i == 4 : + filelist4.append(filen) + i = 1 + + thread_mbz1 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist1,self.directory)) #Writing data + thread_mbz2 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist2,self.directory)) + thread_mbz3 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist3,self.directory)) + thread_mbz4 = Thread(target = self.data_crawler.get_data_from_online, args=(filelist4,self.directory)) + + + thread_mbz1.start() + thread_mbz2.start() + thread_mbz3.start() + thread_mbz4.start() + + + thread_mbz1.join() + thread_mbz2.join() + thread_mbz3.join() + thread_mbz4.join() diff --git a/src/Crawler_modif.py b/src/Crawler_modif.py index 729fd23..2adcf5a 100644 --- a/src/Crawler_modif.py +++ b/src/Crawler_modif.py @@ -1,9 +1,8 @@ -from threading import Thread, RLock +from threading import Thread from .data_crawler import Data_Crawler from .model import Model from .treeview import TreeView -verrou = RLock() class Crawler_Modif(Thread): @@ -33,38 +32,37 @@ def __init__(self, modifs, store, selection, some_file): def run(self): - with verrou : - """Code à exécuter pendant l'exécution du thread.""" - if self.some_file == 1 : - print("modified some tags :") - model, listiter = self.selection.get_selected_rows() + """Code à exécuter pendant l'exécution du thread.""" + if self.some_file == 1 : + print("modified some tags :") + model, listiter = self.selection.get_selected_rows() - for i in range(len(listiter)): ## TODO - namefile = model[listiter[i]][0] - if namefile in self.modifs : - self.data_crawler.update_data_crawled([namefile],self.directory) + for i in range(len(listiter)): ## TODO + namefile = model[listiter[i]][0] + if namefile in self.modifs : + self.data_crawler.update_data_crawled([namefile],self.directory) - else : - self.data_crawler.update_data_crawled(self.modifs,self.directory) + else : + self.data_crawler.update_data_crawled(self.modifs,self.directory) - if(self.selectionequal(self.model.selection)): - model, listiter = self.model.selection.get_selected_rows() + if(self.selectionequal(self.model.selection)): + model, listiter = self.model.selection.get_selected_rows() - if len(listiter)> 1 : - multiple_line_selected = 1 - else : - multiple_line_selected = 0 + if len(listiter)> 1 : + multiple_line_selected = 1 + else : + multiple_line_selected = 0 - data_scrapped = self.data_crawler.get_tags(model, listiter, multiple_line_selected) - lyrics_scrapped = self.data_crawler.get_lyrics(model, listiter, multiple_line_selected) + data_scrapped = self.data_crawler.get_tags(model, listiter, multiple_line_selected) + lyrics_scrapped = self.data_crawler.get_lyrics(model, listiter, multiple_line_selected) - if(self.selectionequal(self.model.selection)): - self.model.view.show_mbz(data_scrapped) - self.model.view.show_lyrics(lyrics_scrapped) - else : - pass + if(self.selectionequal(self.model.selection)): + self.model.view.show_mbz(data_scrapped) + self.model.view.show_lyrics(lyrics_scrapped) + else : + pass diff --git a/src/data_crawler.py b/src/data_crawler.py index 1827bba..ec9b9db 100644 --- a/src/data_crawler.py +++ b/src/data_crawler.py @@ -1,9 +1,5 @@ from os import walk -from threading import RLock - -verrou = RLock() - import musicbrainzngs as mb from PyLyrics import * @@ -157,19 +153,27 @@ def get_lyrics(self,model,listiter, multiline_selected): else : return "Lyrics not avalaible" else : - return "File not crawled yet on lyrics.wikia" + return None def get_tags(self,model,listiter, multiline_selected): - namefile = model[listiter][0] - if namefile in self.tag_finder : - candidat = self.tag_finder[namefile].copy() - else : - return { "title":"", "artist":"", "album":"", "track":"", "year":"", "genre":"", "cover":""} + if multiline_selected == 0 : + + namefile = model[listiter][0] + if namefile in self.tag_finder : + return self.tag_finder[namefile].copy() + else : + return None + elif multiline_selected == 1 : - if multiline_selected == 1 : - for i in range(1,len(listiter)): + namefile = model[listiter][0] + if namefile in self.tag_finder : + candidat = self.tag_finder[namefile].copy() + else : + return None + + for i in range(1,len(listiter)): beta = model[listiter[i]][0] if beta in self.tag_finder : for tagi in ["artist","album","year","genre","cover"] : @@ -177,9 +181,10 @@ def get_tags(self,model,listiter, multiline_selected): candidat[tagi] = "" candidat["title"] = "" candidat["track"] = "" + else : + return None - - return candidat + return candidat def reorder_data(self,mzdata): ''' @@ -257,3 +262,4 @@ def getInstance(): if Data_Crawler.__instance == None: Data_Crawler() return Data_Crawler.__instance + diff --git a/src/model.py b/src/model.py index a638c0a..406164a 100644 --- a/src/model.py +++ b/src/model.py @@ -1,10 +1,12 @@ from os import walk +#from time import sleep +from threading import Thread from .moteur import Moteur from .view import View from .data_crawler import Data_Crawler from .treeview import TreeView -from gi.repository import Pango +#from gi.repository import Pango import os @@ -147,9 +149,79 @@ def update_view(self,selection): lyrics_scrapped = self.data_crawler.get_lyrics(model, listiter, multiple_line_selected) self.view.show_tags(self.tagdico, multiple_line_selected) - self.view.show_mbz(data_scrapped) - self.view.show_lyrics(lyrics_scrapped) + if data_scrapped == None : + self.view.show_mbz({ "title":"", "artist":"", "album":"", "track":"", "year":"", "genre":"", "cover":""}) + + lenselec = len(listiter) + fileselec = [] + + for i in range(len(listiter)): + namefile = model[listiter[i]][0] + fileselec.append(namefile) + + thread_waiting_mbz = Thread(target = self.wait_for_mbz, args=(model,listiter,lenselec,fileselec,multiple_line_selected)) + thread_waiting_mbz.start() + else : + self.view.show_mbz(data_scrapped) + + + if lyrics_scrapped == None : + self.view.show_lyrics("File not crawled yet on lyrics.wikia") + lenselec = len(listiter) + fileselec = [] + + for i in range(len(listiter)): + namefile = model[listiter[i]][0] + fileselec.append(namefile) + + #thread_waiting_lyr = Thread(target = self.wait_for_lyrics, args=(model,listiter,lenselec,fileselec,multiple_line_selected)) #Writing data + #self.wait_for_lyrics(model,listiter,multiple_line_selected) + #thread_waiting_lyr.start() + else : + self.view.show_lyrics(lyrics_scrapped) + + def wait_for_mbz(self,model,listiter,lenselec,fileselec,multiple_line_selected): + print("Entering thread waiting for mbz") + is_waiting_mbz = 1 + + while self.is_selectionequal(self.selection,lenselec,fileselec) and is_waiting_mbz == 1: + data_scrapped = self.data_crawler.get_tags(model, listiter, multiple_line_selected) + if data_scrapped != None and self.is_selectionequal(self.selection,lenselec,fileselec) : + print("mbz found :", data_scrapped["title"]) + is_waiting_mbz = 0 + self.view.show_mbz(data_scrapped) + + + + def wait_for_lyrics(self,model,listiter,lenselec,fileselec,multiple_line_selected): + print("Entering thread waiting for lyrics") + is_waiting_lyrics = 1 + + while self.is_selectionequal(self.selection,lenselec,fileselec) and is_waiting_lyrics == 1 : + lyrics_scrapped = self.data_crawler.get_lyrics(model, listiter, multiple_line_selected) + if lyrics_scrapped != None and self.is_selectionequal(self.selection,lenselec,fileselec) : + print("lyrics found :",lyrics_scrapped[0:10]) + is_waiting_lyrics = 0 + self.view.show_lyrics(lyrics_scrapped) + + + def is_selectionequal(self,selec, lenselec2,filelistselec2): + model, listiter = selec.get_selected_rows() + #print("la taille est :", len(listiter) ) + #print("la taille autorisée :",self.lenselection ) + + if len(listiter) == lenselec2 : + for i in range(len(listiter)): + namefile = model[listiter[i]][0] + if namefile not in filelistselec2: + #print("why element ? ",namefile) + return False + else : + #print("why size ?") + return False + + return True def rename_files(self): @@ -415,3 +487,4 @@ def getInstance(): if Model.__instance == None: Model() return Model.__instance + diff --git a/src/view.py b/src/view.py index 6f1e257..14dd173 100644 --- a/src/view.py +++ b/src/view.py @@ -3,9 +3,14 @@ from gi.repository import Gtk, GdkPixbuf, GLib from PIL import Image +from threading import RLock import io import math +verrou_tags = RLock() +verrou_mbz = RLock() +verrou_lyrics = RLock() + class View: @@ -48,49 +53,51 @@ def __init__(self, tree_view, title, album, artist, genre, cover, track, year, l def show_lyrics(self, lyrics_scrapped): - buf = self.lyrics.get_buffer() - buf.set_text(lyrics_scrapped) + with verrou_lyrics: + buf = self.lyrics.get_buffer() + buf.set_text(lyrics_scrapped) def show_mbz(self, data_scrapped): + with verrou_mbz : - # We show the tag currently in tagdico - self.title_mbz.set_text(data_scrapped["title"]) - self.track_mbz.set_text(data_scrapped["track"]) - self.genre_mbz.set_text(data_scrapped["genre"]) - self.album_mbz.set_text(data_scrapped["album"]) - self.artist_mbz.set_text(data_scrapped["artist"]) - self.year_mbz.set_text(data_scrapped["year"]) + # We show the tag currently in tagdico + self.title_mbz.set_text(data_scrapped["title"]) + self.track_mbz.set_text(data_scrapped["track"]) + self.genre_mbz.set_text(data_scrapped["genre"]) + self.album_mbz.set_text(data_scrapped["album"]) + self.artist_mbz.set_text(data_scrapped["artist"]) + self.year_mbz.set_text(data_scrapped["year"]) - if data_scrapped["cover"] != "" : - with Image.open(io.BytesIO(data_scrapped["cover"])) as img : - glibbytes = GLib.Bytes.new(img.tobytes()) + if data_scrapped["cover"] != "" : + with Image.open(io.BytesIO(data_scrapped["cover"])) as img : + glibbytes = GLib.Bytes.new(img.tobytes()) - width = img.width ##The best fix i could find for the moment - height = img.height + width = img.width ##The best fix i could find for the moment + height = img.height - if glibbytes.get_size() < width * height * 3 : - width = math.sqrt(glibbytes.get_size()/3) - height = math.sqrt(glibbytes.get_size()/3) + if glibbytes.get_size() < width * height * 3 : + width = math.sqrt(glibbytes.get_size()/3) + height = math.sqrt(glibbytes.get_size()/3) - try : - pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(glibbytes, # TODO ERROR HAPPENS WITH SOME COVER - GdkPixbuf.Colorspace.RGB, - False, - 8, - img.width, - img.height, - len(img.getbands())*img.width) + try : + pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(glibbytes, # TODO ERROR HAPPENS WITH SOME COVER + GdkPixbuf.Colorspace.RGB, + False, + 8, + img.width, + img.height, + len(img.getbands())*img.width) - pixbuf = pixbuf.scale_simple(250, 250, GdkPixbuf.InterpType.BILINEAR) + pixbuf = pixbuf.scale_simple(250, 250, GdkPixbuf.InterpType.BILINEAR) - self.cover_mbz.set_from_pixbuf(pixbuf) - except : - self.cover_mbz.set_from_icon_name('gtk-missing-image',6) - else : - self.cover_mbz.set_from_icon_name('gtk-missing-image',6) + self.cover_mbz.set_from_pixbuf(pixbuf) + except : + self.cover_mbz.set_from_icon_name('gtk-missing-image',6) + else : + self.cover_mbz.set_from_icon_name('gtk-missing-image',6) def erase(self): @@ -179,38 +186,38 @@ def show_cover_from_file(self,namefile): def show_tags(self,tagdico, multiple_rows): - - # We show those tags uniquely if there is only one row selected #TODO is it reallly usefull ? I don't think so - self.set_editibility_title(multiple_rows,tagdico["title"]["value"]) - self.set_editability_track(multiple_rows,tagdico["track"]["value"]) - - # Same thing for the labels # TODO show size and length for the concatenation of songs selectionned - self.set_size(multiple_rows,tagdico["size"]["value"]) - self.set_length(multiple_rows,tagdico["length"]["value"]) - - - # We show the tag currently in tagdico - self.genre.set_text(tagdico["genre"]["value"]) - self.album.set_text(tagdico["album"]["value"]) - self.artist.set_text(tagdico["artist"]["value"]) - self.year.set_text(tagdico["year"]["value"]) - #self.show_lyrics(tagdico["lyrics"]["value"]) #TODO : print tags lyrics in case of missing internet lyrics - - - if tagdico["cover"]["value"] != "": # A test to handle if there is a cover - if(tagdico["cover"]["value"] != self.last_cover): - if type(tagdico["cover"]["value"]) == bytes : # A test to detect bytes file - self.show_cover_from_bytes(tagdico["cover"]["value"]) - self.last_cover = tagdico["cover"]["value"] - else: - self.show_cover_from_file(tagdico["cover"]["value"]) - self.last_cover = tagdico["cover"]["value"] + with verrou_tags : + # We show those tags uniquely if there is only one row selected #TODO is it reallly usefull ? I don't think so + self.set_editibility_title(multiple_rows,tagdico["title"]["value"]) + self.set_editability_track(multiple_rows,tagdico["track"]["value"]) + + # Same thing for the labels # TODO show size and length for the concatenation of songs selectionned + self.set_size(multiple_rows,tagdico["size"]["value"]) + self.set_length(multiple_rows,tagdico["length"]["value"]) + + + # We show the tag currently in tagdico + self.genre.set_text(tagdico["genre"]["value"]) + self.album.set_text(tagdico["album"]["value"]) + self.artist.set_text(tagdico["artist"]["value"]) + self.year.set_text(tagdico["year"]["value"]) + #self.show_lyrics(tagdico["lyrics"]["value"]) #TODO : print tags lyrics in case of missing internet lyrics + + + if tagdico["cover"]["value"] != "": # A test to handle if there is a cover + if(tagdico["cover"]["value"] != self.last_cover): + if type(tagdico["cover"]["value"]) == bytes : # A test to detect bytes file + self.show_cover_from_bytes(tagdico["cover"]["value"]) + self.last_cover = tagdico["cover"]["value"] + else: + self.show_cover_from_file(tagdico["cover"]["value"]) + self.last_cover = tagdico["cover"]["value"] + else : + pass else : - pass - else : - self.cover.set_from_icon_name('gtk-missing-image',6) - self.last_cover = "" + self.cover.set_from_icon_name('gtk-missing-image',6) + self.last_cover = ""