From 0416393c53bddb0a73f4d1c5930172e6c2114abc Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 1 Mar 2013 12:53:27 +0100 Subject: simplify sanitizing of settings and core.shared module --- ranger/container/settingobject.py | 36 +++++++++++++++++++- ranger/core/main.py | 5 +-- 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] -- cgit 1.4.1-2-gfad0 hut <hut@lavabit.com> 2010-03-12 00:38:07 +0100 committer hut <hut@lavabit.com> 2010-03-12 00:46:48 +0100 misc changes, make install' href='/akspecs/ranger/commit/Makefile?id=94c5d83e6752cf6506c78c9454c08b9f3df9371c'>94c5d83e ^
582f3519 ^
3a1e1f28 ^
f473be92 ^
3a1e1f28 ^

2c5ea01d ^
5fca2a0b ^
b0a216f5 ^
2c5ea01d ^
b0a216f5 ^



3a1e1f28 ^
b0a216f5 ^
e7758eba ^
b0a216f5 ^

5fca2a0b ^
b0b3af33 ^


b0a216f5 ^
b0b3af33 ^

2c5ea01d ^
5fca2a0b ^
b0b3af33 ^
2c5ea01d ^
582f3519 ^
b06433bc ^
582f3519 ^

94c5d83e ^
dee6cfa6 ^
e9e4b4ff ^
b0a216f5 ^
76612b05 ^
a082b66a ^
b0a216f5 ^
e9e4b4ff ^





612b8b8b ^
e9e4b4ff ^
5fca2a0b ^
f473be92 ^
5fca2a0b ^
9cf43c17 ^
5fca2a0b ^

c7720fff ^


a6836d5d ^

c7720fff ^
8d21b83c ^


e9e4b4ff ^
25a4162d ^
e9e4b4ff ^
0c2c782d ^
636d9393 ^
b0a216f5 ^
c6afc196 ^

c0d63e78 ^


c6afc196 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89