Package plugins :: Package ui :: Package bookmarks :: Module main

[hide private]
[frames] | no frames]

# Source Code for Module plugins.ui.bookmarks.main

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov <syhpoon@syhpoon.name> 2008
      4  #
      5  # This file is part of XYZCommander.
      6  # XYZCommander is free software: you can redistribute it and/or modify
      7  # it under the terms of the GNU Lesser Public License as published by
      8  # the Free Software Foundation, either version 3 of the License, or
      9  # (at your option) any later version.
     10  # XYZCommander is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  # GNU Lesser Public License for more details.
     14  # You should have received a copy of the GNU Lesser Public License
     15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
     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
     31 -class XYZPlugin(BasePlugin):
     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
     44 -    def __init__(self, xyz):
     45          super(XYZPlugin, self).__init__(xyz)
     46
     47          self.export(self.add_bookmark)
     48          self.export(self.del_bookmark)
     49          self.export(self.show_bookmarks)
     50          self.export(self.get_path)
     51
     52          self._bmsubdir = "data"
     53          self._bmfile = "bookmarks"
     54          self._ud = UserData()
     55          self._keys = Keys()
     56
     57      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     58
     59 -    def add_bookmark(self, path, name=None):
     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
     80 -    def del_bookmark(self, name):
     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
     95 -    def get_path(self, name):
     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
    111 -    def show_bookmarks(self):
    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
    149 -    def _load_data(self):
    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                          ustring(str(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                           ustring(str(e)))
    168          _file.close()
    169
    170          return None
    171
    172      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    173
    174 -    def _save_data(self, data):
    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" % ustring(str(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