Beispiel #1
0
    def execute(self):
        """ Compress copied files to current directory """
        copied_files = tuple(self.fm.copy_buffer)

        if not copied_files:
            return

        def refresh(_):
            cwd = self.fm.get_directory(original_path)
            cwd.load_content()

        one_file = copied_files[0]
        cwd = self.fm.thisdir
        original_path = cwd.path
        parts = self.line.split()
        au_flags = parts[1:]

        descr = "compressing files in: " + os.path.basename(parts[1])
        obj = CommandLoader(args=['apack'] + au_flags + \
            [os.path.relpath(f.path, cwd.path) for f in copied_files], descr=descr)

        self.fm.copy_buffer.clear()
        self.fm.do_cut = False

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #2
0
    def execute(self):
        """ Extract copied files to current directory """
        copied_files = tuple(self.fm.copy_buffer)

        if not copied_files:
            return

        def refresh(_):
            cwd = self.fm.get_directory(original_path)
            cwd.load_content()

        one_file = copied_files[0]
        cwd = self.fm.thisdir
        original_path = cwd.path
        au_flags = ['-X', cwd.path]
        au_flags += self.line.split()[1:]
        au_flags += ['-e']

        self.fm.copy_buffer.clear()
        self.fm.cut_buffer = False
        if len(copied_files) == 1:
            descr = "extracting: " + os.path.basename(one_file.path)
        else:
            descr = "extracting files from: " + os.path.basename(one_file.dirname)
        obj = CommandLoader(args=['aunpack'] + au_flags \
                + [f.path for f in copied_files], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #3
0
    def execute(self):
        """ Compress marked files to current directory """
        cwd = self.fm.env.cwd
        marked_files = cwd.get_selection()

        if not marked_files:
            return

        def refresh(_):
            cwd = self.fm.env.get_directory(original_path)
            cwd.load_content()

        original_path = cwd.path
        parts = self.line.split()
        au_flags = parts[1:]

        descr = "compressing files in: " + os.path.basename(parts[1])
        obj = CommandLoader(args=['apack'] + au_flags + \
                            [os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)

        for f in marked_files:
            cwd.mark_item(f, val=False)
Beispiel #4
0
    def execute(self):
        """
        :extract

        Extract marked files to the current directory
        """
        from os.path import basename
        from ranger.core.loader import CommandLoader

        cwd = self.fm.thisdir
        marked_files = cwd.get_selection()

        if not marked_files:
            return

        def refresh(_):
            cwd = self.fm.env.get_directory(original_path)
            cwd.load_content()

        one_file = marked_files[0]
        original_path = cwd.path
        au_flags = ['-x']
        au_flags += self.line.split()[1:]
        au_flags += ['-e']
        au_flags += ['-D']

        if len(marked_files) == 1:
            descr = "extracting: " + basename(one_file.path)
        else:
            descr = "extracting files from: " + basename(one_file.dirname)
        obj = CommandLoader(args=['aunpack'] + au_flags \
                + [f.path for f in marked_files], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #5
0
    def execute(self):
        """ Extract copied files to current directory """
        cwd = self.fm.env.cwd
        marked_files = tuple(cwd.get_selection())
        #old version was wih copy instead of mark
        #copied_files = tuple(self.fm.env.copy)

        if not marked_files:
            return

        def refresh(_):
            cwd = self.fm.env.get_directory(original_path)
            cwd.load_content()

        one_file = marked_files[0]
        cwd = self.fm.env.cwd
        original_path = cwd.path
        au_flags = ['-X', cwd.path]
        au_flags += self.line.split()[1:]
        au_flags += ['-e']

        self.fm.env.copy.clear()
        self.fm.env.cut = False
        if len(marked_files) == 1:
            descr = "extracting: " + os.path.basename(one_file.path)
        else:
            descr = "extracting files from: " + os.path.basename(one_file.dirname)
        obj = CommandLoader(args=['aunpack'] + au_flags \
                            + [f.path for f in marked_files], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #6
0
    def execute(self):
        import os

        cwd = self.fm.thisdir
        cf = self.fm.thisfile
        if not cwd or not cf:
            self.fm.notify("Error: no file selected !", bad=True)
            return
        
        original_path = cwd.path
        files = self.fm.thistab.get_selection()
        languages = self.args[1:]
        if not languages:
            languages = ['en']

        commandLine = ['subliminal', '-l']
        commandLine += languages
        commandLine += ['--']
        commandLine += [f.basename for f in files]
        descr = "Downloading {} subtitle(s)...".format(len(files))

        obj = CommandLoader(args=commandLine , descr=descr)

        def refresh(_):
            cwd = self.fm.env.get_directory(original_path)
            cwd.load_content()
            self.fm.notify("Downloaded {} subtitle(s)".format(len(files)))

        obj.signal_bind('after', refresh)

        self.fm.loader.add(obj) 
Beispiel #7
0
    def _extract(self, files, destination=None):
        args = ['aunpack', '-e']
        if destination is None:
            args.extend((shell_escape(f.path) for f in files))
            command = tmux_command(args)
        else:
            au_flags += ['-X', destination]
            obj = CommandLoader(args=['aunpack'] + au_flags \
                + [f.path for f in files], descr=descr)

            obj.signal_bind('after', refresh)
            self.fm.loader.add(obj)
        self.fm.execute_command(command)
Beispiel #8
0
def annex_call(fm, cmds, fname):
    # git annex fails with absolute paths ...
    thisdir = fm.thisdir
    fname = os.path.basename(fname)
    loader = CommandLoader(['git', 'annex'] + cmds + [fname],
                           'annex:{}'.format(' '.join(cmds)))

    def reload_dir():
        thisdir.unload()
        thisdir.load_content()

    loader.signal_bind('after', reload_dir)
    fm.loader.add(loader)
Beispiel #9
0
	def paste(self, overwrite=False):
		"""Paste the selected items into the current directory"""
		copied_files = tuple(self.env.copy)

		if not copied_files:
			return

		def refresh(_):
			cwd = self.env.get_directory(original_path)
			cwd.load_content()

		cwd = self.env.cwd
		original_path = cwd.path
		one_file = copied_files[0]
		if overwrite:
			cp_flags = ['-af', '--']
			mv_flags = ['-f', '--']
		else:
			cp_flags = ['--backup=numbered', '-a', '--']
			mv_flags = ['--backup=numbered', '--']

		if self.env.cut:
			self.env.copy.clear()
			self.env.cut = False
			if len(copied_files) == 1:
				descr = "moving: " + one_file.path
			else:
				descr = "moving files from: " + one_file.dirname
			obj = CommandLoader(args=['mv'] + mv_flags \
					+ [f.path for f in copied_files] \
					+ [cwd.path], descr=descr)
		else:
			if len(copied_files) == 1:
				descr = "copying: " + one_file.path
			else:
				descr = "copying files from: " + one_file.dirname
			if not overwrite and len(copied_files) == 1 \
					and one_file.dirname == cwd.path:
				# Special case: yypp
				# copying a file onto itself -> create a backup
				obj = CommandLoader(args=['cp', '-f'] + cp_flags \
						+ [one_file.path, one_file.path], descr=descr)
			else:
				obj = CommandLoader(args=['cp'] + cp_flags \
						+ [f.path for f in copied_files] \
						+ [cwd.path], descr=descr)

		obj.signal_bind('after', refresh)
		self.loader.add(obj)
Beispiel #10
0
 def refresh(_):
     cwd = self.fm.get_directory(original_path)
     cwd.load_content()
     original_path = cwd.path
     parts = self.line.split()
     au_flags = parts[1:]
     descr = "compressing files in: " + os.path.basename(parts[1])
     obj = CommandLoader(args=['apack'] + au_flags + \
             [os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr)
     obj.signal_bind('after', refresh)
     self.fm.loader.add(obj)
     def tab(self):
         """ Complete with current folder name """
         extension = ['.zip', '.tar.gz', '.rar', '.7z']
         return ['compress ' + os.path.basename(self.fm.thisdir.path) + ext for ext in extension]
Beispiel #11
0
    def execute(self):
        copied_files = tuple(self.fm.copy_buffer)

        if not copied_files:
            return

        cwd = self.fm.thisdir
        new_name = self.rest(1)

        if not new_name:
            return
        # shell cp/mv %c 

        def refresh(_):
            cwd = self.fm.get_directory(original_path)
            cwd.load_content()

        original_path = cwd.path
        one_file = copied_files[0]

        cp_flags = ['--backup=numbered', '-a', '--']
        mv_flags = ['--backup=numbered', '--']

        if self.fm.do_cut:
            self.fm.copy_buffer.clear()
            self.fm.do_cut = False
            if len(copied_files) == 1:
                descr = "moving: " + one_file.path
            else:
                descr = "moving files from: " + one_file.dirname
            obj = CommandLoader(args=['mv'] + mv_flags \
                    + [f.path for f in copied_files] \
                    + [new_name], descr=descr)
        else:
            descr = "copying files from: " + one_file.dirname
            obj = CommandLoader(args=['cp'] + cp_flags \
                    + [f.path for f in copied_files] \
                    + [new_name], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #12
0
    def execute(self):
        """ Compress marked files to current directory """
        cwd = self.fm.thisdir
        marked_files = cwd.get_selection()

        if not marked_files:
            return

        def refresh(_):
            cwd = self.fm.get_directory(original_path)
            cwd.load_content()

        original_path = cwd.path
        dest = self.line[self.line.index(' ')+1:]

        descr = "compressing files in: " + os.path.basename(dest)
        obj = CommandLoader(args=['apack', dest] + \
            [os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #13
0
    def execute(self):
        cwd = self.fm.thisdir
        copied_files = cwd.get_selection()

        if not copied_files: return

        def refresh(_):
            cwd = self.fm.get_directory(original_path)
            cwd.load_content()

        one_file = copied_files[0]
        cwd = self.fm.thisdir
        original_path = cwd.path

        line_args = self.line.split()[1:]
        if line_args:
            extraction_dir = os.path.join(cwd.path, "".join(line_args))
            os.makedirs(extraction_dir, exist_ok=True)
            flags = ['-X', extraction_dir]
            flags += ['-e']
        else:
            flags = ['-X', cwd.path]
            flags += ['-e']

        self.fm.copy_buffer.clear()
        self.fm.cut_buffer = False

        if len(copied_files) == 1:
            descr = "Extracting: " + os.path.basename(one_file.path)
        else:
            descr = "Extracting files from: " + os.path.basename(
                one_file.dirname)
        obj = CommandLoader(args=['aunpack'] + flags +
                            [f.path for f in copied_files],
                            descr=descr,
                            read=True)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #14
0
    def execute(self):

        cwd = self.fm.thisdir

        def refresh(_):
            cwd = self.fm.get_directory(original_path)
            cwd.load_content()

        original_path = cwd.path

        if self.arg(1):

            target_filenames = self.rest(1)

        else:

            target_filenames = cwd.get_selection()

        for tf in target_filenames:

            if not os.path.exists(tf.path):
                self.fm.notify("The given file does not exist! " +
                               os.path.relpath(tf.path, cwd.path),
                               bad=True)
                return

        rel_filenames = [
            os.path.relpath(f.path, cwd.path) for f in target_filenames
        ]

        descr = "Adding file(s) " + " ".join(rel_filenames) + " to git"

        self.fm.notify(descr)

        obj = CommandLoader(args=['git', 'add'] + rel_filenames, descr=descr)

        obj.signal_bind('after', refresh)
        self.fm.loader.add(obj)
Beispiel #15
0
    def execute(self):

        if self.arg(1):

            commit_msg = self.rest(1)

        else:
            self.fm.notify("You have to provide commit message")
            return

        obj = CommandLoader(args=['git', 'commit', "-m", commit_msg],
                            descr="committing latest changes")

        self.fm.loader.add(obj)
Beispiel #16
0
    def execute(self):

        obj = None

        if not self.arg(1):
            self.fm.notify('Syntax: j <pathname>')
            return

        def handle_results(_):
            lines = obj.stdout_buffer.split('\n')
            path = None

            for line in lines:
                splits = line.split(' ', 1)

                if len(splits) < 2:
                    break

                count, path = splits
                count = count.strip()
                path = path.strip()

                if count == 'common:':
                    break

            if path:
                self.fm.cd(path)
            else:
                self.fm.notify('Nothing matched.')

        z_path = os.path.expanduser('~/Tools/z/z.sh')
        cmd = 'source {} && _z -l {} 2>&1'.format(z_path, self.arg(1))
        obj = CommandLoader(args=['/usr/bin/zsh', '-c', cmd],
                            descr='Running z',
                            read=True)
        obj.signal_bind('after', handle_results)
        self.fm.loader.add(obj)
Beispiel #17
0
    def get_preview(self, file, width, height):
        pager = self.ui.get_pager()
        path = file.realpath

        if not path or not os.path.exists(path):
            return None

        if self.settings.preview_script and self.settings.use_preview_script:
            # self.previews is a 2 dimensional dict:
            # self.previews['/tmp/foo.jpg'][(80, 24)] = "the content..."
            # self.previews['/tmp/foo.jpg']['loading'] = False
            # A -1 in tuples means "any"; (80, -1) = wid. of 80 and any hei.
            # The key 'foundpreview' is added later. Values in (True, False)
            # XXX: Previews can break when collapse_preview is on and the
            # preview column is popping out as you move the cursor on e.g. a
            # PDF file.
            try:
                data = self.previews[path]
            except:
                data = self.previews[path] = {'loading': False}
            else:
                if data['loading']:
                    return None


            found = data.get((-1, -1), data.get((width, -1),
                data.get((-1, height), data.get((width, height), False))))
            if found == False:
                try:
                    stat_ = os.stat(self.settings.preview_script)
                except:
                    self.fm.notify("Preview Script `%s' doesn't exist!" %
                            self.settings.preview_script, bad=True)
                    return None

                if not stat_.st_mode & S_IEXEC:
                    self.fm.notify("Preview Script `%s' is not executable!" %
                            self.settings.preview_script, bad=True)
                    return None

                data['loading'] = True

                if 'directimagepreview' in data:
                    data['foundpreview'] = True
                    data['imagepreview'] = True
                    pager.set_image(path)
                    data['loading'] = False
                    return path

                cacheimg = os.path.join(ranger.arg.cachedir, self.sha1_encode(path))
                if (os.path.isfile(cacheimg) and os.path.getmtime(cacheimg) > os.path.getmtime(path)):
                    data['foundpreview'] = True
                    data['imagepreview'] = True
                    pager.set_image(cacheimg)
                    data['loading'] = False
                    return cacheimg

                loadable = CommandLoader(args=[self.settings.preview_script,
                    path, str(width), str(height), cacheimg,
                    str(self.settings.preview_images)], read=True,
                    silent=True, descr="Getting preview of %s" % path)
                def on_after(signal):
                    exit = signal.process.poll()
                    content = signal.loader.stdout_buffer
                    data['foundpreview'] = True
                    if exit == 0:
                        data[(width, height)] = content
                    elif exit == 3:
                        data[(-1, height)] = content
                    elif exit == 4:
                        data[(width, -1)] = content
                    elif exit == 5:
                        data[(-1, -1)] = content
                    elif exit == 6:
                        data['imagepreview'] = True
                    elif exit == 7:
                        data['directimagepreview'] = True
                    elif exit == 1:
                        data[(-1, -1)] = None
                        data['foundpreview'] = False
                    elif exit == 2:
                        f = codecs.open(path, 'r', errors='ignore')
                        try:
                            data[(-1, -1)] = f.read(1024 * 32)
                        except UnicodeDecodeError:
                            f.close()
                            f = codecs.open(path, 'r', encoding='latin-1',
                                    errors='ignore')
                            data[(-1, -1)] = f.read(1024 * 32)
                        f.close()
                    else:
                        data[(-1, -1)] = None
                    if self.thisfile and self.thisfile.realpath == path:
                        self.ui.browser.need_redraw = True
                    data['loading'] = False
                    pager = self.ui.get_pager()
                    if self.thisfile and self.thisfile.is_file:
                        if 'imagepreview' in data:
                            pager.set_image(cacheimg)
                            return cacheimg
                        elif 'directimagepreview' in data:
                            pager.set_image(path)
                            return path
                        else:
                            pager.set_source(self.thisfile.get_preview_source(
                                pager.wid, pager.hei))
                def on_destroy(signal):
                    try:
                        del self.previews[path]
                    except:
                        pass
                loadable.signal_bind('after', on_after)
                loadable.signal_bind('destroy', on_destroy)
                self.loader.add(loadable)
                return None
            else:
                return found
        else:
            try:
                return codecs.open(path, 'r', errors='ignore')
            except:
                return None
Beispiel #18
0
	def get_preview(self, path, width, height):
		if self.settings.preview_script and self.settings.use_preview_script:
			# self.previews is a 2 dimensional dict:
			# self.previews['/tmp/foo.jpg'][(80, 24)] = "the content..."
			# self.previews['/tmp/foo.jpg']['loading'] = False
			# A -1 in tuples means "any"; (80, -1) = wid. of 80 and any hei.
			# The key 'foundpreview' is added later. Values in (True, False)
			try:
				data = self.previews[path]
			except:
				data = self.previews[path] = {'loading': False}
			else:
				if data['loading']:
					return None

			found = data.get((-1, -1), data.get((width, -1),
				data.get((-1, height), data.get((width, height), False))))
			if found == False:
				data['loading'] = True
				loadable = CommandLoader(args=[self.settings.preview_script,
					path, str(width), str(height)], read=True,
					silent=True, descr="Getting preview of %s" % path)
				def on_after(signal):
					exit = signal.process.poll()
					content = signal.loader.stdout_buffer
					data['foundpreview'] = True
					if exit == 0:
						data[(width, height)] = content
					elif exit == 3:
						data[(-1, height)] = content
					elif exit == 4:
						data[(width, -1)] = content
					elif exit == 5:
						data[(-1, -1)] = content
					elif exit == 1:
						data[(-1, -1)] = None
						data['foundpreview'] = False
					elif exit == 2:
						data[(-1, -1)] = open(path, 'r').read(1024 * 32)
					else:
						data[(-1, -1)] = None
					if self.env.cf.realpath == path:
						self.ui.browser.need_redraw = True
					data['loading'] = False
					pager = self.ui.browser.pager
					if self.env.cf and self.env.cf.is_file:
						pager.set_source(self.env.cf.get_preview_source(
							pager.wid, pager.hei))
				def on_destroy(signal):
					try:
						del self.previews[path]
					except:
						pass
				loadable.signal_bind('after', on_after)
				loadable.signal_bind('destroy', on_destroy)
				self.loader.add(loadable)
				return None
			else:
				return found
		else:
			try:
				return open(path, 'r')
			except:
				return None
Beispiel #19
0
 def execute(self):
     command = ['unar', '-q', '-o', self.fm.thisdir.path] + self.args[1:]
     for file in self.fm.thistab.get_selection():
         obj = CommandLoader(args=command + [file.path], descr=f'unar {file.path}')
         obj.signal_bind('after', lambda _: self.fm.thisdir.load_content())
         self.fm.loader.add(obj)
Beispiel #20
0
 def execute(self):
     files = [file.path for file in self.fm.thistab.get_selection()]
     if files:
         obj = CommandLoader(args=['trash'] + files, descr='Trash')
         obj.signal_bind('after', lambda _: self.fm.thisdir.load_content())
         self.fm.loader.add(obj)
Beispiel #21
0
    def execute(self):

        obj = CommandLoader(args=['git', 'pull'],
                            descr="pulling latest changes")

        self.fm.loader.add(obj)
Beispiel #22
0
	def get_preview(self, path, width, height):
		if self.settings.preview_script and self.settings.use_preview_script:
			# self.previews is a 2 dimensional dict:
			# self.previews['/tmp/foo.jpg'][(80, 24)] = "the content..."
			# self.previews['/tmp/foo.jpg']['loading'] = False
			# A -1 in tuples means "any"; (80, -1) = wid. of 80 and any hei.
			# The key 'foundpreview' is added later. Values in (True, False)
			# XXX: Previews can break when collapse_preview is on and the
			# preview column is popping out as you move the cursor on e.g. a
			# PDF file.
			try:
				data = self.previews[path]
			except:
				data = self.previews[path] = {'loading': False}
			else:
				if data['loading']:
					return None

			found = data.get((-1, -1), data.get((width, -1),
				data.get((-1, height), data.get((width, height), False))))
			if found == False:
				data['loading'] = True
				loadable = CommandLoader(args=[self.settings.preview_script,
					path, str(width), str(height)], read=True,
					silent=True, descr="Getting preview of %s" % path)
				def on_after(signal):
					exit = signal.process.poll()
					content = signal.loader.stdout_buffer
					data['foundpreview'] = True
					if exit == 0:
						data[(width, height)] = content
					elif exit == 3:
						data[(-1, height)] = content
					elif exit == 4:
						data[(width, -1)] = content
					elif exit == 5:
						data[(-1, -1)] = content
					elif exit == 1:
						data[(-1, -1)] = None
						data['foundpreview'] = False
					elif exit == 2:
						f = codecs.open(path, 'r', errors='ignore')
						try:
							data[(-1, -1)] = f.read(1024 * 32)
						except UnicodeDecodeError:
							f.close()
							f = codecs.open(path, 'r', encoding='latin-1',
									errors='ignore')
							data[(-1, -1)] = f.read(1024 * 32)
						f.close()
					else:
						data[(-1, -1)] = None
					if self.thisfile.realpath == path:
						self.ui.browser.need_redraw = True
					data['loading'] = False
					pager = self.ui.browser.pager
					if self.thisfile and self.thisfile.is_file:
						pager.set_source(self.thisfile.get_preview_source(
							pager.wid, pager.hei))
				def on_destroy(signal):
					try:
						del self.previews[path]
					except:
						pass
				loadable.signal_bind('after', on_after)
				loadable.signal_bind('destroy', on_destroy)
				self.loader.add(loadable)
				return None
			else:
				return found
		else:
			try:
				return codecs.open(path, 'r', errors='ignore')
			except:
				return None