You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-console-gui/console/gui/conf_connection.py

233 lines
9.1 KiB

#!/usr/bin/python
#-*- coding: utf-8 -*-
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from PySide import QtGui, QtCore
from suds import WebFault
import threading
from calculate.api.cl_api import DataVarsApi
from calculate.api.client.function import clear
from session_function import client_post_cert
from calculate.api.client.cert_verify import VerifyError
from client_class import Client_suds, HTTPSClientCertTransport
from more import https_server, client_signal, show_msg, uniq, LabelWordWrap
class FrameConnection(QtGui.QWidget):
def __init__(self, parent, ClientObj):
QtGui.QWidget.__init__(self)
# super(FrameConnection, self).__init__(parent)
self.ClientObj = ClientObj
# self.initUI()
#
# def initUI(self):
grid = QtGui.QGridLayout(self)
grid.setSpacing(10)
self.lbl_host = LabelWordWrap(_("Host"))
# f = self.lbl_host.frameShadow()
# self.lbl_host.setFrameShadow(f.Plain)#Plain
# self.lbl_host.setFrameRect(QtCore.QRect(3,3,3,3))
self.lbl_port = LabelWordWrap(_("Port"))
self.text_host = QtGui.QLineEdit(self.ClientObj.default_host)
self.text_port = QtGui.QLineEdit(self.ClientObj.default_port)
######################
# add completer in 'host name' QLineEdit
def add_wordlist(self):
def wrapper():
self.wordList.append (self.text_host.text())
self.wordList = uniq(self.wordList)
del (self.completer)
self.completer = QtGui.QCompleter(self.wordList, self)
self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.completer.setMaxVisibleItems(7)
self.text_host.setCompleter(self.completer)
return wrapper
self.wordList = ['localhost', '127.0.0.1', '10.0.0.81', '10.0.0.84']
self.completer = QtGui.QCompleter(self.wordList, self)
self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.completer.setMaxVisibleItems(7)
self.text_host.setCompleter(self.completer)
self.text_host.lostFocus.connect(add_wordlist(self))
#####################
grid.setRowMinimumHeight(0,20)
grid.setColumnMinimumWidth(0,30)
# grid.addWidget(QtGui.QSpacerItem(10,10),0,0)
grid.addWidget(self.lbl_host, 1, 1)
grid.addWidget(self.lbl_port, 2, 1)
grid.addWidget(self.text_host, 1, 2)
grid.addWidget(self.text_port, 2, 2)
self.cmd_connect = QtGui.QPushButton(_('Connect'))
self.cmd_connect.setIcon(QtGui.QIcon.fromTheme("network-connect"))
self.cmd_connect.setDefault(True)
self.cmd_connect.setAutoDefault(True)
self.cmd_connect.setMaximumWidth(120)
grid.addWidget(self.cmd_connect, 3, 2)
self.pix_lbl = QtGui.QLabel(self)
pi = QtGui.QPixmap()
pi.load('/usr/share/wallpapers/calculate-logo.png')
self.pix_lbl.setPixmap(pi)
grid.addWidget(self.pix_lbl, 4,1,1,2)
self.cmd_connect.connect \
(self.cmd_connect, QtCore.SIGNAL('clicked()'), self.onClick)
# for clear memory after closed this window
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setLayout(grid)
self.move(parent.pos().x() + parent.geometry().width() / 2 \
- self.sizeHint().width() / 2, \
parent.pos().y() + parent.geometry().height() / 2 \
- self.sizeHint().height() / 2)
# self.setGeometry(0, 0, 150, 180)
# self.show()
# establish a connection
def onClick(self):
if self.ClientObj.client:
show_msg('You are connected to server!\nPlease break ' + \
'previous connection!','Connection Error')
return 1
clear()
# write parameters
self.str_host = self.text_host.text()
self.str_port = self.text_port.text()
# self.lbl_host.setText(self.str_host)
# self.lbl_port.setText(self.str_port)
try:
int_port = int(self.str_port)
except:
show_msg("Enter correctly port!")
return 1
# get server hostname
url = "https://%s:%d/?wsdl" %(self.str_host, int_port)
print url
path_to_cert = self.ClientObj.path_to_cert
try:
client = Client_suds(url, transport = \
HTTPSClientCertTransport(None,None, path_to_cert, parent = self.ClientObj))
except AttributeError, e:
# show_msg ('May be you do not have certificate for this server?' ,'Not connected!')
show_msg ('This server is not trusted' ,'Not connected!')
return 1
except Exception, e:
show_msg (e, "Not connected!")
return 1
server_host_name = client.service.get_server_host_name()
del (client)
# self.lbl_port.setText(server_host_name)
try:
import glob
all_cert_list = glob.glob(path_to_cert + '*.crt')
fit_cert_list = []
for client_cert_path in all_cert_list:
client_cert = client_cert_path.replace(path_to_cert, '')
client_cert_name = client_cert.replace('.crt', '')
if server_host_name.endswith(client_cert_name):
fit_cert_list.append(client_cert_name)
fit_cert_list.sort(key = len)
Connect_Error = 1
for i in range (0, len(fit_cert_list)):
#print 'fit_cert_list = ',fit_cert_list
cert_name = fit_cert_list.pop()
CERT_FILE = path_to_cert + cert_name + '.crt'
CERT_KEY = path_to_cert + cert_name + '.key'
# print "cert_name = ",cert_name
try:
self.ClientObj.client = Client_suds(url,\
transport = HTTPSClientCertTransport(CERT_KEY, \
CERT_FILE, path_to_cert, parent = self.ClientObj))
self.ClientObj.client.set_parameters \
(path_to_cert, CERT_FILE, CERT_KEY)
client_post_cert(self.ClientObj.client, self.ClientObj.lang)
Connect_Error = 0
except VerifyError, e:
show_msg (e.value, "VerifyError")
Connect_Error = 1
except Exception, e:
show_msg (e.message)
Connect_Error = 1
#sys.exit()
if Connect_Error == 0:
break
#If the certificate file misses
if Connect_Error:
CERT_FILE = None
CERT_KEY = None
self.ClientObj.client = Client_suds(url,\
transport = HTTPSClientCertTransport(CERT_KEY, CERT_FILE,\
path_to_cert, parent = self.ClientObj))
self.ClientObj.client.set_parameters (path_to_cert, CERT_FILE, CERT_KEY)
Vars = DataVarsApi()
Vars.importApi()
Vars.flIniFile()
try:
self.ClientObj.client.frame_period = int(Vars.Get('cl_api_get_frame_period'))
except:
show_msg('Write cl_api_get_frame_period Error!')
self.ClientObj.client.frame_period = 2
print "&&&&&&&&&&&&&&&&&7" ,url
self.ClientObj.signaling = threading.Thread(target=client_signal, args = (self.ClientObj.client, ))
# self.ClientObj.signaling.setDaemon(True)
https_server(self.ClientObj.client, self.ClientObj.signaling, self.ClientObj)
#----------------------------------------------------
except WebFault, f:
show_msg ("Exception: %s" %f)
print f.fault
self.close()
# except TransportError, te:
# show_msg ("Exception: %s" %te)
# except Exception, e:
# show_msg ("Exception: %s" %e)
# tb.print_exc()
#class FrameConnection_old (QtGui.QScrollArea):
# def __init__(self, parent, ClientObj):
# self.parent = parent
# QtGui.QScrollArea.__init__(self)
# # верхнее правое
# self.connection = FrameConnection(ClientObj)
# self.setWidget(self.connection)
#
# def resizeEvent(self, resize_var):
# self.parent.set_wid (self.size().width())
# if self.size().width() > 30:
# self.connection.setFixedWidth(self.parent.mainwidth - 30)