about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2013-03-16 19:13:46 +0100
committerhut <hut@lavabit.com>2013-03-16 19:18:25 +0100
commit1f5a29d42745f0ae7c37a96339e8df0185d5f3dd (patch)
treee0b1e7e79959ed0c7d45ed32b53cd6c96931bf5e
parent8a5944e1bd52dd749c34a5b4eda401902531175a (diff)
downloadranger-1f5a29d42745f0ae7c37a96339e8df0185d5f3dd.tar.gz
settings: sanitize in separate signal with higher priority
Some signals may require the sanitized value BEFORE it is actually set
with settings._raw_set. We need to separate sanitizing and _raw_set into
separate signals so other signals can be bound in between those two.
-rw-r--r--ranger/container/settings.py23
1 files changed, 11 insertions, 12 deletions
diff --git a/ranger/container/settings.py b/ranger/container/settings.py
index 2ca77d07..85480d1b 100644
--- a/ranger/container/settings.py
+++ b/ranger/container/settings.py
@@ -72,29 +72,32 @@ class Settings(SignalDispatcher, FileManagerAware):
         self.__dict__['_settings'] = dict()
         for name in ALLOWED_SETTINGS:
             self.signal_bind('setopt.'+name,
+                    self._sanitize, priority=1.0)
+            self.signal_bind('setopt.'+name,
                     self._raw_set_with_signal, priority=0.2)
 
-    def _sanitize(self, name, value):
+    def _sanitize(self, signal):
+        name, value = signal.setting, signal.value
         if name == 'column_ratios':
             # TODO: cover more cases here
             if isinstance(value, tuple):
-                return list(value)
+                signal.value = list(value)
             if not isinstance(value, list) or len(value) < 2:
-                return [1, 1]
+                signal.value = [1, 1]
             else:
-                return [int(i) if str(i).isdigit() else 1 for i in value]
+                signal.value = [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
+                    signal.value = result
+                else:
+                    signal.value = None
 
         elif name == 'use_preview_script':
             if self._settings['preview_script'] is None and value \
@@ -102,9 +105,6 @@ class Settings(SignalDispatcher, FileManagerAware):
                 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:
@@ -183,7 +183,6 @@ class Settings(SignalDispatcher, FileManagerAware):
     __setitem__ = __setattr__
 
     def _raw_set(self, name, value, path):
-        value = self._sanitize(name, value)
         if path:
             if not path in self._localsettings:
                 try:
tree' href='/steew/rpncalc/commit/main.c?id=bedbeb372f1d3a36b10d8737a9199c1fe4827e4f'>bedbeb3 ^
fe63146 ^
bedbeb3 ^




fe63146 ^

bedbeb3 ^

fe63146 ^

bedbeb3 ^

fe63146 ^

bedbeb3 ^

fe63146 ^

bedbeb3 ^
fe63146 ^
bedbeb3 ^
2dba790 ^
bedbeb3 ^

7807b47 ^
bedbeb3 ^
2dba790 ^




fe63146 ^
d465d6e ^
bedbeb3 ^
fe63146 ^
d465d6e ^
bedbeb3 ^
d465d6e ^
fe63146 ^

d465d6e ^

fe63146 ^

d465d6e ^
fe63146 ^
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
90
91
92
93
94
95
                 



                   
                   
                 
                
                  
















                                                          
                
                      
                 





                                                            
                        
                                                               
                                   

                                              



                                                          


                                                                                                     
                               




                                                      

                                                                                       

                                     

                                                                                       

                                     

                                                                                       

                                     

                                                                                       
                          
               
                                 
                                                             

                                                
                          
                  




                                                                                           
                             
       
                                    
                                             
     
    
                      

                                                      

     

                  
 
 
#include <math.h>
#include <stdio.h>
#include "state.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
#include "cmds.h"
#include <err.h>
#include <errno.h>

#define BUF_SIZE 50

/* rpncalc: a simple reverse polish notation calculator */
int main() {
  
  /* initialize state struct and its values */
  state s;
  /* default input buffer is stdin */
  s.defbuf = stdin;
  /* default output buffer is stdout */
  s.defout = stdout;
  s.last_op = 0;
  /* set default interactive prompt */
  s.prompt = "[%d]> ";
  stack stack = {{0}, 0};
  /* initialize value stack */
  s.stk = stack;
  s.command_count = 0;
  init_state(&s);
  
  /* start reading from defbuf */
  char buf[BUF_SIZE];
  while(1){
    fprintf(s.defout, s.prompt, s.command_count, s.last_op);
    s.last_op = 0;
    char *endptr = NULL;
    if (s.stk.count == STACK_SIZE) err(1, "exceeded stk size");
    fgets(buf, BUF_SIZE, s.defbuf);
    buf[strcspn(buf, "\n")] = 0;
    double interpreted = strtod(buf, &endptr);
    if (errno == ERANGE) {
      fprintf(s.defout,"sorry, this number is too big\n");
      continue;
    }
    if (interpreted == (double)0 && endptr == buf) { /* strtod returned the char it could not read */
      TYPE t = discriminate(buf);
      if (t == OPERATOR ) {
	if (s.stk.count <= 1) {
	  fprintf(s.defout, "not enough arguments\n");
	  continue;
	}
	char operator = buf[0];
	if (operator == '+') {
	  s.stk.val[s.stk.count-2] = s.stk.val[s.stk.count-1]+s.stk.val[s.stk.count-2];
	  s.stk.val[s.stk.count--] = 0; /* lower count by 1 */
	  s.last_op = '+';
	} else if (operator == '-') {
	  s.stk.val[s.stk.count-2] = s.stk.val[s.stk.count-2]-s.stk.val[s.stk.count-1];
	  s.stk.val[s.stk.count--] = 0; /* lower count by 1 */
	  s.last_op = '-';
	} else if (operator == '*') {
	  s.stk.val[s.stk.count-2] = s.stk.val[s.stk.count-2]*s.stk.val[s.stk.count-1];
	  s.stk.val[s.stk.count--] = 0; /* lower count by 1 */
	  s.last_op = '*';
	} else if (operator == '/') {
	  s.stk.val[s.stk.count-2] = s.stk.val[s.stk.count-2]/s.stk.val[s.stk.count-1];
	  s.stk.val[s.stk.count--] = 0; /* lower count by 1 */
	  s.last_op = '/';
	} else;
      } else if (t == FUNCTION) {
	/* check for special commands, else pass to exec() */
	if (strcmp(buf, "quit") == 0) {
	  fprintf(s.defout, "quitting, bye!\n");
	  free (s.sorted);
	  exit(0);
	} else if (strcmp(buf, "list") == 0) {
	  for (int i = 0; s.sorted[i].name != 0; i++) {
	    fprintf(s.defout, "[%s]\t->\t%s\n", s.sorted[i].name, s.sorted[i].description);
	    continue;
	  }
	} else exec(buf, &s);
      }
    } else { /* we found a number */
      s.stk.val[s.stk.count++] = interpreted;
    }
    
    s.command_count++;
    for (int i = 0; i < s.stk.count; i++) {
      fprintf(s.defout,"%d → %f\n",i, s.stk.val[i]);
    }
  }
  /* free stuff */
  free (s.sorted);
}