Browse Source

updated to comply with python 3

lookup-tables
Stephan Richter 7 years ago
parent
commit
09a15a4c4e
  1. 43
      src/web4rail.client.py
  2. 69
      src/web4rail.server.py

43
src/web4rail.client.py

@ -4,66 +4,67 @@ from ConnectDialog import * @@ -4,66 +4,67 @@ from ConnectDialog import *
from FileDialog import *
from TrackPlan import *
from YesNoDialog import *
def readline(socket):
buffer = socket.recv(4096)
buff = socket.recv(4096)
buffering = True
while buffering:
if "\n" in buffer:
(line, buffer) = buffer.split("\n", 1)
if "\n" in buff:
(line, buff) = buff.split("\n", 1)
yield line
else:
more = socket.recv(4096)
if not more:
buffering = False
else:
buffer += more
if buffer:
yield buffer
buff += more
if buff:
yield buff
conn_dlg = ConnectDialog()
conn_dlg.run()
if conn_dlg.port != None:
if conn_dlg.port is not None:
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_sock.connect((conn_dlg.host,conn_dlg.port))
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)
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")
client_sock.send(file_dialog.dir + "\n")
continue
if line == 'PLAN:':
json = client.next()
plan = TrackPlan(json)
plan.run()
break
continue
if 'does not exist. Create' in line:
dir = line.split('does not exist')[0].strip()
directory = 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(line)
print "Test"

69
src/web4rail.server.py

@ -1,73 +1,76 @@ @@ -1,73 +1,76 @@
#!/usr/bin/python
import socket,sys,os
import socket
import sys
import os
from thread import *
class web4rail_server:
system = None
def __init__(self,port):
def __init__(self, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created.'
try:
self.socket.bind(('',port))
self.socket.bind(('', port))
except socket.error as msg:
print 'Bind failed. Error ('+str(msg[0])+'): '+str(msg[1])
print('Bind failed. Error (' + str(msg[0]) + '): ' + str(msg[1]))
sys.exit(-1)
print 'Bound to socket at port '+str(port)
print('Bound to socket at port ' + str(port))
def client(self,conn):
if self.system == None:
def client(self, conn):
if self.system is None:
self.select_system(conn)
conn.close
def create_system(self,path):
print 'creating new system at '+path
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,conn):
print 'loading system from '+path
with open(path+'/plan.json', 'r') as plan_file:
plan=plan_file.read()
json_file = open(path + '/plan.json', 'w+')
json_file.close()
def load_system(self, path, conn):
print('loading system from ' + path)
with open(path + '/plan.json', 'r') as plan_file:
plan = plan_file.read()
conn.send("PLAN:\n")
conn.send(plan+"\n")
conn.send(plan + "\n")
def select_system(self,conn):
path='/'
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n");
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")
conn.send('current dir: ' + path + "\n")
contents = sorted(os.listdir(path))
conn.send("select one from\n")
for entry in contents:
conn.send(' '+entry+"\n")
conn.send(' ' + entry + "\n")
conn.send("--\n")
entry = conn.recv(1024).strip()
if entry in contents:
if os.path.isfile(path+entry):
if os.path.isfile(path + entry):
break
path += entry+'/'
path += entry + '/'
else:
conn.send(path+entry+' does not exist. Create (yes/no/abort)?\n')
input = conn.recv(1024).strip()
if input == 'yes':
conn.send(path + entry + ' does not exist. Create (yes/no/abort)?\n')
response = conn.recv(1024).strip()
if response == 'yes':
path += entry
self.create_system(path)
break
if input == 'abort':
if response == 'abort':
path = None
break
path = '/'
if path == None:
if path is None:
return
self.load_system(path,conn)
self.load_system(path, conn)
def start(self):
self.socket.listen(10)
@ -75,7 +78,7 @@ class web4rail_server: @@ -75,7 +78,7 @@ class web4rail_server:
while True:
conn, add = self.socket.accept()
start_new_thread(self.client,(conn,))
start_new_thread(self.client, (conn,))
if len(sys.argv) == 2:
port = int(sys.argv[1])

Loading…
Cancel
Save