summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/container/bookmarks.py84
1 files changed, 46 insertions, 38 deletions
diff --git a/ranger/container/bookmarks.py b/ranger/container/bookmarks.py
index 305ac4c2..23606028 100644
--- a/ranger/container/bookmarks.py
+++ b/ranger/container/bookmarks.py
@@ -6,10 +6,13 @@ from __future__ import (absolute_import, division, print_function)
 import string
 import re
 import os
+
+from ranger.core.shared import FileManagerAware
+
 ALLOWED_KEYS = string.ascii_letters + string.digits + "`'"
 
 
-class Bookmarks(object):
+class Bookmarks(FileManagerAware):
     """Bookmarks is a container which associates keys with bookmarks.
 
     A key is a string with: len(key) == 1 and key in ALLOWED_KEYS.
@@ -39,9 +42,8 @@ class Bookmarks(object):
 
     def load(self):
         """Load the bookmarks from path/bookmarks"""
-        try:
-            new_dict = self._load_dict()
-        except OSError:
+        new_dict = self._load_dict()
+        if new_dict is None:
             return
 
         self._set_dict(new_dict, original=new_dict)
@@ -110,12 +112,10 @@ class Bookmarks(object):
 
         Useful if two instances are running which define different bookmarks.
         """
-
-        try:
-            real_dict = self._load_dict()
-            real_dict_copy = real_dict.copy()
-        except OSError:
+        real_dict = self._load_dict()
+        if real_dict is None:
             return
+        real_dict_copy = real_dict.copy()
 
         for key in set(self.dct) | set(real_dict):
             # set some variables
@@ -152,46 +152,54 @@ class Bookmarks(object):
         self.update()
         if self.path is None:
             return
-        if os.access(self.path, os.W_OK):
-            fobj = open(self.path + ".new", 'w')
-            for key, value in self.dct.items():
-                if isinstance(key, str) and key in ALLOWED_KEYS:
-                    fobj.write("{0}:{1}\n".format(str(key), str(value)))
 
-            fobj.close()
+        path_new = self.path + '.new'
+        try:
+            fobj = open(path_new, 'w')
+        except OSError as ex:
+            self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+            return
+        for key, value in self.dct.items():
+            if isinstance(key, str) and key in ALLOWED_KEYS:
+                fobj.write("{0}:{1}\n".format(str(key), str(value)))
+        fobj.close()
+
+        try:
             old_perms = os.stat(self.path)
-            try:
-                os.chown(self.path + ".new", old_perms.st_uid, old_perms.st_gid)
-                os.chmod(self.path + ".new", old_perms.st_mode)
-            except OSError:
-                pass
-            os.rename(self.path + ".new", self.path)
+            os.chown(path_new, old_perms.st_uid, old_perms.st_gid)
+            os.chmod(path_new, old_perms.st_mode)
+            os.rename(path_new, self.path)
+        except OSError as ex:
+            self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+            return
+
         self._update_mtime()
 
     def _load_dict(self):
-        dct = {}
-
         if self.path is None:
-            return dct
+            return {}
 
         if not os.path.exists(self.path):
             try:
-                fobj = open(self.path, 'w')
-            except OSError:
-                raise
-            fobj.close()
+                with open(self.path, 'w') as fobj:
+                    pass
+            except OSError as ex:
+                self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+                return None
 
-        if os.access(self.path, os.R_OK):
+        try:
             fobj = open(self.path, 'r')
-            for line in fobj:
-                if self.load_pattern.match(line):
-                    key, value = line[0], line[2:-1]
-                    if key in ALLOWED_KEYS:
-                        dct[key] = self.bookmarktype(value)
-            fobj.close()
-            return dct
-        else:
-            raise OSError('Cannot read the given path')
+        except OSError as ex:
+            self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+            return None
+        dct = {}
+        for line in fobj:
+            if self.load_pattern.match(line):
+                key, value = line[0], line[2:-1]
+                if key in ALLOWED_KEYS and not os.path.isfile(value):
+                    dct[key] = self.bookmarktype(value)
+        fobj.close()
+        return dct
 
     def _set_dict(self, dct, original):
         if original is None:
'alt'>
03ed2a43 ^
240394a4 ^
240394a4 ^

dc4216a5 ^
7dc8fef8 ^
9e89f023 ^
36e4e71e ^
20ab9343 ^

20ab9343 ^





4ea0f69a ^

240394a4 ^
7dc8fef8 ^
36e4e71e ^

4a383291 ^
78a7d762 ^
e952d6cb ^
78a7d762 ^
03ed2a43 ^
4ea0f69a ^

7838675f ^

03ed2a43 ^
6ff9bb6f ^
03ed2a43 ^
7838675f ^

2c8cb95f ^
03ed2a43 ^



7838675f ^
03ed2a43 ^
2c8cb95f ^
03ed2a43 ^







240394a4 ^



240394a4 ^
03ed2a43 ^
240394a4 ^

03ed2a43 ^


240394a4 ^

03ed2a43 ^


2c8cb95f ^
03ed2a43 ^
240394a4 ^

e5fb3d74 ^
240394a4 ^

03ed2a43 ^

7dc8fef8 ^


03ed2a43 ^


7b33b517 ^
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113