예제 #1
0
파일: main.py 프로젝트: me-vlad/xyzcmd
    def __init__(self, xyz):
        super(XYZPlugin, self).__init__(xyz)
        self._ud = UserData()
        self._wfile = "where"

        self.xyz.hm.register("event:startup", self.load)
        self.xyz.hm.register("event:shutdown", self.save)

        self.export(self.save)
        self.export(self.load)
예제 #2
0
파일: main.py 프로젝트: me-vlad/xyzcmd
    def __init__(self, xyz):
        super(XYZPlugin, self).__init__(xyz)

        self.export(self.add_bookmark)
        self.export(self.del_bookmark)
        self.export(self.show_bookmarks)
        self.export(self.get_path)

        self._bmsubdir = "data"
        self._bmfile = "bookmarks"
        self._ud = UserData()
        self._keys = Keys()
예제 #3
0
파일: main.py 프로젝트: me-vlad/xyzcmd
    def __init__(self, xyz):
        super(XYZPlugin, self).__init__(xyz)

        self.export(self.learn_keys)
        self.export(self.delete_keys)
        self.export(self.get_keys)

        self._keysfile = "keycodes"
        self._keyssubdir = "data"
        self._terminal = None

        self._ud = UserData()

        self._keys = uilib.Keys()

        self.keys = (("F1", self._keys.F1),
                     ("F2", self._keys.F2),
                     ("F3", self._keys.F3),
                     ("F4", self._keys.F4),
                     ("F5", self._keys.F5),
                     ("F6", self._keys.F6),
                     ("F7", self._keys.F7),
                     ("F8", self._keys.F8),
                     ("F9", self._keys.F9),
                     ("F10", self._keys.F10),
                     ("F11", self._keys.F11),
                     ("F12", self._keys.F12),
                     ("F13", self._keys.F13),
                     ("F14", self._keys.F14),
                     ("F15", self._keys.F15),
                     ("F16", self._keys.F16),
                     ("F17", self._keys.F17),
                     ("F18", self._keys.F18),
                     ("F19", self._keys.F19),
                     ("F20", self._keys.F20),
                     ("BACKSPACE", self._keys.BACKSPACE),
                     ("END", self._keys.END),
                     ("UP", self._keys.UP),
                     ("DOWN", self._keys.DOWN),
                     ("LEFT", self._keys.LEFT),
                     ("RIGHT", self._keys.RIGHT),
                     ("HOME", self._keys.HOME),
                     ("PAGE UP", self._keys.PAGE_UP),
                     ("PAGE DOWN", self._keys.PAGE_DOWN),
                     ("INSERT", self._keys.INSERT),
                     ("TAB", self._keys.TAB),
                    )
예제 #4
0
파일: main.py 프로젝트: me-vlad/xyzcmd
class XYZPlugin(BasePlugin):
    """
    Terminal keycodes handling
    """

    NAME = u"keycodes"
    AUTHOR = u"Max E. Kuznecov ~syhpoon <*****@*****.**>"
    VERSION = u"0.1"
    NAMESPACE = u"core"

    BRIEF_DESCRIPTION = _(u"Setup terminal keycodes")

    FULL_DESCRIPTION = _(u"keycodes plugin is used to properly "\
                         u"configure terminal keycodes.\n"\
                         u"For each terminal type keycodes are stored "\
                         u"independently. Terminal type determined "\
                         u"by examining "\
                         u"TERM environment variable.")

    HOMEPAGE = u"xyzcmd.syhpoon.name"
    EVENTS = [(u"show",
               _(u"Fires upon showing dialog")),
              ]

    def __init__(self, xyz):
        super(XYZPlugin, self).__init__(xyz)

        self.export(self.learn_keys)
        self.export(self.delete_keys)
        self.export(self.get_keys)

        self._keysfile = "keycodes"
        self._keyssubdir = "data"
        self._terminal = None

        self._ud = UserData()

        self._keys = uilib.Keys()

        self.keys = (("F1", self._keys.F1),
                     ("F2", self._keys.F2),
                     ("F3", self._keys.F3),
                     ("F4", self._keys.F4),
                     ("F5", self._keys.F5),
                     ("F6", self._keys.F6),
                     ("F7", self._keys.F7),
                     ("F8", self._keys.F8),
                     ("F9", self._keys.F9),
                     ("F10", self._keys.F10),
                     ("F11", self._keys.F11),
                     ("F12", self._keys.F12),
                     ("F13", self._keys.F13),
                     ("F14", self._keys.F14),
                     ("F15", self._keys.F15),
                     ("F16", self._keys.F16),
                     ("F17", self._keys.F17),
                     ("F18", self._keys.F18),
                     ("F19", self._keys.F19),
                     ("F20", self._keys.F20),
                     ("BACKSPACE", self._keys.BACKSPACE),
                     ("END", self._keys.END),
                     ("UP", self._keys.UP),
                     ("DOWN", self._keys.DOWN),
                     ("LEFT", self._keys.LEFT),
                     ("RIGHT", self._keys.RIGHT),
                     ("HOME", self._keys.HOME),
                     ("PAGE UP", self._keys.PAGE_UP),
                     ("PAGE DOWN", self._keys.PAGE_DOWN),
                     ("INSERT", self._keys.INSERT),
                     ("TAB", self._keys.TAB),
                    )

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def prepare(self):
        self._terminal = os.getenv("TERM") or "DEFAULT"

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def learn_keys(self):
        """
        Show LearnKeys dialog
        """

        self.fire_event("show")
        _title = u"%s - %s" % (self.NAME, self.VERSION)

        _pressed = self._load_data()

        if self._terminal not in _pressed:
            _pressed[self._terminal] = {}

        _msg = _(u"Please press key %s\nPress ENTER to skip key\n"\
                 u"Press ESCAPE to quit dialog")

        for _label, _key in self.keys:
            _m = _msg % _label
            _p = uilib.MessageBox(self.xyz, self.xyz.top, _m, _title).show()

            if _p == [] or _p[0] == self._keys.ENTER:
                continue

            if _p[0] == self._keys.ESCAPE:
                break

            _cur = _pressed[self._terminal]
            _tkey = tuple(_p)

            if _p[0] != _key or (_tkey in _cur and tuple(_p[0]) !=_cur[_tkey]):
                _cur[_tkey] = _key

        _ask_msg = _(u"Save learned keys?")

        if uilib.YesNoBox(self.xyz, self.xyz.top, _ask_msg, _title).show():
            # Save data
            self._save_data(_pressed)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def delete_keys(self, all=False):
        """
        Delete learned keycodes data.
        If all is True, delete all saved data for all terminal types,
        otherwise delete only current terminal type data.
        """

        if all:
            try:
                self._ud.delfile(self._keysfile, self._keyssubdir)
            except XYZRuntimeError, e:
                pass
        else:
예제 #5
0
파일: main.py 프로젝트: me-vlad/xyzcmd
class XYZPlugin(BasePlugin):
    "Plugin where"

    NAME = u"where"
    AUTHOR = u"Max E. Kuznecov <*****@*****.**>"
    VERSION = u"0.2"
    BRIEF_DESCRIPTION = _(u"Save panels locations")
    FULL_DESCRIPTION = _(u"When starting load previously saved locations")
    NAMESPACE = u"misc"
    MIN_XYZ_VERSION = 4
    DOC = None
    HOMEPAGE = "http://xyzcmd.syhpoon.name/"
    EVENTS = None

    def __init__(self, xyz):
        super(XYZPlugin, self).__init__(xyz)
        self._ud = UserData()
        self._wfile = "where"

        self.xyz.hm.register("event:startup", self.load)
        self.xyz.hm.register("event:shutdown", self.save)

        self.export(self.save)
        self.export(self.load)
        
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def load(self):
        """
        Restore locations on startup.
        File format is following:
        /panel-1/path
        /panel-2/path
        <Number of tabs in panel-1>
        <Active tab index in panel-1>
        <Active tab selected name in panel-1>
        panel-1 tab-1 path
        panel-1 tab-1 selected
        panel-1 tab-n path
        panel-1 tab-n selected
        <Number of tabs in panel-2>
        <Active tab index in panel-2>
        <Active tab selected name in panel-2>
        panel-2 tab-1 path
        panel-2 tab-1 selected
        panel-2 tab-n path
        panel-2 tab-n selected
        """

        chdir = self.xyz.pm.from_load(":sys:panel", "chdir")
        new_tab = self.xyz.pm.from_load(":sys:panel", "new_tab")
        select = self.xyz.pm.from_load(":sys:panel", "select")
        switch = self.xyz.pm.from_load(":sys:panel", "switch_tab")

        def restore_tabs(d, num, active):
            i = 0

            while i < num:
                tab_path = d[i].rstrip()
                tab_selected = d[i + 1].rstrip()

                chdir(tab_path, active=active)
                select(tab_selected, active=active)

                if i < num - 2:
                    new_tab(active=active)
                    
                i += 2

            return i

        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        f = None

        try:
            f = self._ud.openfile(self._wfile, "r", "data")
            data = [x.rstrip() for x in f.readlines()]
            act = data[0]
            inact = data[1]

            chdir(act)
            chdir(inact, active=False)

            tabsnum = int(data[2]) * 2
            active_tab = int(data[3])
            selected = data[4]

            data = data[5:]

            restored = restore_tabs(data, tabsnum, True)

            tabsnum = int(data[restored]) * 2
            iactive_tab = int(data[restored + 1])
            iselected = data[restored + 2]

            data = data[restored + 3:]

            restore_tabs(data, tabsnum, False)

            switch(iactive_tab, active=False)
            switch(active_tab)
            select(iselected, active=False)
            select(selected)
        except Exception:
            pass
        
        if f:
            f.close()

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def save(self):
        """
        Save locations on shutdown
        """

        panel = self.xyz.pm.load(":sys:panel")

        act = panel.cwd()
        inact = panel.cwd(active=False)
        tabs = panel.get_tabs()
        inacttabs = panel.get_tabs(active=False)

        f = None

        try:
            data = [act, inact]
            data.append(str(len(tabs)))
            data.append(str(panel.active_tab()))
            data.append(str(panel.get_selected().name))

            for tab_path, tab_selected in tabs:
                if tab_selected is None:
                    tab_selected = os.path.sep

                data.extend([tab_path, tab_selected])

            data.append(str(len(inacttabs)))
            data.append(str(panel.active_tab(active=False)))
            data.append(str(panel.get_selected(active=False).name))

            for itab_path, itab_selected in inacttabs:
                if itab_selected is None:
                    itab_selected = os.path.sep

                data.extend([itab_path, itab_selected])

            f = self._ud.openfile(self._wfile, "w", "data")

            f.write("\n".join(data))
        except XYZRuntimeError, e:
            xyzlog.info(_(u"Unable to open where data file: %s")
                        % unicode(e))
        if f:
            f.close()
예제 #6
0
파일: main.py 프로젝트: me-vlad/xyzcmd
class XYZPlugin(BasePlugin):
    "Bookmarks - frequently used directories list"

    NAME = u"bookmarks"
    AUTHOR = u"Max E. Kuznecov <*****@*****.**>"
    VERSION = u"0.1"
    BRIEF_DESCRIPTION = _(u"Frequently used directories")
    FULL_DESCRIPTION = u""
    NAMESPACE = u"ui"
    MIN_XYZ_VERSION = None
    DOC = None
    HOMEPAGE = u"http://xyzcmd.syhpoon.name/"

    def __init__(self, xyz):
        super(XYZPlugin, self).__init__(xyz)

        self.export(self.add_bookmark)
        self.export(self.del_bookmark)
        self.export(self.show_bookmarks)
        self.export(self.get_path)

        self._bmsubdir = "data"
        self._bmfile = "bookmarks"
        self._ud = UserData()
        self._keys = Keys()

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def add_bookmark(self, path, name=None):
        """
        Add new bookmark entry
        If name is not specified, path is used instead
        """

        if name is None:
            name = path

        path = ustring(path)
        name = ustring(name)

        _data = self._load_data()

        if _data is not None:
            _data[name] = path

        return self._save_data(_data)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def del_bookmark(self, name):
        """
        Delete saved bookmark entry by name
        """

        name = ustring(name)

        _data = self._load_data()

        if _data is not None and name in _data:
            del(_data[name])
            return self._save_data(_data)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def get_path(self, name):
        """
        Get bookmark path by name
        """

        name = ustring(name)

        _data = self._load_data()

        if _data is not None and name in _data:
            return _data[name]

        return None

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def show_bookmarks(self):
        """
        Show currently saved bookmarks and chdir to one of them if needed
        """

        def _enter_cb(num):
            if num >= len(_bookmarks):
                return

            _chdir(_bookmarks.index(num))

        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        def custom_cb(key, focusw):
            _meth = self.xyz.km.process(key, self.ns.pfull)

            if _meth is not None:
                _meth(focusw.msg)
                return False

            return True

        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        _bookmarks = self._load_data()

        if _bookmarks is None:
            return

        _chdir = self.xyz.pm.from_load(u":sys:panel", u"chdir")

        _sel_attr = self.xyz.skin.attr(XYZListBox.resolution, u"selected")
        _wdata = []

        i = 0

        for b in _bookmarks:
            _wdata.append(NumEntry(b, _sel_attr, i, enter_cb=_enter_cb))
            i += 1

        _walker = lowui.SimpleListWalker(_wdata)
        _dim = tuple([x - 2 for x in self.xyz.screen.get_cols_rows()])
        _ek = [self._keys.ENTER]

        XYZListBox(self.xyz, self.xyz.top, _walker, _(u"Bookmarks"),
                   _dim).show(exit_keys=_ek, custom=custom_cb)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def _load_data(self):
        """
        Load and parse saved bookmarks from file
        """

        try:
            _file = self._ud.openfile(self._bmfile, "r", self._bmsubdir)
        except XYZRuntimeError, e:
            xyzlog.info(_(u"Unable to open bookmarks file: %s") %
                        unicode(e))
            return ParsedData()

        _parser = FlatParser()

        try:
            return _parser.parse(_file)
        except ParseError, e:
            xyzlog.error(_(u"Error parsing bookmarks file: %s") %
                         unicode(e))