diff options
-rw-r--r-- | ranger/container/settingobject.py | 36 | ||||
-rw-r--r-- | ranger/core/main.py | 5 | ||||
-rw-r--r-- | ranger/core/shared.py | 71 |
3 files changed, 51 insertions, 61 deletions
diff --git a/ranger/container/settingobject.py b/ranger/container/settingobject.py index 8fa27a43..28f1c639 100644 --- a/ranger/container/settingobject.py +++ b/ranger/container/settingobject.py @@ -2,9 +2,11 @@ # This software is distributed under the terms of the GNU GPL version 3. from inspect import isfunction -from ranger.ext.signals import SignalDispatcher +from ranger.ext.signals import SignalDispatcher, Signal from ranger.core.shared import FileManagerAware +from ranger.gui.colorscheme import _colorscheme_name_to_class import re +import os ALLOWED_SETTINGS = { 'autosave_bookmarks': bool, @@ -70,6 +72,37 @@ class SettingObject(SignalDispatcher, FileManagerAware): self.signal_bind('setopt.'+name, self._raw_set_with_signal, priority=0.2) + def _sanitize(self, name, value): + if name == 'column_ratios': + # TODO: cover more cases here + if isinstance(value, tuple): + return list(value) + if not isinstance(value, list) or len(value) < 2: + return [1, 1] + else: + return [int(i) if str(i).isdigit() else 1 for i in value] + + elif name == 'colorscheme': + signal = Signal(value=value, previous="Penis", fm=self.fm) + _colorscheme_name_to_class(signal) + return signal.value + + elif name == 'preview_script': + if isinstance(value, str): + result = os.path.expanduser(value) + if os.path.exists(result): + return result + return None + + elif name == 'use_preview_script': + if self._settings['preview_script'] is None and value \ + and self.fm.ui.is_on: + self.fm.notify("Preview script undefined or not found!", + bad=True) + + # fallback: + return value + def set(self, name, value, path=None): assert name in ALLOWED_SETTINGS, "No such setting: {0}!".format(name) if name not in self._settings: @@ -139,6 +172,7 @@ class SettingObject(SignalDispatcher, FileManagerAware): __setitem__ = __setattr__ def _raw_set(self, name, value, path): + value = self._sanitize(name, value) if path: if not path in self._localsettings: self._localsettings[path] = dict() diff --git a/ranger/core/main.py b/ranger/core/main.py index 7ad71e96..67377147 100644 --- a/ranger/core/main.py +++ b/ranger/core/main.py @@ -10,6 +10,7 @@ def main(): """initialize objects and run the filemanager""" import locale import ranger.api + from ranger.container.settingobject import SettingObject from ranger.core.shared import FileManagerAware, SettingsAware from ranger.core.fm import FM @@ -52,7 +53,7 @@ def main(): sys.stdout.write(line) return 1 if arg.fail_unless_cd else 0 # COMPAT - SettingsAware._setup(clean=arg.clean) + SettingsAware._setup(SettingObject()) if arg.selectfile: arg.selectfile = os.path.abspath(arg.selectfile) @@ -87,7 +88,7 @@ def main(): try: # Initialize objects fm = FM(paths=targets) - FileManagerAware.fm = fm + FileManagerAware._setup(fm) load_settings(fm, arg.clean) if arg.list_unused_keys: diff --git a/ranger/core/shared.py b/ranger/core/shared.py index 30eb9038..23f1cc24 100644 --- a/ranger/core/shared.py +++ b/ranger/core/shared.py @@ -4,67 +4,22 @@ """Shared objects contain singletons for shared use.""" from ranger.ext.lazy_property import lazy_property -import os.path -class Awareness(object): - pass +class FileManagerAware(object): + """Subclass this to gain access to the global "FM" object.""" + @staticmethod + def _setup(fm): + FileManagerAware.fm = fm + +class SettingsAware(object): + """Subclass this to gain access to the global "SettingObject" object.""" + @staticmethod + def _setup(settings): + SettingsAware.settings = settings -class EnvironmentAware(Awareness): - # This creates an instance implicitly, mainly for unit tests +class EnvironmentAware(object): # COMPAT + """DO NOT USE. This is for backward compatibility only.""" @lazy_property def env(self): from ranger.core.environment import Environment return Environment(".") - -class FileManagerAware(Awareness): - # This creates an instance implicitly, mainly for unit tests - @lazy_property - def fm(self): - from ranger.core.fm import FM - return FM() - -class SettingsAware(Awareness): - # This creates an instance implicitly, mainly for unit tests - @lazy_property - def settings(self): - from ranger.ext.openstruct import OpenStruct - return OpenStruct() - - @staticmethod - def _setup(clean=True): - from ranger.container.settingobject import SettingObject - import ranger - import sys - settings = SettingObject() - - from ranger.gui.colorscheme import _colorscheme_name_to_class - settings.signal_bind('setopt.colorscheme', - _colorscheme_name_to_class, priority=1) - - settings.signal_bind('setopt.column_ratios', - _sanitize_setting_column_ratios, priority=1) - - def after_setting_preview_script(signal): - if isinstance(signal.value, str): - signal.value = os.path.expanduser(signal.value) - if not os.path.exists(signal.value): - signal.value = None - settings.signal_bind('setopt.preview_script', - after_setting_preview_script, priority=1) - def after_setting_use_preview_script(signal): - if signal.fm.settings.preview_script is None and signal.value \ - and signal.fm.ui.is_on: - signal.fm.notify("Preview script undefined or not found!", - bad=True) - settings.signal_bind('setopt.use_preview_script', - after_setting_use_preview_script, priority=1) - - SettingsAware.settings = settings - -def _sanitize_setting_column_ratios(signal): - if isinstance(signal.value, tuple): - signal.value = list(signal.value) - if not isinstance(signal.value, list) or len(signal.value) < 2: - signal.value = [1,1] - else: - signal.value = [int(i) if str(i).isdigit() else 1 for i in signal.value] |