Browse Source

working on python dialogs

lookup-tables
Stephan Richter 7 years ago
parent
commit
fafe2b8728
  1. 52
      src/ConnectDialog.py
  2. 52
      src/FileDialog.py
  3. 32
      src/YesNoDialog.py
  4. 0
      src/__init__.py
  5. 59
      src/server.py
  6. 109
      src/web4rail.client.py
  7. 21
      src/web4rail.server.py

52
src/ConnectDialog.py

@ -0,0 +1,52 @@ @@ -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

@ -0,0 +1,52 @@ @@ -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

@ -0,0 +1,32 @@ @@ -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

59
src/server.py

@ -0,0 +1,59 @@ @@ -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

@ -1,56 +1,61 @@ @@ -1,56 +1,61 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
#!/usr/bin/python
import socket
from ConnectDialog import *
from FileDialog import *
from YesNoDialog import *
def readline(socket):
buffer = socket.recv(4096)
buffering = True
while buffering:
if "\n" in buffer:
(line, buffer) = buffer.split("\n", 1)
yield line
else:
more = socket.recv(4096)
if not more:
buffering = False
else:
buffer += more
if buffer:
yield buffer
class ConnectDialog(Gtk.Window):
host = None
port = None
def __init__(self):
Gtk.Window.__init__(self, title="Connect to server")
grid = Gtk.Grid()
self.add(grid)
conn_dlg = ConnectDialog()
conn_dlg.run()
self.host_label = Gtk.Label('Hostname or IP')
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()
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.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()
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
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.run()
print str(conn_dlg.host)
print str(conn_dlg.port)
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"

21
src/web4rail.server.py

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/python
import socket,sys,os
from thread import *
@ -25,27 +25,32 @@ class web4rail_server: @@ -25,27 +25,32 @@ class web4rail_server:
def create_system(self,path):
print 'creating new system at '+path
os.mkdir(path, 0755)
file = open(path+'/plan.json', 'w+')
def load_system(self,path):
print 'loading system from '+path
def select_system(self,conn):
path='/'
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n");
while True:
conn.send('current dir: '+path+"\n")
contents = os.listdir(path)
contents = sorted(os.listdir(path))
conn.send("select one from\n")
for entry in contents:
conn.send(' '+entry+"\n")
conn.send("--\n")
entry = conn.recv(1024).strip()
if entry in contents:
path += entry
if os.path.isfile(path):
if os.path.isfile(path+entry):
break
path += '/'
path += entry+'/'
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()
if input == 'yes':
path += entry
@ -54,6 +59,8 @@ class web4rail_server: @@ -54,6 +59,8 @@ class web4rail_server:
if input == 'abort':
path = None
break
path = '/'
if path == None:
return

Loading…
Cancel
Save