Пример #1
0
def os_delete_fs_items(fsi_paths, events=E_FILE_DELETED, force=False):
    from aipoed import utils
    _CONSOLE_LOG.start_cmd(_('delete {0}').format(utils.quoted_join(fsi_paths)))
    serr = ""
    errorcode = CmdResult.ERROR
    for fsi_path in fsi_paths:
        try:
            if os.path.isdir(fsi_path) and not os.path.islink(fsi_path):
                if force:
                    shutil.rmtree(fsi_path)
                else:
                    os.removedirs(fsi_path)
            else:
                os.remove(fsi_path)
            _CONSOLE_LOG.append_stdout(_('Deleted: {0}\n').format(fsi_path))
        except OSError as edata:
            if edata.errno == errno.ENOTEMPTY:
                errorcode = CmdResult.ERROR | Suggestion.FORCE
            errmsg = _("Error: {}: \"{}\"\n").format(edata.strerror, fsi_path)
            serr += errmsg
            _CONSOLE_LOG.append_stderr(errmsg)
    _CONSOLE_LOG.end_cmd()
    enotify.notify_events(events)
    return CmdResult(errorcode, "", serr)  if serr else CmdResult.ok()
Пример #2
0
 def handle_control_c_key_press_cb(self):
     clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
     sel = utils.quoted_join(self.get_selected_remotes())
     clipboard.set_text(sel, len(sel))
Пример #3
0
 def add_selected_fsi_paths_to_clipboard(self, clipboard=None):
     if not clipboard:
         clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
     sel = utils.quoted_join(self.get_selected_fsi_paths())
     clipboard.set_text(sel, len(sel))
Пример #4
0
def os_move_or_copy_fs_items(fsi_paths, destn, opsym, overwrite=False, force=False, verbose=False):
    assert opsym in (Relation.MOVED_TO, Relation.COPIED_TO), _("Invalid operation requested")
    assert len(fsi_paths) > 1
    from aipoed import utils
    def _overwrite_msg(overwrites):
        if len(overwrites) == 0:
            return ""
        elif len(overwrites) > 1:
            return _("Files:\n\t{0}\nalready exist.").format("\n\t".join(["\"" + fp + "\"" for fp in overwrites]))
        else:
            return _("File \"{0}\" already exists.").format(overwrites[0])
    _CONSOLE_LOG.start_cmd("{0} {1} {2}\n".format(utils.quoted_join(fsi_paths), opsym, destn))
    if not os.path.isdir(destn):
        result = CmdResult.error(stderr=_('"{0}": Destination must be a directory for multifile move/copy.').format(destn))
        _CONSOLE_LOG.end_cmd(result)
        return result
    opn_paths_list = [(fsi_path, os.path.join(destn, os.path.basename(fsi_path))) for fsi_path in fsi_paths]
    omsg = "\n".join(["{0} {1} {2}.".format(src, opsym, destn) for (src, destn) in opn_paths_list]) if verbose else ""
    if not overwrite:
        overwrites = [destn for (src, destn) in opn_paths_list if os.path.exists(destn)]
        if len(overwrites) > 0:
            emsg = _overwrite_msg(overwrites)
            result = CmdResult.error(omsg, emsg) | Suggestion.OVERWRITE_OR_RENAME
            _CONSOLE_LOG.end_cmd(result)
            return result
    failed_opns_str = ""
    rescode = CmdResult.OK
    for (src, tgt) in opn_paths_list:
        if verbose:
            _CONSOLE_LOG.append_stdout("{0} {1} {2}.".format(src, opsym, tgt))
        if os.path.exists(tgt):
            try:
                if os.path.isdir(tgt) and not os.path.islink(tgt):
                    if force:
                        shutil.rmtree(tgt)
                    else:
                        os.removedirs(tgt)
                else:
                    os.remove(tgt)
            except OSError as edata:
                rescode |= CmdResult.ERROR | Suggestion.FORCE if edata.errno == errno.ENOTEMPTY else CmdResult.ERROR
                serr = _("Error: {}: \"{}\" {} \"{}\"\n").format(edata.strerror, src, opsym, tgt)
                _CONSOLE_LOG.append_stderr(serr)
                failed_opns_str += serr
                continue
            except shutil.Error as edata:
                rescode |= CmdResult.ERROR
                serr = _("Error: \"{0}\" {1} \"{2}\" failed.\n").format(src, opsym, tgt)
                for src_path, tgt_path, reason in edata.args:
                    serr += _("Error: \"{0}\" {1} \"{2}\": {3}.\n").format(src_path, opsym, tgt_path, reason)
                _CONSOLE_LOG.append_stderr(serr)
                failed_opns_str += serr
                continue
        try:
            if opsym is Relation.MOVED_TO:
                os.rename(src, tgt)
            elif os.path.isdir(src):
                shutil.copytree(src, tgt)
            else:
                shutil.copy2(src, tgt)
        except OSError as edata:
            rescode |= CmdResult.ERROR
            serr = _("Error: \"{0}\" {1} \"{2}\" failed. {3}.\n").format(src, opsym, tgt, edata.strerror)
            _CONSOLE_LOG.append_stderr(serr)
            failed_opns_str += serr
            continue
        except shutil.Error as edata:
            rescode |= CmdResult.ERROR
            serr = _("Error: \"{0}\" {1} \"{2}\" failed.\n").format(src, opsym, tgt)
            for src_path, tgt_path, reason in edata.args:
                serr += _("Error: \"{0}\" {1} \"{2}\": {3}.\n").format(src_path, opsym, tgt_path, reason)
            _CONSOLE_LOG.append_stderr(serr)
            failed_opns_str += serr
            continue
    _CONSOLE_LOG.end_cmd()
    enotify.notify_events(E_FILE_MOVED)
    return CmdResult(rescode, omsg, failed_opns_str)