Python - Putty embebido en PyGtk3

 
Vista:

Putty embebido en PyGtk3

Publicado por Roberto Carlos Guevara (1 intervención) el 25/01/2016 21:07:49
Hola, estoy desarrollando una app para administrar la aplicacion Putty (SSH and telnet client) en solapas, puesto que no hay ninguna app parecida en linux (en windows hay varias) .
Soy principiante, por lo que mucho de mi codigo seguro esta mal, pero lo importante ahora es que ahora ya puedo embeber la aplicacion en un objeto StackSwitcher, y las nuevas terminales tambien. El tema es que una vez abierto el putty no tengo el foco inmediato en el. Intente usar la funcion grab_focus() y no hace nada, tengo que tocar la tecla de direcion ABAJO, 2 veces para que me de el foco. Mi duda tambien es que si el "widget" en el que esta embebido es el que trato como socket o es un sub-objeto, de cualquier manera tengo muy poca documentacion sobre estos objetos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from gi.repository import Gtk
from gi.repository import Gio
import sys
import os
import urllib.parse
import shlex, subprocess
import time
 
 
class VentanaPrincipal(Gtk.Window):
	def __init__(self):
		Gtk.Window.__init__(self, title="Putty Tray")
		self.set_border_width(10)
		self.set_default_size(800, 600)
 
		self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
		self.add(self.vbox)
		self.stack = Gtk.Stack()
		self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
		self.stack.set_transition_duration(1000)
 
		self.stack_switcher = Gtk.StackSwitcher()
		self.stack_switcher.set_stack(self.stack)
 
		self.vbox.pack_start(self.stack_switcher, False, True, 0)
		self.vbox.pack_start(self.stack, True, True, 0)
 
	def plug_event(self, garb):
		print("Un putty fue insertado")
 
	def addPutty(self, pid, session):
 
		print("Putty window_id: " + str(pid))
		socket = Gtk.Socket()
		socket.connect("plug-added", self.plug_event)
		self.stack.add_titled(socket, session, session)
 
		socket.add_id(pid)
 
		#self.stack.grab_focus()
		#self.get_toplevel().child_focus(Gtk.DIR_TAB_FORWARD)
		socket.grab_focus()
		self.show_all()
		socket.grab_focus()
 
class IconoTray:
	pids = []
	def __init__(self, iconname):
		self.menu = Gtk.Menu()
 
		APPIND_SUPPORT = 1
		try: from gi.repository import AppIndicator3
		except: APPIND_SUPPORT = 0
 
		if APPIND_SUPPORT == 1:
			self.ind = AppIndicator3.Indicator.new("putty", iconname, AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
			self.ind.set_status (AppIndicator3.IndicatorStatus.ACTIVE)
			self.ind.set_menu(self.menu)
		else:
			self.myStatusIcon = Gtk.StatusIcon()
			self.myStatusIcon.set_from_icon_name(iconname)
			self.myStatusIcon.connect('popup-menu', self.right_click_event_statusicon)
 
	def add_seperator(self):
		aMenuitem = Gtk.SeparatorMenuItem()
		self.menu.append(aMenuitem)
		self.menu.show_all()
 
	def get_tray_menu(self):
		return self.menu
 
	def right_click_event_statusicon(self, icon, button, time):
		self.get_tray_menu()
 
		def pos(menu, aicon):
			return (Gtk.StatusIcon.position_menu(menu, aicon))
 
		self.menu.popup(None, None, pos, icon, button, time)
 
	def display_message_dialog(self, message_type):
		messagedialog = Gtk.MessageDialog(message_format="MessageDialog")
		messagedialog.set_property("message-type", message_type)
 
		messagedialog.run()
		messagedialog.destroy()
 
	def add_menu_item(self, command, title, session=""):
		aMenuitem = Gtk.MenuItem()
		aMenuitem.set_label(title)
		aMenuitem.connect("activate", command, session)
 
		self.menu.append(aMenuitem)
		self.menu.show_all()
 
 
	def add_menu_item_s(self, command, title, session=""):
		aMenuitem = Gtk.MenuItem()
		aMenuitem.set_label(title)
		aMenuitem.connect("activate", command,session)
 
		self.menu_sessions.append(aMenuitem)
		self.menu_sessions.show_all()
		self.menu.show_all()
 
 
	def addpid(self, par, p_session):
		aMenuitem = Gtk.MenuItem()
		aMenuitem.set_label(par)
		#aMenuitem.connect("activate", command, session)
 
		self.menu.append(aMenuitem)
		self.menu.show_all()
 
	def children_pid(self, ppid):
		"""get the list of the children pids of a pid (linux only)"""
		import subprocess as SP
		print("Parent PID: "+str(ppid))
		proc = SP.Popen('ps -o pid,ppid ax | grep "'+str(ppid)+'"', shell=True, stdout=SP.PIPE)
		pidppid  = [x.split() for x in proc.communicate()[0].split(b"\n") if x]
		return list(int(p) for p, pp in pidppid if int(pp) == ppid)
 
	def new_session(self, par, p_session):
 
		if len(self.pids) < 1:
			self.appmain = VentanaPrincipal()
			self.appmain.show_all()
 
		p = subprocess.Popen('/usr/bin/putty -load '+ p_session, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
 
		childrenpid=self.children_pid(p.pid)
		print(childrenpid[0])
		self.pids.append(childrenpid)
 
		#Relacionar PID con el window_id
		#xwininfo -tree -root | grep -i putty        -> All_Windows_ID
		#xprop -id Windows_ID[i] | grep _NET_WM_PID(CARDINAL)
		#_NET_WM_PID(CARDINAL) = 21151
 
		#shell(./find_windows.sh PID)
		print ("PID: " + str(childrenpid[0]))
		time.sleep(1)
		proc = subprocess.Popen("/home/roberto/Projects/puttytray/find_windows.sh "+ str(childrenpid[0]), shell=True, stdout=subprocess.PIPE)
		proc.wait()
		for line in proc.stdout:
			#the real code does filtering here
			print (b"test:" + line.rstrip())
			win_id_hex=line.strip()
		#win_id_hex=os.popen("/home/robeto/Projects/puttytray/find_windows.sh "+ str(childrenpid)).read()
		win_id = int(win_id_hex, 0)
		print("WIN_ID: " + str(win_id))
		self.appmain.addPutty(win_id, p_session)
		self.add_menu_item( self.addpid, str(p.pid), "\""+p_session+"\"")
 
	def add_menu_sessions(self):
		self.menu_sessions = Gtk.Menu()
		submenuitem = Gtk.MenuItem(label="Sessiones")
		submenuitem.set_submenu(self.menu_sessions)
		self.menu.append(submenuitem)
 
	def quit(self, par, p_session):
		Gtk.main_quit()
 
#test/debug stuff below here
def main():
	app = IconoTray("putty") #rhythmbox
 
	app.add_menu_sessions()
	dirputty = "/home/roberto/.putty/sessions"
	onlyfiles = [f for f in os.listdir(dirputty) if os.path.isfile(os.path.join(dirputty, f))]
	for filessessionhtml in onlyfiles:
			filessession = urllib.parse.unquote(filessessionhtml)
			app.add_menu_item_s( app.new_session, filessession, "\""+filessession+"\"")
			#app.add_menu_item_s( app.new_session, filessession, filessession)
 
	app.add_seperator()
	app.add_menu_item( app.quit, "Salir", "Salir")
 
 
	if __name__ == '__main__':
		Gtk.main()
 
main()

Desde ya muchas gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder