1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from libxyz.ui import lowui
18 from libxyz.ui import Keys
19 from libxyz.ui import XYZListBox
20 from libxyz.ui import NumEntry
21
22 from libxyz.core.plugins import BasePlugin
23 from libxyz.core import UserData
24 from libxyz.core.utils import ustring, bstring
25 from libxyz.parser import FlatParser
26 from libxyz.parser import ParsedData
27
28 from libxyz.exceptions import XYZRuntimeError
29 from libxyz.exceptions import ParseError
30
32 "Bookmarks - frequently used directories list"
33
34 NAME = u"bookmarks"
35 AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>"
36 VERSION = u"0.1"
37 BRIEF_DESCRIPTION = _(u"Frequently used directories")
38 FULL_DESCRIPTION = u""
39 NAMESPACE = u"ui"
40 MIN_XYZ_VERSION = None
41 DOC = None
42 HOMEPAGE = u"http://xyzcmd.syhpoon.name/"
43
56
57
58
60 """
61 Add new bookmark entry
62 If name is not specified, path is used instead
63 """
64
65 if name is None:
66 name = path
67
68 path = ustring(path)
69 name = ustring(name)
70
71 _data = self._load_data()
72
73 if _data is not None:
74 _data[name] = path
75
76 return self._save_data(_data)
77
78
79
81 """
82 Delete saved bookmark entry by name
83 """
84
85 name = ustring(name)
86
87 _data = self._load_data()
88
89 if _data is not None and name in _data:
90 del(_data[name])
91 return self._save_data(_data)
92
93
94
96 """
97 Get bookmark path by name
98 """
99
100 name = ustring(name)
101
102 _data = self._load_data()
103
104 if _data is not None and name in _data:
105 return _data[name]
106
107 return None
108
109
110
112 """
113 Show currently saved bookmarks and chdir to one of them if needed
114 """
115
116 def _enter_cb(num):
117 if num >= len(_bookmarks):
118 return
119
120 _chdir(_bookmarks.index(num))
121
122
123
124 _bookmarks = self._load_data()
125
126 if _bookmarks is None:
127 return
128
129 _chdir = self.xyz.pm.from_load(u":sys:panel", u"chdir")
130
131 _sel_attr = self.xyz.skin.attr(XYZListBox.resolution, u"selected")
132 _wdata = []
133
134 i = 0
135
136 for b in _bookmarks:
137 _wdata.append(NumEntry(b, _sel_attr, i, enter_cb=_enter_cb))
138 i += 1
139
140 _walker = lowui.SimpleListWalker(_wdata)
141 _dim = tuple([x - 2 for x in self.xyz.screen.get_cols_rows()])
142 _ek = [self._keys.ENTER]
143
144 XYZListBox(self.xyz, self.xyz.top, _walker, _(u"Bookmarks"),
145 _dim).show(exit_keys=_ek)
146
147
148
150 """
151 Load and parse saved bookmarks from file
152 """
153
154 try:
155 _file = self._ud.openfile(self._bmfile, "r", self._bmsubdir)
156 except XYZRuntimeError, e:
157 xyzlog.info(_(u"Unable to open bookmarks file: %s") %
158 unicode(e))
159 return ParsedData()
160
161 _parser = FlatParser()
162
163 try:
164 return _parser.parse(_file)
165 except ParseError, e:
166 xyzlog.error(_(u"Error parsing bookmarks file: %s") %
167 unicode(e))
168 _file.close()
169
170 return None
171
172
173
175 """
176 Save data to bookmarks file
177 data is a mapping: {name: path}
178 """
179
180 try:
181 _file = self._ud.openfile(self._bmfile, "w", self._bmsubdir)
182 except XYZRuntimeError, e:
183 xyzlog.info("Unable to open bookmarks file: %s" % unicode(e))
184 return None
185
186 for _name, _path in data.iteritems():
187 _file.write('"%s": "%s"\n' %
188 (bstring(_name), bstring(_path)))
189
190 _file.close()
191
192 return True
193