def execute(self): import os import sys import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc py3 = sys.version_info[0] >= 3 output = self.arg(1) if not self.arg(1): output = "output.pdf" # Create and edit the file list filenames = [f.relative_path for f in self.fm.thistab.get_selection()] listfile = tempfile.NamedTemporaryFile(delete=False) listpath = listfile.name if py3: listfile.write("\n".join(filenames).encode("utf-8")) else: listfile.write("\n".join(filenames)) listfile.close() self.fm.execute_file([File(listpath)], app='editor') listfile = open(listpath, 'r') # TODO check name and number consistance new_filenames = listfile.read().split("\n") listfile.close() os.unlink(listpath) listfilenames = " ".join((esc(f) for f in new_filenames)) # if all(a == b for a, b in zip(filenames, new_filenames)): # self.fm.notify("No renaming to be done!") # return # TODO check output name # Generate script cmdfile = tempfile.NamedTemporaryFile() script_lines = [] script_lines.append( "# This file will be executed when you close the editor.\n") script_lines.append( "# Please double-check everything, clear the file to abort.\n") script_lines.extend("pdfjam %s -o %s\n" % (listfilenames, esc(output))) script_content = "".join(script_lines) if py3: cmdfile.write(script_content.encode("utf-8")) else: cmdfile.write(script_content) cmdfile.flush() # Open the script and let the user review it, then check if the script # was modified by the user self.fm.execute_file([File(cmdfile.name)], app='editor') cmdfile.seek(0) script_was_edited = (script_content != cmdfile.read()) # # Do the renaming self.fm.run(['/bin/sh', cmdfile.name], flags='w') cmdfile.close()
def execute(self): import os import sys import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc py3 = sys.version_info[0] >= 3 output = self.arg(1) if not self.arg(1): output = "output.pdf" # Create and edit the file list filenames = [f.relative_path for f in self.fm.thistab.get_selection()] listfile = tempfile.NamedTemporaryFile(delete=False) listpath = listfile.name if py3: listfile.write("\n".join(filenames).encode("utf-8")) else: listfile.write("\n".join(filenames)) listfile.close() self.fm.execute_file([File(listpath)], app='editor') listfile = open(listpath, 'r') # TODO check name and number consistance new_filenames = listfile.read().split("\n") listfile.close() os.unlink(listpath) listfilenames = " ".join((esc(f) for f in new_filenames)) # if all(a == b for a, b in zip(filenames, new_filenames)): # self.fm.notify("No renaming to be done!") # return # TODO check output name # Generate script cmdfile = tempfile.NamedTemporaryFile() script_lines = [] script_lines.append("# This file will be executed when you close the editor.\n") script_lines.append("# Please double-check everything, clear the file to abort.\n") script_lines.extend("pdfjam %s -o %s\n" % (listfilenames, esc(output))) script_content = "".join(script_lines) if py3: cmdfile.write(script_content.encode("utf-8")) else: cmdfile.write(script_content) cmdfile.flush() # Open the script and let the user review it, then check if the script # was modified by the user self.fm.execute_file([File(cmdfile.name)], app='editor') cmdfile.seek(0) script_was_edited = (script_content != cmdfile.read()) # # Do the renaming self.fm.run(['/bin/sh', cmdfile.name], flags='w') cmdfile.close()
def execute(self): import sys import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc py3 = sys.version > "3" # Create and edit the file list filenames = [f.basename for f in self.fm.thistab.get_selection()] listfile = tempfile.NamedTemporaryFile() if py3: listfile.write("\n".join(filenames).encode("utf-8")) else: listfile.write("\n".join(filenames)) listfile.flush() self.fm.execute_file([File(listfile.name)], app="editor") listfile.seek(0) if py3: new_filenames = listfile.read().decode("utf-8").split("\n") else: new_filenames = listfile.read().split("\n") listfile.close() if all(a == b for a, b in zip(filenames, new_filenames)): self.fm.notify("No renaming to be done!") return # Generate and execute script cmdfile = tempfile.NamedTemporaryFile() cmdfile.write(b"# This file will be executed when you close the editor.\n") cmdfile.write(b"# Please double-check everything, clear the file to abort.\n") if py3: cmdfile.write( "\n".join( "mv -vi -- " + esc(old) + " " + esc(new) for old, new in zip(filenames, new_filenames) if old != new ).encode("utf-8") ) else: cmdfile.write( "\n".join( "mv -vi -- " + esc(old) + " " + esc(new) for old, new in zip(filenames, new_filenames) if old != new ) ) cmdfile.flush() self.fm.execute_file([File(cmdfile.name)], app="editor") self.fm.run(["/bin/sh", cmdfile.name], flags="w") cmdfile.close()
def execute(self): import sys import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc py3 = sys.version > "3" # Create and edit the file list filenames = [f.basename for f in self.fm.thistab.get_selection()] listfile = tempfile.NamedTemporaryFile(delete=False) listpath = listfile.name if py3: listfile.write("\n".join(filenames).encode("utf-8")) else: listfile.write("\n".join(filenames)) listfile.close() self.fm.execute_file([File(listpath)], app='editor') listfile = open(listpath, 'r') if py3: new_filenames = listfile.read().decode("utf-8").split("\n") else: new_filenames = listfile.read().split("\n") listfile.close() os.unlink(listpath) if all(a == b for a, b in zip(filenames, new_filenames)): self.fm.notify("No renaming to be done!") return # Generate and execute script cmdfile = tempfile.NamedTemporaryFile() cmdfile.write( b"# This file will be executed when you close the editor.\n") cmdfile.write( b"# Please double-check everything, clear the file to abort.\n") if py3: cmdfile.write("\n".join("mv -vi -- " + esc(old) + " " + esc(new) \ for old, new in zip(filenames, new_filenames) \ if old != new).encode("utf-8")) else: cmdfile.write("\n".join("mv -vi -- " + esc(old) + " " + esc(new) \ for old, new in zip(filenames, new_filenames) if old != new)) cmdfile.flush() self.fm.execute_file([File(cmdfile.name)], app='editor') self.fm.run(['/bin/sh', cmdfile.name], flags='w') cmdfile.close()
def execute(self): archive = self.rest(1) if not archive: self.fm.notify("Error: missing archive filename", bad=True) return cmd = self.command.format(esc(archive)) if '%' in cmd: cmd = self.fm.substitute_macros(cmd, escape=True) self.fm.execute_command(cmd)
def execute(self): import sys import tempfile from os.path import relpath from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc py3 = sys.version > "3" # Create and edit the file list filenames = [relpath(f.path, start=self.fm.thisdir.path) for f in self.fm.thistab.get_selection()] listfile = tempfile.NamedTemporaryFile(delete=False) listpath = listfile.name if py3: listfile.write("\n".join(filenames).encode("utf-8")) else: listfile.write("\n".join(filenames)) listfile.close() self.fm.execute_file([File(listpath)], app='editor') listfile = open(listpath, 'r') new_filenames = listfile.read().split("\n") listfile.close() os.unlink(listpath) if all(a == b for a, b in zip(filenames, new_filenames)): self.fm.notify("No renaming to be done!") return # Generate and execute script cmdfile = tempfile.NamedTemporaryFile() cmdfile.write(b"# This file will be executed when you close the editor.\n") cmdfile.write(b"# Please double-check everything, clear the file to abort.\n") if py3: cmdfile.write("\n".join("mv -vi -- " + esc(old) + " " + esc(new) \ for old, new in zip(filenames, new_filenames) \ if old != new).encode("utf-8")) else: cmdfile.write("\n".join("mv -vi -- " + esc(old) + " " + esc(new) \ for old, new in zip(filenames, new_filenames) if old != new)) cmdfile.flush() self.fm.execute_file([File(cmdfile.name)], app='editor') self.fm.run(['/bin/sh', cmdfile.name], flags='w') cmdfile.close()
def execute(self): import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc cmdfile = tempfile.NamedTemporaryFile() script_files = ["#!/bin/bash"] script_files.extend( self.rest(1).format(fn=esc(f.relative_path)) for f in self.fm.thistab.get_selection()) script_files.append("echo FINISHED; read") cmdfile.write("\n".join(script_files)) cmdfile.flush() self.fm.execute_file([File(cmdfile.name)], app='editor') self.fm.run(['/bin/sh', cmdfile.name], flags='w') cmdfile.close()
def execute(self): import sys import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc py3 = sys.version_info[0] >= 3 # Create and edit the file list filenames = [f.relative_path for f in self.fm.thistab.get_selection()] listfile = tempfile.NamedTemporaryFile(delete=False) listpath = listfile.name if py3: listfile.write("\n".join(filenames).encode("utf-8")) else: listfile.write("\n".join(filenames)) listfile.close() self.fm.execute_file([File(listpath)], app='editor') listfile = open(listpath, 'r') new_filenames = listfile.read().split("\n") listfile.close() os.unlink(listpath) if all(a == b for a, b in zip(filenames, new_filenames)): self.fm.notify("No renaming to be done!") return # Generate script cmdfile = tempfile.NamedTemporaryFile() script_lines = [] script_lines.append( "# This file will be executed when you close the editor.\n") script_lines.append( "# Please double-check everything, clear the file to abort.\n") script_lines.extend("mv -vi -- %s %s\n" % (esc(old), esc(new)) \ for old, new in zip(filenames, new_filenames) if old != new) script_content = "".join(script_lines) if py3: cmdfile.write(script_content.encode("utf-8")) else: cmdfile.write(script_content) cmdfile.flush() # Open the script and let the user review it, then check if the script # was modified by the user self.fm.execute_file([File(cmdfile.name)], app='editor') cmdfile.seek(0) script_was_edited = (script_content != cmdfile.read()) # Do the renaming self.fm.run(['/bin/sh', cmdfile.name], flags='w') cmdfile.close() # Retag the files, but only if the script wasn't changed during review, # because only then we know which are the source and destination files. if not script_was_edited: tags_changed = False for old, new in zip(filenames, new_filenames): if old != new: oldpath = self.fm.thisdir.path + '/' + old newpath = self.fm.thisdir.path + '/' + new if oldpath in self.fm.tags: old_tag = self.fm.tags.tags[oldpath] self.fm.tags.remove(oldpath) self.fm.tags.tags[newpath] = old_tag tags_changed = True if tags_changed: self.fm.tags.dump() else: fm.notify("files have not been retagged")
def execute(self): # pylint: disable=too-many-locals,too-many-statements import sys import tempfile from ranger.container.file import File from ranger.ext.shell_escape import shell_escape as esc py3 = sys.version_info[0] >= 3 # Create and edit the file list filenames = [f.relative_path for f in self.fm.thistab.get_selection()] listfile = tempfile.NamedTemporaryFile(delete=False) listpath = listfile.name if py3: listfile.write("\n".join(filenames).encode("utf-8")) else: listfile.write("\n".join(filenames)) listfile.close() self.fm.execute_file([File(listpath)], app='editor') listfile = open(listpath, 'r') new_filenames = listfile.read().split("\n") listfile.close() os.unlink(listpath) if all(a == b for a, b in zip(filenames, new_filenames)): self.fm.notify("No renaming to be done!") return # Generate script cmdfile = tempfile.NamedTemporaryFile() script_lines = [] script_lines.append("# This file will be executed when you close the editor.\n") script_lines.append("# Please double-check everything, clear the file to abort.\n") script_lines.extend("mv -vi -- %s %s\n" % (esc(old), esc(new)) for old, new in zip(filenames, new_filenames) if old != new) script_content = "".join(script_lines) if py3: cmdfile.write(script_content.encode("utf-8")) else: cmdfile.write(script_content) cmdfile.flush() # Open the script and let the user review it, then check if the script # was modified by the user self.fm.execute_file([File(cmdfile.name)], app='editor') cmdfile.seek(0) script_was_edited = (script_content != cmdfile.read()) # Do the renaming self.fm.run(['/bin/sh', cmdfile.name], flags='w') cmdfile.close() # Retag the files, but only if the script wasn't changed during review, # because only then we know which are the source and destination files. if not script_was_edited: tags_changed = False for old, new in zip(filenames, new_filenames): if old != new: oldpath = self.fm.thisdir.path + '/' + old newpath = self.fm.thisdir.path + '/' + new if oldpath in self.fm.tags: old_tag = self.fm.tags.tags[oldpath] self.fm.tags.remove(oldpath) self.fm.tags.tags[newpath] = old_tag tags_changed = True if tags_changed: self.fm.tags.dump() else: fm.notify("files have not been retagged")
def execute(self): from ranger.container.file import File # pylint: disable=import-outside-toplevel # pylint: disable=import-outside-toplevel from ranger.ext.shell_escape import shell_escape as esc editor = os.getenv('EDITOR') if not editor: editor = 'nvim' # Create and edit the file list filenames = [f.relative_path for f in self.fm.thistab.get_selection()] with tempfile.NamedTemporaryFile(delete=False) as listfile: listpath = listfile.name listfile.write('\n'.join(filenames).encode( encoding='utf-8', errors='surrogateescape')) self.fm.execute_file([File(listpath)], app='{}'.format(editor)) with open(listpath, 'r', encoding='utf-8', errors='surrogateescape') as listfile: new_filenames = listfile.read().split("\n") os.unlink(listpath) if all(a == b for a, b in zip(filenames, new_filenames)): self.fm.notify('No renaming to be done!') return # Generate script with tempfile.NamedTemporaryFile() as cmdfile: script_lines = [] script_lines.append( '# This file will be executed when you close the editor.') script_lines.append( '# Please double-check everything, clear the file to abort.') new_dirs = [] for old, new in zip(filenames, new_filenames): if old != new: basepath, _ = os.path.split(new) if (basepath and basepath not in new_dirs and not os.path.isdir(basepath)): script_lines.append( 'mkdir -vp -- {dir}'.format(dir=esc(basepath))) new_dirs.append(basepath) script_lines.append('mv -vi -- {old} {new}'.format( old=esc(old), new=esc(new))) # Make sure not to forget the ending newline script_content = '\n'.join(script_lines) + '\n' cmdfile.write( script_content.encode(encoding='utf-8', errors='surrogateescape')) cmdfile.flush() # Open the script and let the user review it, then check if the # script was modified by the user self.fm.execute_file([File(cmdfile.name)], app='{}'.format(editor)) cmdfile.seek(0) new_content = cmdfile.read() script_was_edited = (script_content != new_content) # Do the renaming self.fm.run(['/bin/sh', cmdfile.name], flags='w') _parse_cmd_and_move_buf(new_content) # Retag the files, but only if the script wasn't changed during review, # because only then we know which are the source and destination files. if not script_was_edited: tags_changed = False for old, new in zip(filenames, new_filenames): if old != new: oldpath = self.fm.thisdir.path + '/' + old newpath = self.fm.thisdir.path + '/' + new if oldpath in self.fm.tags: old_tag = self.fm.tags.tags[oldpath] self.fm.tags.remove(oldpath) self.fm.tags.tags[newpath] = old_tag tags_changed = True if tags_changed: self.fm.tags.dump() else: self.fm.notify('files have not been retagged')