# This file is part of ranger, the console file manager.
# License: GNU GPL version 3, see the file "AUTHORS" for details.
from collections import deque
from time import time, sleep
from subprocess import Popen, PIPE
from ranger.core.shared import FileManagerAware
from ranger.ext.signals import SignalDispatcher
import math
import os.path
import sys
import select
try:
import chardet
HAVE_CHARDET = True
except:
HAVE_CHARDET = False
class Loadable(object):
paused = False
progressbar_supported = False
def __init__(self, gen, descr):
self.load_generator = gen
self.description = descr
self.percent = 0
def get_description(self):
return self.description
def pause(self):
self.paused = True
def unpause(self):
try:
del self.paused
except:
pass
def destroy(self):
pass
class CopyLoader(Loadable, FileManagerAware):
progressbar_supported = True
def __init__(self, copy_buffer, do_cut=False, overwrite=False):
self.copy_buffer = tuple(copy_buffer)
self.do_cut = do_cut
self.original_copy_buffer = copy_buffer
self.original_path = self.fm.thistab.path
self.overwrite = overwrite
self.percent = 0
if self.copy_buffer:
self.one_file = self.copy_buffer[0]
Loadable.__init__(self, self.generate(), 'Calculating size...')
def _calculate_size(self, step):
from os.path import join
size = 0
stack = [f.path for f in self.copy_buffer]
while stack:
fname = stack.pop()
if os.path.islink(fname):
continue
if os.path.isdir(fname):
stack.extend([join(fname, item) for item in os.listdir(fname)])
else:
try:
fstat = os.stat(fname)
except:
continue
size += max(step, math.ceil(fstat.st_size / step) * step)
return size
def generate(self):
from ranger.ext import shutil_generatorized as shutil_g
if self.copy_buffer:
# TODO: Don't calculate size when renaming (needs detection)
bytes_per_tick = shutil_g.BLOCK_SIZE
size = max(1, self._calculate_size(bytes_per_tick))
if self.do_cut:
self.original_copy_buffer.clear()
if len(self.copy_buffer) == 1:
self.description = "moving: " + self.one_file.path
else:
self.description = "moving files from: " + self.one_file.dirname
for f in self.copy_buffer:
for tf in self.fm.tags.tags:
if tf == f.path or str(tf).startswith(f.path):
tag = self.fm.tags.tags[tf]
self.fm.tags.remove(tf)
self.fm.tags.tags[tf.replace(f.path, self.original_path \
+ '/' + f.basename)] = tag
self.fm.tags.dump()
for done in shutil_g.move(src=f.path,
dst=self.original_path,
overwrite=self.overwrite):
self.percent = float(done) / size * 100.
yield
else:
if len(self.copy_buffer) == 1:
self.description = "copying: " + self.one_file.path
else:
self.description = "copying files from: " + self.one_file.dirname
for f in self.copy_buffer:
if os.path.isdir(f.path) and not os.path.islink(f.path):
for done in shutil_g.copytree(src<<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module ranger.container.commandlist</title>
</head><body bgcolor="#f0f0f8">
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.<a href="ranger.container.html"><font color="#ffffff">container</font></a>.commandlist</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/ranger/ranger/container/commandlist.py">/home/hut/ranger/ranger/container/commandlist.py</a></font></td></tr></table>
<p><tt># Copyright (c) 2009, 2010 hut <hut@lavabit.com><br>
#<br>
# Permission to use, copy, modify, and/or distribute this software for any<br>
# purpose with or without fee is hereby granted, provided that the above<br>
# copyright notice and this permission notice appear in all copies.<br>
#<br>
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES<br>
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF<br>
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR<br>
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES<br>
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN<br>
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF<br>
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a>
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="ranger.container.commandlist.html#Command">Command</a>
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="ranger.container.commandlist.html#AliasedCommand">AliasedCommand</a>
</font></dt></dl>
</dd>
<dt><font face="helvetica, arial"><a href="ranger.container.commandlist.html#CommandArgument">CommandArgument</a>
</font></dt><dt><font face="helvetica, arial"><a href="ranger.container.commandlist.html#CommandList">CommandList</a>
</font></dt><dt><font face="helvetica, arial"><a href="ranger.container.commandlist.html#Show">Show</a>
</font></dt></dl>
</dd>
</dl>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="AliasedCommand">class <strong>AliasedCommand</strong></a>(<a href="ranger.container.commandlist.html#Command">Command</a>)</font></td></tr>
<tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td>
<td width="100%"><dl><dt>Method resolution order:</dt>
<dd><a href="ranger.container.commandlist.html#AliasedCommand">AliasedCommand</a></dd>
<dd><a href="ranger.container.commandlist.html#Command">Command</a></dd>
<dd><a href="__builtin__.html#object">__builtin__.object</a></dd>
</dl>
<hr>
Methods defined here:<br>
<dl><dt><a name="AliasedCommand-__init__"><strong>__init__</strong></a>(self, getter, keys)</dt></dl>
<dl><dt><a name="AliasedCommand-get_execute"><strong>get_execute</strong></a>(self)</dt></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>execute</strong></dt>
</dl>
<hr>
Methods inherited from <a href="ranger.container.commandlist.html#Command">Command</a>:<br>
<dl><dt><a name="AliasedCommand-execute_wrap"><strong>execute_wrap</strong></a>(self, displayable)</dt></dl>
<hr>
Data descriptors inherited from <a href="ranger.container.commandlist.html#Command">Command</a>:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary for instance variables (if defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list of weak references to the object (if defined)</tt></dd>
</dl>
<hr>
Data and other attributes inherited from <a href="ranger.container.commandlist.html#Command">Command</a>:<br>
<dl><dt><strong>keys</strong> = []</dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="Command">class <strong>Command</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt><a href="#Command">Command</a> objects store information about a command<br> </tt></td></tr>
<tr><td> </td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="Command-__init__"><strong>__init__</strong></a>(self, fnc, keys)</dt></dl>
<dl><dt><a name="Command-execute"><strong>execute</strong></a>(self, *args)</dt><dd><tt>Execute the command</tt></dd></dl>
<dl><dt><a name="Command-execute_wrap"><strong>execute_wrap</strong></a>(self, displayable)</dt></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary for instance variables (if defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list of weak references to the object (if defined)</tt></dd>
</dl>
<hr>
Data and other attributes defined here:<br>
<dl><dt><strong>keys</strong> = []</dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="CommandArgument">class <strong>CommandArgument</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
<tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="CommandArgument-__init__"><strong>__init__</strong></a>(self, fm, displayable, keybuffer)</dt></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary for instance variables (if defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list of weak references to the object (if defined)</tt></dd>
</dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="CommandList">class <strong>CommandList</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt>CommandLists are dictionary-like objects which give you a command<br>
for a given key combination. CommandLists must be filled before use.<br> </tt></td></tr>
<tr><td> </td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="CommandList-__getitem__"><strong>__getitem__</strong></a>(self, key)</dt><dd><tt>Returns the command with the given key combination</tt></dd></dl>
<dl><dt><a name="CommandList-__init__"><strong>__init__</strong></a>(self)</dt></dl>
<dl><dt><a name="CommandList-alias"><strong>alias</strong></a>(self, existing, *new)</dt><dd><tt>bind the <new> keys to the command of the <existing> key</tt></dd></dl>
<dl><dt><a name="CommandList-bind"><strong>bind</strong></a>(self, fnc, *keys)</dt><dd><tt>create a <a href="#Command">Command</a> <a href="__builtin__.html#object">object</a> and assign it to the given key combinations.</tt></dd></dl>
<dl><dt><a name="CommandList-clear"><strong>clear</strong></a>(self)</dt><dd><tt>remove all bindings</tt></dd></dl>
<dl><dt><a name="CommandList-rebuild_paths"><strong>rebuild_paths</strong></a>(self)</dt><dd><tt>Fill the path dictionary with dummie objects.<br>
We need to know when to clear the keybuffer (when a wrong key is pressed)<br>
and when to wait for the rest of the key combination. For "gg" we<br>
will assign "g" to a dummy which tells the program to do the latter<br>
and wait.</tt></dd></dl>
<dl><dt><a name="CommandList-remove_dummies"><strong>remove_dummies</strong></a>(self)</dt><dd><tt>Remove dummie objects in case you have to rebuild a path dictionary<br>
which already contains dummie objects.</tt></dd></dl>
<dl><dt><a name="CommandList-show"><strong>show</strong></a>(self, *keys, **keywords)</dt><dd><tt>create a <a href="#Show">Show</a> <a href="__builtin__.html#object">object</a> and assign it to the given key combinations.</tt></dd></dl>
<dl><dt><a name="CommandList-unbind"><strong>unbind</strong></a>(self, *keys)</dt></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary for instance variables (if defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list of weak references to the object (if defined)</tt></dd>
</dl>
<hr>
Data and other attributes defined here:<br>
<dl><dt><strong>commandlist</strong> = []</dl>
<dl><dt><strong>dummies_in_paths</strong> = False</dl>
<dl><dt><strong>dummy_object</strong> = None</dl>
<dl><dt><strong>paths</strong> = {}</dl>
</td></tr></table> <p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="Show">class <strong>Show</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt><a href="#Show">Show</a> objects do things without clearing the keybuffer<br> </tt></td></tr>
<tr><td> </td>
<td width="100%">Methods defined here:<br>
<dl><dt><a name="Show-__init__"><strong>__init__</strong></a>(self, dictionary, keys)</dt></dl>
<hr>
Data descriptors defined here:<br>
<dl><dt><strong>__dict__</strong></dt>
<dd><tt>dictionary for instance variables (if defined)</tt></dd>
</dl>
<dl><dt><strong>__weakref__</strong></dt>
<dd><tt>list of weak references to the object (if defined)</tt></dd>
</dl>
<hr>
Data and other attributes defined here:<br>
<dl><dt><strong>keys</strong> = []</dl>
<dl><dt><strong>text</strong> = ''</dl>
</td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#eeaa77">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
<tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td>
<td width="100%"><dl><dt><a name="-cmdarg"><strong>cmdarg</strong></a>(displayable)</dt></dl>
</td></tr></table>
</body></html>