Package plugins :: Package core :: Package console :: Module main
[hide private]
[frames] | no frames]

Source Code for Module plugins.core.console.main

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov <syhpoon@syhpoon.name> 2008-2009 
  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.core.plugins import BasePlugin 
 18  from libxyz.core import dsl 
 19  from libxyz.core.utils import bstring 
 20  from libxyz.core import Queue 
 21  from libxyz.ui import lowui 
 22   
 23  import libxyz.ui as ui 
 24   
25 -class XYZPlugin(BasePlugin):
26 "Plugin console" 27 28 NAME = u"console" 29 AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>" 30 VERSION = u"0.1" 31 BRIEF_DESCRIPTION = u"Interactive management console" 32 FULL_DESCRIPTION = u"Provides interactive management console" 33 NAMESPACE = u"core" 34
35 - def __init__(self, xyz):
36 super(XYZPlugin, self).__init__(xyz) 37 38 self.attr = lambda x: self.xyz.skin.get_palette(u"plugin.console", x) 39 40 self._keys= ui.Keys() 41 self._index = 1 42 self.output = [] 43 self.edit = lowui.Edit(self.conf["prompt"], wrap="clip") 44 self._input = lowui.AttrWrap(self.edit, self.attr("input")) 45 self._header = lowui.AttrWrap(lowui.Text(_(u"Management console")), 46 self.attr("header")) 47 self._history = Queue(self.conf["history_depth"]) 48 49 self.export(self.show)
50 51 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 52
53 - def show(self):
54 """ 55 Show console window 56 """ 57 58 def _get_cmd(k): 59 """ 60 Fetch previous command from history 61 """ 62 63 _i = -1 if k == self._keys.UP else 1 64 65 _pos = len(self._history) - 1 + (self._index + _i) 66 67 if _pos < 0: 68 return None 69 elif _pos > len(self._history): 70 return None 71 else: 72 self._index += _i 73 74 if _pos == len(self._history): 75 return "" 76 77 try: 78 cmd = self._history[_pos] 79 except Exception: 80 return None 81 else: 82 return cmd
83 84 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 85 86 _stop = False 87 88 while True: 89 walker = lowui.SimpleListWalker(self.output) 90 walker.focus = len(walker) - 1 91 lbox = lowui.AttrWrap(lowui.ListBox(walker), self.attr("output")) 92 93 console = lowui.Frame(lbox, header=self._header, 94 footer=self._input, focus_part='footer') 95 dim = self.xyz.screen.get_cols_rows() 96 97 self.xyz.screen.draw_screen(dim, console.render(dim, True)) 98 99 data = self.xyz.input.get() 100 101 for k in data: 102 if k in (self._keys.UP, self._keys.DOWN): 103 cmd = _get_cmd(k) 104 105 if cmd is not None: 106 self.edit.set_edit_text("") 107 self.edit.insert_text(cmd) 108 elif k == self._keys.ENTER: 109 self._index = 1 110 chunk = self.edit.get_edit_text() 111 self.edit.set_edit_text("") 112 compiled = None 113 114 if not chunk: 115 continue 116 117 self._history.push(chunk) 118 119 self._write("> %s" % chunk) 120 121 try: 122 compiled = compile(chunk, "<input>", "eval") 123 except Exception, e: 124 self._write(str(e)) 125 break 126 else: 127 # Incomplete 128 if compiled is None: 129 break 130 else: 131 chunk = "" 132 try: 133 self._write( 134 eval(compiled, dsl.XYZ.get_env())) 135 except Exception, e: 136 self._write(str(e)) 137 elif k == self._keys.ESCAPE: 138 _stop = True 139 break 140 else: 141 self._input.keypress((dim[0],), k) 142 143 if _stop: 144 break
145 146 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 147
148 - def _write(self, msg):
149 """ 150 Write text to output 151 """ 152 153 self.output.extend([lowui.Text(x) for x in bstring(msg).split("\n")])
154