Package plugins :: Package misc :: Package where :: Module main |
[hide private]
[frames] | no frames]

# Source Code for Module plugins.misc.where.main

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov <syhpoon@syhpoon.name> 2009
      4  #
      5
      6  import os
      7
      8  from libxyz.core.plugins import BasePlugin
      9  from libxyz.core import UserData
     10
     11  from libxyz.exceptions import XYZRuntimeError
     12
     13 -class XYZPlugin(BasePlugin):
     14      "Plugin where"
     15
     16      NAME = u"where"
     17      AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>"
     18      VERSION = u"0.2"
     19      BRIEF_DESCRIPTION = _(u"Save panels locations")
     20      FULL_DESCRIPTION = _(u"When starting load previously saved locations")
     21      NAMESPACE = u"misc"
     22      MIN_XYZ_VERSION = 4
     23      DOC = None
     24      HOMEPAGE = "http://xyzcmd.syhpoon.name/"
     25      EVENTS = None
     26
     27 -    def __init__(self, xyz):
     28          super(XYZPlugin, self).__init__(xyz)
     29          self._ud = UserData()
     30          self._wfile = "where"
     31
     32          self.xyz.hm.register("event:startup", self.load)
     33          self.xyz.hm.register("event:shutdown", self.save)
     34
     35          self.export(self.save)
     36          self.export(self.load)
     37
     38      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     39
     40 -    def load(self):
     41          """
     42          Restore locations on startup.
     43          File format is following:
     44          /panel-1/path
     45          /panel-2/path
     46          <Number of tabs in panel-1>
     47          <Active tab index in panel-1>
     48          <Active tab selected name in panel-1>
     49          panel-1 tab-1 path
     50          panel-1 tab-1 selected
     51          panel-1 tab-n path
     52          panel-1 tab-n selected
     53          <Number of tabs in panel-2>
     54          <Active tab index in panel-2>
     55          <Active tab selected name in panel-2>
     56          panel-2 tab-1 path
     57          panel-2 tab-1 selected
     58          panel-2 tab-n path
     59          panel-2 tab-n selected
     60          """
     61
     62          chdir = self.xyz.pm.from_load(":sys:panel", "chdir")
     63          new_tab = self.xyz.pm.from_load(":sys:panel", "new_tab")
     64          select = self.xyz.pm.from_load(":sys:panel", "select")
     65          switch = self.xyz.pm.from_load(":sys:panel", "switch_tab")
     66
     67          def restore_tabs(d, num, active):
     68              i = 0
     69
     70              while i < num:
     71                  tab_path = d[i].rstrip()
     72                  tab_selected = d[i + 1].rstrip()
     73
     74                  chdir(tab_path, active=active)
     75                  select(tab_selected, active=active)
     76
     77                  if i < num - 2:
     78                      new_tab(active=active)
     79
     80                  i += 2
     81
     82              return i
     83
     84          #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     85
     86          f = None
     87          try:
     88              f = self._ud.openfile(self._wfile, "r", "data")
     89              data = [x.rstrip() for x in f.readlines()]
     90              act = data[0]
     91              inact = data[1]
     92
     93              chdir(act)
     94              chdir(inact, active=False)
     95
     96              tabsnum = int(data[2]) * 2
     97              active_tab = int(data[3])
     98              selected = data[4]
     99
    100              data = data[5:]
    101
    102              restored = restore_tabs(data, tabsnum, True)
    103
    104              tabsnum = int(data[restored]) * 2
    105              iactive_tab = int(data[restored + 1])
    106              iselected = data[restored + 2]
    107
    108              data = data[restored + 3:]
    109
    110              restore_tabs(data, tabsnum, False)
    111
    112              switch(iactive_tab, active=False)
    113              switch(active_tab)
    114              select(iselected, active=False)
    115              select(selected)
    116          except IOError:
    117              pass
    118
    119          if f:
    120              f.close()
    121
    122      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    123
    124 -    def save(self):
    125          """
    126          Save locations on shutdown
    127          """
    128
    129          panel = self.xyz.pm.load(":sys:panel")
    130
    131          act = panel.cwd()
    132          inact = panel.cwd(active=False)
    133          tabs = panel.get_tabs()
    134          inacttabs = panel.get_tabs(active=False)
    135
    136          f = None
    137
    138          try:
    139              data = [act, inact]
    140              data.append(str(len(tabs)))
    141              data.append(str(panel.active_tab()))
    142              data.append(str(panel.get_selected().name))
    143
    144              for tab_path, tab_selected in tabs:
    145                  if tab_selected is None:
    146                      tab_selected = os.path.sep
    147
    148                  data.extend([tab_path, tab_selected])
    149
    150              data.append(str(len(inacttabs)))
    151              data.append(str(panel.active_tab(active=False)))
    152              data.append(str(panel.get_selected(active=False).name))
    153
    154              for itab_path, itab_selected in inacttabs:
    155                  if itab_selected is None:
    156                      itab_selected = os.path.sep
    157
    158                  data.extend([itab_path, itab_selected])
    159
    160              f = self._ud.openfile(self._wfile, "w", "data")
    161
    162              f.write("\n".join(data))
    163          except XYZRuntimeError, e:
    164              xyzlog.info(_(u"Unable to open where data file: %s")
    165                          % ustring(str(e)))
    166          if f:
    167              f.close()
    168