From 20d28b94d5d1f7898b376e3b3dcf41a8d6c37065 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 12 Mar 2010 17:17:48 +0100 Subject: moved ranger.container.environment to ranger.core.environment --- ranger/__main__.py | 2 +- ranger/container/environment.py | 176 ---------------------------------------- ranger/core/environment.py | 176 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 177 deletions(-) delete mode 100644 ranger/container/environment.py create mode 100644 ranger/core/environment.py diff --git a/ranger/__main__.py b/ranger/__main__.py index 361f9455..f6da1910 100644 --- a/ranger/__main__.py +++ b/ranger/__main__.py @@ -92,7 +92,7 @@ def main(): import ranger from ranger.ext import curses_interrupt_handler from ranger.core.fm import FM - from ranger.container.environment import Environment + from ranger.core.environment import Environment from ranger.shared.settings import SettingsAware from ranger.gui.defaultui import DefaultUI as UI from ranger.fsobject.file import File diff --git a/ranger/container/environment.py b/ranger/container/environment.py deleted file mode 100644 index 347bd2b1..00000000 --- a/ranger/container/environment.py +++ /dev/null @@ -1,176 +0,0 @@ -# Copyright (C) 2009, 2010 Roman Zimbelmann -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -from os.path import abspath, normpath, join, expanduser, isdir -import os -from ranger.fsobject.directory import Directory, NoDirectoryGiven -from ranger.container import KeyBuffer, History -from ranger.shared import SettingsAware -import curses - -class Environment(SettingsAware): - """A collection of data which is relevant for more than - one class. - """ - - pwd = None # current directory - cf = None # current file - copy = None - cmd = None - cut = None - termsize = None - history = None - directories = None - last_search = None - pathway = None - path = None - keybuffer = None - - def __init__(self, path): - self.path = abspath(expanduser(path)) - self.pathway = () - self.directories = {} - self.keybuffer = KeyBuffer() - self.copy = set() - self.history = History(self.settings.max_history_size) - - from ranger.shared import EnvironmentAware - EnvironmentAware.env = self - - def key_append(self, key): - """Append a key to the keybuffer""" - - # special keys: - if key == curses.KEY_RESIZE: - self.keybuffer.clear() - - self.keybuffer.append(key) - - def key_clear(self): - """Clear the keybuffer""" - self.keybuffer.clear() - - def at_level(self, level): - """Returns the FileSystemObject at the given level. - level 1 => preview - level 0 => current file/directory - level <0 => parent directories""" - if level <= 0: - try: - return self.pathway[level - 1] - except IndexError: - return None - else: - try: - return self.directories[self.cf.path] - except AttributeError: - return None - except KeyError: - return self.cf - - def garbage_collect(self): - """Delete unused directory objects""" - from ranger.fsobject.fsobject import FileSystemObject - for key in tuple(self.directories.keys()): - value = self.directories[key] - if isinstance(value, FileSystemObject): - if value.is_older_than(1200): - del self.directories[key] - - def get_selection(self): - if self.pwd: - return self.pwd.get_selection() - return set() - - def get_directory(self, path): - """Get the directory object at the given path""" - path = abspath(path) - try: - return self.directories[path] - except KeyError: - obj = Directory(path) - self.directories[path] = obj - return obj - - def get_free_space(self, path): - from os import statvfs - stat = statvfs(path) - return stat.f_bavail * stat.f_bsize - - def assign_cursor_positions_for_subdirs(self): - """Assign correct cursor positions for subdirectories""" - last_path = None - for path in reversed(self.pathway): - if last_path is None: - last_path = path - continue - - path.move_to_obj(last_path) - last_path = path - - def ensure_correct_pointer(self): - if self.pwd: - self.pwd.correct_pointer() - - def history_go(self, relative): - """Move relative in history""" - if self.history: - self.history.move(relative).go() - - def enter_dir(self, path, history = True): - """Enter given path""" - if path is None: return - path = str(path) - - # get the absolute path - path = normpath(join(self.path, expanduser(path))) - - if not isdir(path): - return - - try: - new_pwd = self.get_directory(path) - except NoDirectoryGiven: - return False - - self.path = path - self.pwd = new_pwd - os.chdir(path) - - self.pwd.load_content_if_outdated() - - # build the pathway, a tuple of directory objects which lie - # on the path to the current directory. - if path == '/': - self.pathway = (self.get_directory('/'), ) - else: - pathway = [] - currentpath = '/' - for dir in path.split('/'): - currentpath = join(currentpath, dir) - pathway.append(self.get_directory(currentpath)) - self.pathway = tuple(pathway) - - self.assign_cursor_positions_for_subdirs() - - # set the current file. - self.pwd.directories_first = self.settings.directories_first - self.pwd.sort_if_outdated() - self.cf = self.pwd.pointed_obj - - if history: - self.history.add(new_pwd) - - return True diff --git a/ranger/core/environment.py b/ranger/core/environment.py new file mode 100644 index 00000000..347bd2b1 --- /dev/null +++ b/ranger/core/environment.py @@ -0,0 +1,176 @@ +# Copyright (C) 2009, 2010 Roman Zimbelmann +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from os.path import abspath, normpath, join, expanduser, isdir +import os +from ranger.fsobject.directory import Directory, NoDirectoryGiven +from ranger.container import KeyBuffer, History +from ranger.shared import SettingsAware +import curses + +class Environment(SettingsAware): + """A collection of data which is relevant for more than + one class. + """ + + pwd = None # current directory + cf = None # current file + copy = None + cmd = None + cut = None + termsize = None + history = None + directories = None + last_search = None + pathway = None + path = None + keybuffer = None + + def __init__(self, path): + self.path = abspath(expanduser(path)) + self.pathway = () + self.directories = {} + self.keybuffer = KeyBuffer() + self.copy = set() + self.history = History(self.settings.max_history_size) + + from ranger.shared import EnvironmentAware + EnvironmentAware.env = self + + def key_append(self, key): + """Append a key to the keybuffer""" + + # special keys: + if key == curses.KEY_RESIZE: + self.keybuffer.clear() + + self.keybuffer.append(key) + + def key_clear(self): + """Clear the keybuffer""" + self.keybuffer.clear() + + def at_level(self, level): + """Returns the FileSystemObject at the given level. + level 1 => preview + level 0 => current file/directory + level <0 => parent directories""" + if level <= 0: + try: + return self.pathway[level - 1] + except IndexError: + return None + else: + try: + return self.directories[self.cf.path] + except AttributeError: + return None + except KeyError: + return self.cf + + def garbage_collect(self): + """Delete unused directory objects""" + from ranger.fsobject.fsobject import FileSystemObject + for key in tuple(self.directories.keys()): + value = self.directories[key] + if isinstance(value, FileSystemObject): + if value.is_older_than(1200): + del self.directories[key] + + def get_selection(self): + if self.pwd: + return self.pwd.get_selection() + return set() + + def get_directory(self, path): + """Get the directory object at the given path""" + path = abspath(path) + try: + return self.directories[path] + except KeyError: + obj = Directory(path) + self.directories[path] = obj + return obj + + def get_free_space(self, path): + from os import statvfs + stat = statvfs(path) + return stat.f_bavail * stat.f_bsize + + def assign_cursor_positions_for_subdirs(self): + """Assign correct cursor positions for subdirectories""" + last_path = None + for path in reversed(self.pathway): + if last_path is None: + last_path = path + continue + + path.move_to_obj(last_path) + last_path = path + + def ensure_correct_pointer(self): + if self.pwd: + self.pwd.correct_pointer() + + def history_go(self, relative): + """Move relative in history""" + if self.history: + self.history.move(relative).go() + + def enter_dir(self, path, history = True): + """Enter given path""" + if path is None: return + path = str(path) + + # get the absolute path + path = normpath(join(self.path, expanduser(path))) + + if not isdir(path): + return + + try: + new_pwd = self.get_directory(path) + except NoDirectoryGiven: + return False + + self.path = path + self.pwd = new_pwd + os.chdir(path) + + self.pwd.load_content_if_outdated() + + # build the pathway, a tuple of directory objects which lie + # on the path to the current directory. + if path == '/': + self.pathway = (self.get_directory('/'), ) + else: + pathway = [] + currentpath = '/' + for dir in path.split('/'): + currentpath = join(currentpath, dir) + pathway.append(self.get_directory(currentpath)) + self.pathway = tuple(pathway) + + self.assign_cursor_positions_for_subdirs() + + # set the current file. + self.pwd.directories_first = self.settings.directories_first + self.pwd.sort_if_outdated() + self.cf = self.pwd.pointed_obj + + if history: + self.history.add(new_pwd) + + return True -- cgit 1.4.1-2-gfad0