FollieHiyuki-dotfiles/home/.config/ranger/plugins/extract.py
2021-03-26 17:28:09 +03:00

84 lines
2.5 KiB
Python

import os
from ranger.api.commands import *
from ranger.core.loader import CommandLoader
class extract(Command):
def execute(self):
"""Extract copied files to current directory or directory
specified in a command line
"""
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:
flags = ['-X', os.path.join(cwd.path, "".join(line_args))]
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)
class extract_to_dirs(Command):
def execute(self):
""" Extract copied files to a subdirectories """
cwd = self.fm.thisdir
original_path = cwd.path
copied_files = cwd.get_selection()
if not copied_files:
return
def refresh(_):
cwd = self.fm.get_directory(original_path)
cwd.load_content()
def make_flags(fn):
dirname = os.path.splitext(fn)[0] + os.path.sep
#os.mkdir(dirname, exists_ok=True)
flags = ['-X', dirname]
#flags += self.line.split()[1:]
flags += ['-e']
return flags
one_file = copied_files[0]
self.fm.copy_buffer.clear()
self.fm.cut_buffer = False
# Making description line
if len(copied_files) == 1:
descr = "Extracting: " + os.path.basename(one_file.path)
else:
descr = "Extracting files from: " + os.path.basename(one_file.dirname)
# Extracting files
for f in copied_files:
obj = CommandLoader(args=['aunpack'] + make_flags(f.path) + [f.path], descr=descr, read=True)
obj.signal_bind('after', refresh)
self.fm.loader.add(obj)