def open_files(self, path_hash_dictionary): # file_paths is expected to be a dictionary # This allows for threading file opening # Which should speed up multiple file opening # especially @ application start file_paths = [i for i in path_hash_dictionary] for filename in path_hash_dictionary.items(): file_md5 = filename[1] if not file_md5: try: with open(filename[0], 'rb') as current_book: first_bytes = current_book.read( 1024 * 32) # First 32KB of the file file_md5 = hashlib.md5(first_bytes).hexdigest() except FileNotFoundError: return # Remove any already open files # Set focus to last file in case only one is open for i in range(1, self.tabWidget.count()): tab_metadata = self.tabWidget.widget(i).metadata if tab_metadata['hash'] == file_md5: file_paths.remove(filename[0]) if not file_paths: self.tabWidget.setCurrentIndex(i) return if not file_paths: return def finishing_touches(): self.profile_functions.format_contentView() self.start_culling_timer() print('Attempting to open: ' + ', '.join(file_paths)) contents = sorter.BookSorter(file_paths, ('reading', None), self.database_path, True, self.temp_dir.path()).initiate_threads() # TODO # Notification feedback in case all books return nothing if not contents: return for i in contents: # New tabs are created here # Initial position adjustment is carried out by the tab itself file_data = contents[i] Tab(file_data, self) if self.settings['last_open_tab'] == 'library': self.tabWidget.setCurrentIndex(0) self.listView.setFocus() self.settings['last_open_tab'] = None return for i in range(1, self.tabWidget.count()): this_path = self.tabWidget.widget(i).metadata['path'] if self.settings['last_open_tab'] == this_path: self.tabWidget.setCurrentIndex(i) self.settings['last_open_tab'] = None finishing_touches() return self.tabWidget.setCurrentIndex(self.tabWidget.count() - 1) finishing_touches()
def open_files(self, path_hash_dictionary): # file_paths is expected to be a dictionary # This allows for threading file opening # Which should speed up multiple file opening # especially @ application start file_paths = [i for i in path_hash_dictionary] for filename in path_hash_dictionary.items(): file_md5 = filename[1] if not file_md5: try: with open(filename[0], 'rb') as current_book: first_bytes = current_book.read( 1024 * 32) # First 32KB of the file file_md5 = hashlib.md5(first_bytes).hexdigest() except FileNotFoundError: return # Remove any already open files # Set focus to last file in case only one is open for i in range(1, self.tabWidget.count()): tab_metadata = self.tabWidget.widget(i).metadata if tab_metadata['hash'] == file_md5: file_paths.remove(filename[0]) if not file_paths: self.tabWidget.setCurrentIndex(i) return if not file_paths: return logger.info('Attempting to open: ' + ', '.join(file_paths)) contents, errors = sorter.BookSorter( file_paths, ('reading', None), self.database_path, self.settings, self.temp_dir.path()).initiate_threads() if errors: self.display_error_notification(errors) if not contents: logger.error('No parseable files found') return successfully_opened = [] for i in contents: # New tabs are created here # Initial position adjustment is carried out by the tab itself file_data = contents[i] Tab(file_data, self) successfully_opened.append(file_data['path']) logger.info('Successfully opened: ' + ', '.join(file_paths)) if self.settings['last_open_tab'] == 'library': self.tabWidget.setCurrentIndex(0) self.listView.setFocus() self.settings['last_open_tab'] = None return for i in range(1, self.tabWidget.count()): this_path = self.tabWidget.widget(i).metadata['path'] if self.settings['last_open_tab'] == this_path: self.tabWidget.setCurrentIndex(i) self.settings['last_open_tab'] = None return self.tabWidget.setCurrentIndex(self.tabWidget.count() - 1)