working on python dialogs
This commit is contained in:
52
src/ConnectDialog.py
Normal file
52
src/ConnectDialog.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
import gi
|
||||||
|
gi.require_version('Gtk', '3.0')
|
||||||
|
from gi.repository import Gtk
|
||||||
|
|
||||||
|
class ConnectDialog(Gtk.Window):
|
||||||
|
|
||||||
|
host = None
|
||||||
|
port = None
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
Gtk.Window.__init__(self, title="Connect to server")
|
||||||
|
self.connect("delete-event", Gtk.main_quit)
|
||||||
|
|
||||||
|
grid = Gtk.Grid()
|
||||||
|
self.add(grid)
|
||||||
|
|
||||||
|
self.host_label = Gtk.Label('Hostname or IP')
|
||||||
|
|
||||||
|
self.host_input = Gtk.Entry();
|
||||||
|
self.host_input.set_text('localhost')
|
||||||
|
|
||||||
|
self.port_label = Gtk.Label('Port')
|
||||||
|
self.port_input = Gtk.Entry();
|
||||||
|
self.port_input.set_text('7887')
|
||||||
|
|
||||||
|
self.connect_btn = Gtk.Button(label="Connect")
|
||||||
|
self.connect_btn.connect("clicked", self.finish)
|
||||||
|
|
||||||
|
self.abort_btn = Gtk.Button(label="Abort")
|
||||||
|
self.abort_btn.connect("clicked", self.abort)
|
||||||
|
|
||||||
|
grid.attach(self.host_label,0,0,1,1)
|
||||||
|
grid.attach(self.host_input,1,0,1,1)
|
||||||
|
grid.attach(self.port_label,0,1,1,1)
|
||||||
|
grid.attach(self.port_input,1,1,1,1)
|
||||||
|
grid.attach(self.abort_btn,0,2,1,1)
|
||||||
|
grid.attach(self.connect_btn,1,2,1,1)
|
||||||
|
|
||||||
|
|
||||||
|
def abort(self, widget):
|
||||||
|
Gtk.main_quit()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.show_all()
|
||||||
|
Gtk.main()
|
||||||
|
|
||||||
|
def finish(self, widget):
|
||||||
|
self.host = self.host_input.get_text()
|
||||||
|
self.port = int(self.port_input.get_text())
|
||||||
|
self.hide()
|
||||||
|
Gtk.main_quit()
|
||||||
52
src/FileDialog.py
Normal file
52
src/FileDialog.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
import gi
|
||||||
|
gi.require_version('Gtk', '3.0')
|
||||||
|
from gi.repository import Gtk
|
||||||
|
|
||||||
|
class FileDialog(Gtk.Window):
|
||||||
|
|
||||||
|
def __init__(self,title,hint):
|
||||||
|
Gtk.Window.__init__(self, title=title)
|
||||||
|
self.connect("delete-event", Gtk.main_quit)
|
||||||
|
|
||||||
|
self.hint = Gtk.Label(hint);
|
||||||
|
self.dir_grid = Gtk.Grid()
|
||||||
|
self.dir_input = Gtk.Entry();
|
||||||
|
self.new_btn = Gtk.Button('Create')
|
||||||
|
self.new_btn.connect('clicked',self.new_btn_clck)
|
||||||
|
|
||||||
|
self.y = 0
|
||||||
|
|
||||||
|
|
||||||
|
def add_dir(self,name):
|
||||||
|
btn = Gtk.Button(label=name)
|
||||||
|
btn.connect("clicked",self.select_dir)
|
||||||
|
self.dir_grid.attach(btn,0,self.y,1,1)
|
||||||
|
self.y+=1
|
||||||
|
|
||||||
|
def new_btn_clck(self,widget):
|
||||||
|
self.dir = self.dir_input.get_text()
|
||||||
|
self.hide()
|
||||||
|
Gtk.main_quit()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
scroll = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
|
||||||
|
|
||||||
|
|
||||||
|
grid = Gtk.Grid()
|
||||||
|
scroll.add(self.dir_grid)
|
||||||
|
|
||||||
|
grid.attach(self.hint, 0,0,2,1)
|
||||||
|
grid.attach(scroll, 0,1,2,1)
|
||||||
|
grid.attach(self.dir_input, 0,2,1,1)
|
||||||
|
grid.attach(self.new_btn, 1,2,1,1)
|
||||||
|
|
||||||
|
self.add(grid)
|
||||||
|
self.set_size_request( 300, 900)
|
||||||
|
self.show_all()
|
||||||
|
Gtk.main()
|
||||||
|
|
||||||
|
def select_dir(self,widget):
|
||||||
|
self.dir = widget.get_label()
|
||||||
|
self.hide()
|
||||||
|
Gtk.main_quit()
|
||||||
32
src/YesNoDialog.py
Normal file
32
src/YesNoDialog.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
import gi
|
||||||
|
gi.require_version('Gtk', '3.0')
|
||||||
|
from gi.repository import Gtk
|
||||||
|
|
||||||
|
class YesNoDialog(Gtk.Window):
|
||||||
|
def __init__(self,question):
|
||||||
|
Gtk.Window.__init__(self)
|
||||||
|
self.connect("delete-event", Gtk.main_quit)
|
||||||
|
|
||||||
|
grid = Gtk.Grid()
|
||||||
|
|
||||||
|
question = Gtk.Label(question);
|
||||||
|
yes = Gtk.Button('Yes')
|
||||||
|
yes.connect('clicked',self.evaluate)
|
||||||
|
no = Gtk.Button('No')
|
||||||
|
no.connect('clicked',self.evaluate)
|
||||||
|
|
||||||
|
grid.attach(question,0,0,2,1)
|
||||||
|
grid.attach(yes,0,1,1,1)
|
||||||
|
grid.attach(no,1,1,1,1)
|
||||||
|
self.add(grid)
|
||||||
|
|
||||||
|
self.answer = None
|
||||||
|
|
||||||
|
self.show_all()
|
||||||
|
Gtk.main()
|
||||||
|
|
||||||
|
def evaluate(self,widget):
|
||||||
|
self.answer=widget.get_label() == 'Yes'
|
||||||
|
self.hide()
|
||||||
|
Gtk.main_quit()
|
||||||
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
59
src/server.py
Executable file
59
src/server.py
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
'''
|
||||||
|
Simple socket server using threads
|
||||||
|
'''
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
from thread import *
|
||||||
|
|
||||||
|
HOST = '' # Symbolic name meaning all available interfaces
|
||||||
|
PORT = 8888 # Arbitrary non-privileged port
|
||||||
|
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
print 'Socket created'
|
||||||
|
|
||||||
|
#Bind socket to local host and port
|
||||||
|
try:
|
||||||
|
s.bind((HOST, PORT))
|
||||||
|
except socket.error as msg:
|
||||||
|
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
print 'Socket bind complete'
|
||||||
|
|
||||||
|
#Start listening on socket
|
||||||
|
s.listen(10)
|
||||||
|
print 'Socket now listening'
|
||||||
|
|
||||||
|
#Function for handling connections. This will be used to create threads
|
||||||
|
def clientthread(conn):
|
||||||
|
#Sending message to connected client
|
||||||
|
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
|
||||||
|
|
||||||
|
#infinite loop so that function do not terminate and thread do not end.
|
||||||
|
while True:
|
||||||
|
|
||||||
|
#Receiving from client
|
||||||
|
data = conn.recv(1024).strip()
|
||||||
|
if data == 'exit':
|
||||||
|
conn.sendall('Bye!')
|
||||||
|
break
|
||||||
|
reply = 'OK...' + data
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
|
||||||
|
conn.sendall(reply)
|
||||||
|
|
||||||
|
#came out of loop
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
#now keep talking with the client
|
||||||
|
while 1:
|
||||||
|
#wait to accept a connection - blocking call
|
||||||
|
conn, addr = s.accept()
|
||||||
|
print 'Connected with ' + addr[0] + ':' + str(addr[1])
|
||||||
|
|
||||||
|
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
|
||||||
|
start_new_thread(clientthread ,(conn,))
|
||||||
|
|
||||||
|
s.close()
|
||||||
109
src/web4rail.client.py
Normal file → Executable file
109
src/web4rail.client.py
Normal file → Executable file
@@ -1,56 +1,61 @@
|
|||||||
import gi
|
#!/usr/bin/python
|
||||||
gi.require_version('Gtk', '3.0')
|
import socket
|
||||||
from gi.repository import Gtk
|
from ConnectDialog import *
|
||||||
|
from FileDialog import *
|
||||||
|
from YesNoDialog import *
|
||||||
|
|
||||||
class ConnectDialog(Gtk.Window):
|
def readline(socket):
|
||||||
|
buffer = socket.recv(4096)
|
||||||
host = None
|
buffering = True
|
||||||
port = None
|
while buffering:
|
||||||
|
if "\n" in buffer:
|
||||||
def __init__(self):
|
(line, buffer) = buffer.split("\n", 1)
|
||||||
Gtk.Window.__init__(self, title="Connect to server")
|
yield line
|
||||||
|
else:
|
||||||
grid = Gtk.Grid()
|
more = socket.recv(4096)
|
||||||
self.add(grid)
|
if not more:
|
||||||
|
buffering = False
|
||||||
self.host_label = Gtk.Label('Hostname or IP')
|
else:
|
||||||
|
buffer += more
|
||||||
self.host_input = Gtk.Entry();
|
if buffer:
|
||||||
self.host_input.set_text('localhost')
|
yield buffer
|
||||||
|
|
||||||
self.port_label = Gtk.Label('Port')
|
|
||||||
self.port_input = Gtk.Entry();
|
|
||||||
self.port_input.set_text('7887')
|
|
||||||
|
|
||||||
self.connect_btn = Gtk.Button(label="Connect")
|
|
||||||
self.connect_btn.connect("clicked", self.try_connect)
|
|
||||||
|
|
||||||
self.abort_btn = Gtk.Button(label="Abort")
|
|
||||||
self.abort_btn.connect("clicked", self.abort)
|
|
||||||
|
|
||||||
grid.attach(self.host_label,0,0,1,1)
|
|
||||||
grid.attach(self.host_input,1,0,1,1)
|
|
||||||
grid.attach(self.port_label,0,1,1,1)
|
|
||||||
grid.attach(self.port_input,1,1,1,1)
|
|
||||||
grid.attach(self.abort_btn,0,2,1,1)
|
|
||||||
grid.attach(self.connect_btn,1,2,1,1)
|
|
||||||
|
|
||||||
self.connect("delete-event", Gtk.main_quit)
|
|
||||||
|
|
||||||
def abort(self, widget):
|
|
||||||
Gtk.main_quit()
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
self.show_all()
|
|
||||||
Gtk.main()
|
|
||||||
|
|
||||||
|
|
||||||
def try_connect(self, widget):
|
|
||||||
self.host = self.host_input.get_text()
|
|
||||||
self.port = self.port_input.get_text()
|
|
||||||
Gtk.main_quit()
|
|
||||||
|
|
||||||
conn_dlg = ConnectDialog()
|
conn_dlg = ConnectDialog()
|
||||||
conn_dlg.run()
|
conn_dlg.run()
|
||||||
print str(conn_dlg.host)
|
|
||||||
print str(conn_dlg.port)
|
if conn_dlg.port != None:
|
||||||
|
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
client_sock.connect((conn_dlg.host,conn_dlg.port))
|
||||||
|
client = readline(client_sock)
|
||||||
|
current_dir = ''
|
||||||
|
|
||||||
|
while True:
|
||||||
|
line = client.next()
|
||||||
|
|
||||||
|
if line.startswith('current dir'):
|
||||||
|
current_dir = line
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line.startswith('select one'):
|
||||||
|
file_dialog = FileDialog(line,current_dir)
|
||||||
|
while True:
|
||||||
|
line = client.next()
|
||||||
|
if line.startswith('--'):
|
||||||
|
break
|
||||||
|
file_dialog.add_dir(line.strip())
|
||||||
|
file_dialog.run()
|
||||||
|
|
||||||
|
client_sock.send(file_dialog.dir+"\n")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if 'does not exist. Create' in line:
|
||||||
|
dir = line.split('does not exist')[0].strip()
|
||||||
|
dialog = YesNoDialog(line)
|
||||||
|
if dialog.answer:
|
||||||
|
client_sock.send("yes\n")
|
||||||
|
else:
|
||||||
|
client_sock.send("no\n")
|
||||||
|
continue
|
||||||
|
|
||||||
|
print line
|
||||||
|
print "Test"
|
||||||
19
src/web4rail.server.py
Normal file → Executable file
19
src/web4rail.server.py
Normal file → Executable file
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/python
|
||||||
|
|
||||||
import socket,sys,os
|
import socket,sys,os
|
||||||
from thread import *
|
from thread import *
|
||||||
@@ -25,27 +25,32 @@ class web4rail_server:
|
|||||||
|
|
||||||
def create_system(self,path):
|
def create_system(self,path):
|
||||||
print 'creating new system at '+path
|
print 'creating new system at '+path
|
||||||
|
os.mkdir(path, 0755)
|
||||||
|
file = open(path+'/plan.json', 'w+')
|
||||||
|
|
||||||
def load_system(self,path):
|
def load_system(self,path):
|
||||||
print 'loading system from '+path
|
print 'loading system from '+path
|
||||||
|
|
||||||
|
|
||||||
def select_system(self,conn):
|
def select_system(self,conn):
|
||||||
path='/'
|
path='/'
|
||||||
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n");
|
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n");
|
||||||
while True:
|
while True:
|
||||||
conn.send('current dir: '+path+"\n")
|
conn.send('current dir: '+path+"\n")
|
||||||
contents = os.listdir(path)
|
contents = sorted(os.listdir(path))
|
||||||
|
|
||||||
conn.send("select one from\n")
|
conn.send("select one from\n")
|
||||||
for entry in contents:
|
for entry in contents:
|
||||||
conn.send(' '+entry+"\n")
|
conn.send(' '+entry+"\n")
|
||||||
|
conn.send("--\n")
|
||||||
entry = conn.recv(1024).strip()
|
entry = conn.recv(1024).strip()
|
||||||
if entry in contents:
|
if entry in contents:
|
||||||
path += entry
|
if os.path.isfile(path+entry):
|
||||||
if os.path.isfile(path):
|
|
||||||
break
|
break
|
||||||
path += '/'
|
path += entry+'/'
|
||||||
else:
|
else:
|
||||||
conn.send(path+entry+' does not exist. Create (yes/no/abort)? ')
|
print entry
|
||||||
|
conn.send(path+entry+' does not exist. Create (yes/no/abort)?\n')
|
||||||
input = conn.recv(1024).strip()
|
input = conn.recv(1024).strip()
|
||||||
if input == 'yes':
|
if input == 'yes':
|
||||||
path += entry
|
path += entry
|
||||||
@@ -54,6 +59,8 @@ class web4rail_server:
|
|||||||
if input == 'abort':
|
if input == 'abort':
|
||||||
path = None
|
path = None
|
||||||
break
|
break
|
||||||
|
path = '/'
|
||||||
|
|
||||||
if path == None:
|
if path == None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user