diff options
author | hut <hut@lavabit.com> | 2011-10-22 04:23:36 +0200 |
---|---|---|
committer | hut <hut@lavabit.com> | 2011-10-22 04:23:36 +0200 |
commit | b2b58e7f15e5700a1f8248fb1bf32386b4b45c57 (patch) | |
tree | 07ab83bf101bf11ac42c50e9a55adb4d3ae228a4 /ranger/fsobject/directory.py | |
parent | 812447408be9b67850b253fe9d53a88ad31856ae (diff) | |
download | ranger-b2b58e7f15e5700a1f8248fb1bf32386b4b45c57.tar.gz |
New key "dc" to get cumulative size of selected directories
Diffstat (limited to 'ranger/fsobject/directory.py')
-rw-r--r-- | ranger/fsobject/directory.py | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/ranger/fsobject/directory.py b/ranger/fsobject/directory.py index b3a35a0a..04ed35e0 100644 --- a/ranger/fsobject/directory.py +++ b/ranger/fsobject/directory.py @@ -25,6 +25,7 @@ from ranger.fsobject import File, FileSystemObject from ranger.core.shared import SettingsAware from ranger.ext.accumulator import Accumulator from ranger.ext.lazy_property import lazy_property +from ranger.ext.human_readable import human_readable def sort_by_basename(path): """returns path.basename (for sorting)""" @@ -79,6 +80,8 @@ class Directory(FileSystemObject, Accumulator, Loadable, SettingsAware): content_outdated = False content_loaded = False + _cumulative_size_calculated = False + sort_dict = { 'basename': sort_by_basename, 'natural': sort_naturally, @@ -185,8 +188,14 @@ class Directory(FileSystemObject, Accumulator, Loadable, SettingsAware): hidden_filter = not self.settings.show_hidden \ and self.settings.hidden_filter filelist = os.listdir(mypath) - self.size = len(filelist) - self.infostring = ' %d' % self.size + if not self._cumulative_size_calculated \ + or self.content_loaded: + self.size = len(filelist) + self.infostring = ' %d' % self.size + if self._cumulative_size_calculated: + self._cumulative_size_calculated = False + if self.is_link: + self.infostring = '->' + self.infostring filenames = [mypath + (mypath == '/' and fname or '/' + fname)\ for fname in filelist if accept_file( fname, mypath, hidden_filter, self.filter)] @@ -327,6 +336,31 @@ class Directory(FileSystemObject, Accumulator, Loadable, SettingsAware): else: self.correct_pointer() + def _get_cumulative_size(self): + if self.size == 0: + return 0 + cum = 0 + for dirpath, dirnames, filenames in os.walk(self.path, + onerror=lambda _: None): + for file in filenames: + try: + if dirpath == self.path: + stat = os.stat(os.path.realpath(dirpath + "/" + file)) + else: + stat = os.stat(dirpath + "/" + file) + cum += stat.st_size + except: + pass + return cum + + def look_up_cumulative_size(self): + if not self._cumulative_size_calculated: + self._cumulative_size_calculated = True + cum = self._get_cumulative_size() + self.size = cum + self.infostring = ('-> ' if self.is_link else ' ') + \ + human_readable(cum) + @lazy_property def size(self): try: |