class BusyBar(QThread): """ Adapted from: http://stackoverflow.com/questions/8007602/ looping-qprogressbar-gives-error-qobjectinstalleventfilter-cannot-filter-e Looping progress bar create the signal that the thread will emit .. note:: This function creates a busy bar but I have not figured out how to \ attach it to a process. Therefore, it is currently functionally \ useless. """ changeValue = pyqtSignal(int) def __init__(self, text=""): QThread.__init__(self, parent=None) self.text = text self.stop = False self.proBar = QProgressBar() self.proBar.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.SplashScreen) self.proBar.setRange(0, 100) self.proBar.setTextVisible(True) self.proBar.setFormat(self.text) self.proBar.setValue(0) self.proBar.setFixedSize(300, 40) self.proBar.setAlignment(Qt.AlignCenter) self.proBar.show() #self.changeValue.connect(self.proBar.setValue, Qt.QueuedConnection) # Make the Busybar delete itself and the QProgressBar when done self.finished.connect(self.onFinished) def run(self): """ """ while not self.stop: # keep looping while self is visible # Loop sending mail for i in range(100): # emit the signal instead of calling setValue # also we can't read the progress bar value from the thread self.changeValue.emit(i) time.sleep(0.01) self.changeValue.emit(0) def onFinished(self): """ """ self.proBar.deleteLater() self.deleteLater() def Kill(self): """ """ self.stop = True
class BusyBar(QThread): """ Adapted from: http://stackoverflow.com/questions/8007602/ looping-qprogressbar-gives-error-qobjectinstalleventfilter-cannot-filter-e Looping progress bar create the signal that the thread will emit .. note:: This function creates a busy bar but I have not figured out how to \ attach it to a process. Therefore, it is currently functionally \ useless. """ changeValue = pyqtSignal(int) def __init__(self, text = "" ): QThread.__init__(self, parent = None) self.text = text self.stop = False self.proBar = QProgressBar() self.proBar.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.SplashScreen ) self.proBar.setRange( 0, 100 ) self.proBar.setTextVisible( True ) self.proBar.setFormat( self.text ) self.proBar.setValue( 0 ) self.proBar.setFixedSize( 300 , 40 ) self.proBar.setAlignment(Qt.AlignCenter) self.proBar.show() #self.changeValue.connect(self.proBar.setValue, Qt.QueuedConnection) # Make the Busybar delete itself and the QProgressBar when done self.finished.connect(self.onFinished) def run(self): """ """ while not self.stop: # keep looping while self is visible # Loop sending mail for i in range(100): # emit the signal instead of calling setValue # also we can't read the progress bar value from the thread self.changeValue.emit( i ) time.sleep(0.01) self.changeValue.emit( 0 ) def onFinished(self): """ """ self.proBar.deleteLater() self.deleteLater() def Kill(self): """ """ self.stop = True
class FileItem(QTreeWidgetItem): cols = ['Filepath', 'Renamed Filepath'] invalid_char_map = {':': ''} re_filename_episodes = [ re.compile( r'.*[Ss](?P<season>\d{1,2})[\s\-_]*[Ee](?P<episode>\d{1,2}).*', re.I), re.compile( r'.*season\s*(?P<season>\d{1,2})[\s\-_]*episode\s*[Ee](?P<episode>\d{1,2}).*', re.I), ] re_filepath_episodes = [ re.compile( r'.*season\s*(?P<season>\d{1,2})\\(?:season\s*\d{1,2}[\s\-_]*)?(?:episode|ep\.?|e)\s*(?P<episode>\d{1,2}).*', re.I), ] def __init__(self, parent, filepath, mediaman): super(FileItem, self).__init__(parent) self._renamed = False self.mediaman = mediaman self.filepath = filepath self.renamed_filepath = None self.media_info = None self.episode_info = None self.thread = None self.pbar = None self.setText(0, self.filepath) self.send_thread() def send_thread(self): if self.thread is None: self.thread = FilepathSearchThread(self.treeWidget()) self.thread.finished.connect(self.receive_thread) thread_ready = self.thread.wait(10000) if thread_ready: tree = self.treeWidget() self.pbar = QProgressBar(tree) self.pbar.setRange(0, 0) tree.setItemWidget(self, 1, self.pbar) self.thread.filepath = self.filepath self.thread.start() def receive_thread(self): self.media_info = self.thread.result self.pbar.reset() tree = self.treeWidget() tree.removeItemWidget(self, 1) self.pbar.deleteLater() self.refresh() def _replace_invalid_chars(self, text): for k, v in FileItem.invalid_char_map.items(): if k in text: text = text.replace(k, v) return text def refresh(self): moviedir = self.mediaman.moviedir() tvdir = self.mediaman.tvdir() if self.media_info: title = self.media_info.get('title') title = self._replace_invalid_chars(title) year = self.media_info.get('year') filename, ext = os.path.splitext(os.path.basename(self.filepath)) if self.episode_info or self.media_info.get('episodes'): if not self.episode_info: episodes = self.media_info['episodes'] ep_match = None for reg in FileItem.re_filename_episodes: m = reg.search(filename) if m: ep_match = m.groupdict() break if not ep_match: for reg in FileItem.re_filepath_episodes: m = reg.search(self.filepath) if m: ep_match = m.groupdict() if ep_match: season, episode = int(ep_match['season']), int( ep_match['episode']) for episode_info in episodes: if (episode_info['season'], episode_info['episode']) == (season, episode): self.episode_info = episode_info break if self.episode_info: self.renamed_filepath = ur'{tvdir}\{title}\Season{season:02d}\{title}.S{season:02d}E{episode:02d}.{episode_title}{ext}'.format( tvdir=tvdir, title=title, year=year, season=self.episode_info['season'], episode=self.episode_info['episode'], episode_title=self._replace_invalid_chars( self.episode_info['title']), ext=ext, ) else: self.renamed_filepath = ur'{tvdir}\{title}\Season00\{title}.S00E00.XXX{ext}'.format( tvdir=tvdir, title=title, year=year, ext=ext, ) else: self.renamed_filepath = ur'{moviedir}\{title} ({year})\{title}{ext}'.format( moviedir=moviedir, title=title, year=year, ext=ext) self.setText(1, self.renamed_filepath) if os.path.exists(self.renamed_filepath): self.setBackgroundColor( FileItem.cols.index('Renamed Filepath'), QColor(0, 255, 0, 100))
class FileItem(QTreeWidgetItem): cols = ['Filepath', 'Renamed Filepath'] invalid_char_map = {':': ''} re_filename_episodes = [ re.compile(r'.*[Ss](?P<season>\d{1,2})[\s\-_]*[Ee](?P<episode>\d{1,2}).*', re.I), re.compile(r'.*season\s*(?P<season>\d{1,2})[\s\-_]*episode\s*[Ee](?P<episode>\d{1,2}).*', re.I), ] re_filepath_episodes = [ re.compile(r'.*season\s*(?P<season>\d{1,2})\\(?:season\s*\d{1,2}[\s\-_]*)?(?:episode|ep\.?|e)\s*(?P<episode>\d{1,2}).*', re.I), ] def __init__(self, parent, filepath, mediaman): super(FileItem, self).__init__(parent) self._renamed = False self.mediaman = mediaman self.filepath = filepath self.renamed_filepath = None self.media_info = None self.episode_info = None self.thread = None self.pbar = None self.setText(0, self.filepath) self.send_thread() def send_thread(self): if self.thread is None: self.thread = FilepathSearchThread(self.treeWidget()) self.thread.finished.connect(self.receive_thread) thread_ready = self.thread.wait(10000) if thread_ready: tree = self.treeWidget() self.pbar = QProgressBar(tree) self.pbar.setRange(0, 0) tree.setItemWidget(self, 1, self.pbar) self.thread.filepath = self.filepath self.thread.start() def receive_thread(self): self.media_info = self.thread.result self.pbar.reset() tree = self.treeWidget() tree.removeItemWidget(self, 1) self.pbar.deleteLater() self.refresh() def _replace_invalid_chars(self, text): for k, v in FileItem.invalid_char_map.items(): if k in text: text = text.replace(k, v) return text def refresh(self): moviedir = self.mediaman.moviedir() tvdir = self.mediaman.tvdir() if self.media_info: title = self.media_info.get('title') title = self._replace_invalid_chars(title) year = self.media_info.get('year') filename, ext = os.path.splitext(os.path.basename(self.filepath)) if self.episode_info or self.media_info.get('episodes'): if not self.episode_info: episodes = self.media_info['episodes'] ep_match = None for reg in FileItem.re_filename_episodes: m = reg.search(filename) if m: ep_match = m.groupdict() break if not ep_match: for reg in FileItem.re_filepath_episodes: m = reg.search(self.filepath) if m: ep_match = m.groupdict() if ep_match: season, episode = int(ep_match['season']), int(ep_match['episode']) for episode_info in episodes: if (episode_info['season'], episode_info['episode']) == (season, episode): self.episode_info = episode_info break if self.episode_info: self.renamed_filepath = ur'{tvdir}\{title}\Season{season:02d}\{title}.S{season:02d}E{episode:02d}.{episode_title}{ext}'.format( tvdir=tvdir, title=title, year=year, season=self.episode_info['season'] , episode=self.episode_info['episode'], episode_title=self._replace_invalid_chars(self.episode_info['title']), ext=ext, ) else: self.renamed_filepath = ur'{tvdir}\{title}\Season00\{title}.S00E00.XXX{ext}'.format( tvdir=tvdir, title=title, year=year, ext=ext, ) else: self.renamed_filepath = ur'{moviedir}\{title} ({year})\{title}{ext}'.format( moviedir=moviedir, title=title, year=year, ext=ext ) self.setText(1, self.renamed_filepath) if os.path.exists(self.renamed_filepath): self.setBackgroundColor(FileItem.cols.index('Renamed Filepath'), QColor(0, 255, 0, 100))