def _set_initial(self): self.clear_all() app.recursiveFolderOn = False utils.set_busy(True) self.enable_panel_widget('selectNone', False) main.menuPicker.getNoneMenu.Enable(False) main.bottomWindow.go.Enable(False)
def __init__(self, mainWindow, configFilePath): global main main = mainWindow self.loadError = False utils.set_busy(True) main.set_status_msg(_(u"Loading configuration ..."), u'wait') self.__load_file(configFilePath) utils.set_busy(False)
def rename(self, event): """ Write undo files first (safety first !), then attemp to perform the renaming operation. """ itemsRenamed = 0 error = False main.currentItem = None main.bottomWindow.display.mode = 'rename' utils.set_busy(True) main.picker.view.path.SetEditable(False) # adjust and sort items when recursively renaming folders if app.recursiveFolderOn and event != u'undo': main.set_status_msg( _(u"Adjusting %s recursive paths, please wait ...") % len(main.toRename), u'wait') progressDialog = classes.ProgressDialog( main, app.prefs, main.items, _(u"Adjusting %%% recursive paths, please wait ...")) if app.showTimes: t = time.time() def sort_test(n): return -n[1][0].count(os.sep) main.toRename.sort(key=sort_test) progressDialog.destroy() if app.showTimes: print("%s items recursive adjust : %s" % (len(main.toRename), (time.time() - t))) if not error: main.set_status_msg(_(u"Renaming in progress, please wait ..."), u'wait') # open undo files for writing try: self.originalFile = codecs.open( utils.get_user_path(u'undo/original.bak'), 'w', "utf-8") self.renamedFile = codecs.open( utils.get_user_path(u'undo/renamed.bak'), 'w', "utf-8") except IOError as (n, strerror): msg = strerror + _( u"\nMake sure 'undo' directory exists and is read/write\n\nYou will not be able to undo!!\nDo you want to continue??" ) title = _(u"Problem with undo!") if not utils.make_yesno_dlg(msg, title): error = 'cancelled' dlg.Destroy()
def undo_last_rename(self, event): """ Grabs names from .bak files and reverts them to their originals. Keep this separate from csv file functions for safety and stability. """ original = [] renamed = [] utils.set_busy(True) try: originalFile = codecs.open( utils.get_user_path(u'undo/original.bak'), 'r', 'utf-8') renamedFile = codecs.open(utils.get_user_path(u'undo/renamed.bak'), 'r', 'utf-8') for line in originalFile: original.append([line.strip(), False]) for line in renamedFile: renamed.append([line.strip(), 1]) except IOError as error: utils.make_err_msg(_(u"%s\n\nUndo Failed !!") % error, _(u"Error")) pass else: def get_name(x): return x[0] commonPrefix = os.path.commonprefix(map(get_name, original)).rstrip(os.sep) if os.path.exists(commonPrefix): main.picker.view.path.SetValue(commonPrefix) main.picker.view.dirPicker.SetPath(commonPrefix) if not len(original) == 0: main.toRename = zip( renamed, original) #reverse order from original rename! main.currentItem = None main.display_results() main.rename_items(u'undo') main.menuFile.SaveLog.Enable(False) else: main.set_status_msg(_(u"Nothing to undo"), u'eyes') main.bottomWindow.set_undo_redo_type('check') utils.set_busy(False)
def rename(self, event): """ Write undo files first (safety first !), then attemp to perform the renaming operation. """ itemsRenamed = 0 error = False main.currentItem = None main.bottomWindow.display.mode = 'rename' utils.set_busy(True) main.picker.view.path.SetEditable(False) # adjust and sort items when recursively renaming folders if app.recursiveFolderOn and event != u'undo': main.set_status_msg(_(u"Adjusting %s recursive paths, please wait ...") % len(main.toRename), u'wait') progressDialog = classes.ProgressDialog(main, app.prefs, main.items, _(u"Adjusting %%% recursive paths, please wait ...")) if app.showTimes: t = time.time() def sort_test(n): return -n[1][0].count(os.sep) main.toRename.sort(key=sort_test) progressDialog.destroy() if app.showTimes: print("%s items recursive adjust : %s" % (len(main.toRename), (time.time() - t))) if not error: main.set_status_msg(_(u"Renaming in progress, please wait ..."), u'wait') # open undo files for writing try: self.originalFile = codecs.open(utils.get_user_path(u'undo/original.bak'), 'w', "utf-8") self.renamedFile = codecs.open(utils.get_user_path(u'undo/renamed.bak'), 'w', "utf-8") except IOError as (n, strerror): msg = strerror + _(u"\nMake sure 'undo' directory exists and is read/write\n\nYou will not be able to undo!!\nDo you want to continue??") title = _(u"Problem with undo!") if not utils.make_yesno_dlg(msg, title): error = 'cancelled' dlg.Destroy()
def undo_last_rename(self, event): """ Grabs names from .bak files and reverts them to their originals. Keep this separate from csv file functions for safety and stability. """ original = [] renamed = [] utils.set_busy(True) try: originalFile = codecs.open(utils.get_user_path(u'undo/original.bak'), 'r', 'utf-8') renamedFile = codecs.open(utils.get_user_path(u'undo/renamed.bak'), 'r', 'utf-8') for line in originalFile: original.append([line.strip(), False]) for line in renamedFile: renamed.append([line.strip(), 1]) except IOError as error: utils.make_err_msg(_(u"%s\n\nUndo Failed !!") % error, _(u"Error")) pass else: def get_name(x): return x[0] commonPrefix = os.path.commonprefix(map(get_name, original)).rstrip(os.sep) if os.path.exists(commonPrefix): main.picker.view.path.SetValue(commonPrefix) main.picker.view.dirPicker.SetPath(commonPrefix) if not len(original) == 0: main.toRename = zip(renamed, original)#reverse order from original rename! main.currentItem = None main.display_results() main.rename_items(u'undo') main.menuFile.SaveLog.Enable(False) else: main.set_status_msg(_(u"Nothing to undo"), u'eyes') main.bottomWindow.set_undo_redo_type('check') utils.set_busy(False)
error, itemsRenamed = self.__rename_item_list(event) # end of operations if not error: main.set_status_msg(_(u"Renaming for %s items completed") % itemsRenamed, u"complete") if app.prefs.get('reloadAfterRename') and event != u'undo': main.picker.refresh(event) else: main.picker.clear_all() if app.prefs.get(u'alwaysMakeLog'): main.save_items_as_text(False) elif error == 'cancelled': main.set_status_msg(_(u"Renaming cancelled"), u'failed') utils.set_busy(False) self._set_display(len(main.toRename)-1) if app.showTimes: print("%s items renamed : %s" % (itemsRenamed, (time.time() - t))) # auto if app.autoModeLevel > 2 and not error: print(_(u"Renaming for %s items completed") % itemsRenamed) sys.exit() main.picker.refresh_dir_tree() main.bottomWindow.go.Enable(False) main.menuFile.GoMenu.Enable(False) if event != 'undo':
def refresh(self, event): """ Grab items from a specified directory, either as a listing or as a walk, and filter out entries based on user settings. Files and folders are seperated for proper sorting. Called, depending on user preferences, from almost every widget in the picker panel, and from the main application class. """ files = [] #files will go here folders = [] #folders will go here error = False params = self.params.load() if params.root is False: self.clear_all() # OK, load items up: else: self._set_initial() main.set_status_msg(_(u"Getting directory contents please wait ..."), u'wait') if app.showTimes: t = time.time() filter, useRE = self._get_filter(params) # create the search (filtering) operations ... notType = params.notType # normal filtering: if filter and not useRE: def _filter_folders(entry): if filter in entry.lower() and not notType: folders.append(entry) if filter not in entry.lower() and notType: folders.append(entry) def _filter_files(entry): if filter in entry.lower() and not notType: files.append(entry) if filter not in entry.lower() and notType: files.append(entry) # regular expression filtering elif filter and useRE: def _filter_folders(entry): if filter.search(entry) and not notType: folders.append(entry) if not filter.search(entry) and notType: folders.append(entry) def _filter_files(entry): if filter.search(entry) and not notType: files.append(entry) if not filter.search(entry) and notType: files.append(entry) # no filtering: else: def _filter_folders(entry): folders.append(entry) def _filter_files(entry): files.append(entry) # define here for speed boost def isdir(entry): join = os.path.join(params.root, entry) return os.path.isdir(join) def get_encoded_name(filename): filename = filename.decode(sys.getfilesystemencoding(), 'replace') return filename # Now to get the items according to operations defined above # retrieve items by walking if params.walkIt: maxDepth = params.walkDepth if params.foldersOn: app.recursiveFolderOn = True try: for dirpath, dirnames, filenames in os.walk(params.root): base = dirpath.replace(params.root, '') if maxDepth != 0 and len(base.split(os.path.sep)) > maxDepth: continue # grab files if params.filesOn: for entry in filenames: entry = os.path.join(base, entry) _filter_files(entry) # grab folders if params.foldersOn: for entry in dirnames: entry = os.path.join(base, entry) _filter_folders(entry) except UnicodeDecodeError as err: entry = err[1].decode(sys.getfilesystemencoding(), 'replace') msg = _("The item '%s' has an encoding error.\nUnable to process this path in recursive mode, please correct the name and try again.")\ % entry utils.make_err_msg(msg, _("Unable to load item")) error = True pass except: main.set_status_msg(_(u"Cannot read path!"), u'warn') error = True pass else: main.set_status_msg(_(u"Retrieved %s items from directory") % len(files), u'wait') # normal retrieval else: encodingError = False try: listedDir = os.listdir(params.root) except: main.set_status_msg(_(u"Cannot read path!"), u'warn') error = True pass else: # loop through items in directory for entry in listedDir: try: isFolder = isdir(entry) except UnicodeDecodeError: entry = entry.decode(sys.getfilesystemencoding(), 'replace') isFolder = isdir(entry) encodingError = True # load folders if set: if params.foldersOn and isFolder: _filter_folders(entry) # load files if set: if params.filesOn and not isFolder: _filter_files(entry) if encodingError: utils.make_err_msg(_("At least one item has an encoding error in its name. You will not be able to modify these."), _("Encoding Error")) if error is not True: self.add_items_to_panel(folders, files) main.set_status_msg(_(u"Retrieved %s items from directory") % self.count_panel_items(), u'complete') # after retrieval: self.enable_panel_widget('selectAll', True) main.menuPicker.getAllMenu.Enable(True) main.bottomWindow.display.DeleteAllItems() utils.set_busy(False) main.Refresh() # output time taken if set if app.showTimes: print("%s items load : %s" % (self.count_panel_items(), (time.time() - t))) if app.prefs.get(u'autoSelectAll'): self.select_all()
def on_preview_button(self, event): utils.set_busy(True) self.renamer.preview(event) utils.set_busy(False)