Package flumotion :: Package admin :: Package text :: Module greeter
[hide private]

Source Code for Module flumotion.admin.text.greeter

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  """interface displayed when you first run the cursor interface""" 
 19   
 20  import curses 
 21   
 22  import gobject 
 23  from twisted.internet import reactor 
 24  from zope.interface import implements 
 25   
 26  from flumotion.admin.connections import getRecentConnections 
 27  from flumotion.admin.text import misc_curses 
 28  from flumotion.admin.text import connection 
 29  from flumotion.common.connection import PBConnectionInfo 
 30  from flumotion.common import log 
 31  from flumotion.twisted import flavors, pb as fpb 
 32   
 33  __version__ = "$Rev$" 
 34   
 35   
36 -class AdminTextGreeter(log.Loggable, gobject.GObject, misc_curses.CursesStdIO):
37 implements(flavors.IStateListener) 38 39 logCategory = 'admintextgreeter' 40
41 - def __init__(self, stdscr):
42 self.stdscr = stdscr 43 self.connections = getRecentConnections() 44 self.current_connection = 0 45 self.state = 0 46 self.current_input = '' 47 curses.curs_set(0) 48 self.entries = ['', 'Hostname', 'Port', 'Secure?', 49 'Username', 'Password'] 50 self.inputs = ['', 'localhost', '7531', 'Yes', 'user', '']
51
52 - def show(self):
53 self.stdscr.addstr(0, 0, "Please choose a connection:") 54 55 cury = 3 56 maxyx = self.stdscr.getmaxyx() 57 self.debug("mayx: %d, %d", maxyx[0], maxyx[1]) 58 for c in self.connections: 59 self.debug("cury: %d", cury) 60 if cury - 3 == self.current_connection: 61 self.stdscr.addstr(cury, 10, c.name, curses.A_REVERSE) 62 else: 63 self.stdscr.addstr(cury, 10, c.name) 64 if cury + 10 > maxyx[0]: 65 break 66 cury = cury + 1 67 self.displayed_connections = cury - 3 68 if cury - 3 == self.current_connection: 69 self.stdscr.addstr(cury + 1, 10, "New connection...", 70 curses.A_REVERSE) 71 else: 72 self.stdscr.addstr(cury + 1, 10, "New connection...") 73 self.stdscr.refresh()
74
76 cury = self.displayed_connections + 5 + self.state 77 if self.state > 0 and self.state < 5: 78 self.stdscr.addstr(cury, 10, "%s: %s" % (self.entries[self.state], 79 self.current_input)) 80 elif self.state == 5: 81 # password entry 82 self.stdscr.addstr(cury, 10, "%s: " % self.entries[self.state]) 83 else: 84 self.stdscr.move(cury, 10) 85 self.stdscr.clrtobot() 86 self.stdscr.refresh()
87
88 - def connectionLost(self, failure):
89 pass
90
91 - def doRead(self):
92 c= self.stdscr.getch() 93 if self.state == 0: 94 if c == curses.KEY_DOWN: 95 if self.current_connection >= self.displayed_connections: 96 self.current_connection = 0 97 else: 98 self.current_connection = self.current_connection + 1 99 self.show() 100 elif c == curses.KEY_UP: 101 if self.current_connection == 0: 102 self.current_connection = self.displayed_connections 103 else: 104 self.current_connection = self.current_connection - 1 105 self.show() 106 elif c == curses.KEY_ENTER or c == 10: 107 # if new connection, ask for username, password, hostname etc. 108 if self.current_connection == self.displayed_connections: 109 curses.curs_set(1) 110 self.current_input = self.inputs[1] 111 self.state = 1 112 self.display_current_input_line() 113 else: 114 # ok a recent connection has been selected 115 curses.curs_set(1) 116 c = self.connections[self.current_connection] 117 info = c.info 118 reactor.removeReader(self) 119 connection.connect_to_manager(self.stdscr, info) 120 else: 121 if c == curses.KEY_ENTER or c == 10: 122 if self.state < 6: 123 self.inputs[self.state] = self.current_input 124 if self.state < 5: 125 self.current_input = self.inputs[self.state+1] 126 self.state = self.state + 1 127 self.display_current_input_line() 128 else: 129 # connect 130 reactor.removeReader(self) 131 try: 132 port = int(self.inputs[2]) 133 except ValueError: 134 port = 7531 135 info = PBConnectionInfo(self.inputs[1], port, 136 self.inputs[3] == 'Yes', fpb.Authenticator( 137 username=self.inputs[4], password=self.inputs[5])) 138 139 connection.connect_to_manager(self.stdscr, info) 140 pass 141 elif c == curses.KEY_BACKSPACE or c == 127: 142 self.current_input = self.current_input[:-1] 143 self.display_current_input_line() 144 elif c == curses.KEY_UP: 145 if self.state > 0: 146 self.current_input = self.inputs[self.state-1] 147 self.state = self.state - 1 148 if self.state == 0: 149 # turn off cursor 150 curses.curs_set(0) 151 self.display_current_input_line() 152 elif c == curses.KEY_DOWN: 153 pass 154 else: 155 self.current_input = self.current_input + chr(c) 156 self.display_current_input_line()
157