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

324 lines
13 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 logging
import ConfigParser, os, urllib2
from calculate.core.client.function import clear as clear_suds_cache
from session_function import client_post_cert
from calculate.core.client.cert_verify import VerifyError
from client_class import Client_suds, HTTPSClientCertTransport
from more import post_connect_action, show_msg, uniq, LabelWordWrap
class FrameConnection(QtGui.QWidget):
def __init__(self, parent, ClientObj):
QtGui.QWidget.__init__(self)
self.ClientObj = ClientObj
def initUI(self, parent, window):
grid = QtGui.QGridLayout(self)
grid.setSpacing(10)
self.lbl_host = LabelWordWrap(_("Host"), self)
self.lbl_port = LabelWordWrap(_("Port"), self)
self.text_host = QtGui.QLineEdit(self.ClientObj.default_host, self)
self.text_port = QtGui.QLineEdit(self.ClientObj.default_port, self)
######################
# 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
config = ConfigParser.ConfigParser()
config.read(self.ClientObj.user_config)
try:
wordList = config.get('other', 'connect_list')
self.wordList = wordList.split(',')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
self.wordList = []
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)
self.cmd_connect.setIcon(QtGui.QIcon.fromTheme("network-connect"))
self.cmd_connect.setDefault(True)
self.cmd_connect.setAutoDefault(True)
self.cmd_connect.setMaximumWidth(120)
self.cmd_connect.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return))
self.cmd_cancel = QtGui.QPushButton(_('Cancel'), self)
self.cmd_cancel.setMaximumWidth(120)
self.cmd_cancel.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape))
self.cmd_cancel.clicked.connect(self.close)
grid.addWidget(self.cmd_connect, 3, 1)
grid.addWidget(self.cmd_cancel, 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.clicked.connect (self.set_disabled)
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(window.geometry().x() + window.geometry().width() / 2 \
- self.sizeHint().width() / 2, \
window.geometry().y() + window.geometry().height() / 2 \
- self.sizeHint().height() / 2)
self.setWindowIcon (QtGui.QIcon.fromTheme("network-connect"))
def set_disabled(self):
self.setDisabled(True)
self.ClientObj.app.processEvents()
# establish a connection
def onClick(self):
self.setDisabled(True)
if self.ClientObj.client:
show_msg(_('You are connected to server!')+'\n' + \
_('Please break ') + _('previous connection!'), \
_('Connection Error'))
return 1
self.ClientObj.app.processEvents()
self.setDisabled(False)
# write parameters
self.str_host = self.text_host.text()
self.str_port = self.text_port.text()
# add host_name in list ########## start
while len(self.wordList) > 6:
self.wordList.remove(self.wordList[6])
if not self.str_host in self.wordList:
self.wordList.insert(0, self.str_host)
if not os.path.isfile (self.ClientObj.user_config):
f = open (self.ClientObj.user_config, 'w')
f.close()
fc = open (self.ClientObj.user_config, 'r')
config = fc.readlines()
fc.close()
new_config = []
host_list_flag = False
for line in config:
if line.startswith('connect_list '):
host_list_flag = True
new_config.append('connect_list = %s\n' \
%','.join(self.wordList))
else:
new_config.append(line)
if not host_list_flag:
part_flag = False
temp_cfg = []
for line in new_config:
temp_cfg.append(line)
#add new line in config
if line.startswith('[other]'):
temp_cfg.append('%s = %s\n' \
%('connect_list', ','.join(self.wordList)))
part_flag = True
new_config = temp_cfg
# if part not exists
if not part_flag:
new_config.append('\n')
new_config.append('%s\n' %'[other]')
new_config.append('%s = %s\n' \
%('connect_list', ','.join(self.wordList)))
fnc = open(self.ClientObj.user_config, 'w')
for line in new_config:
fnc.write(line)
fnc.close()
# add host_name in list ########## end
try:
int_port = int(self.str_port)
except:
show_msg(_("Enter correctly port!"))
return 1
self.connect_to_host(self.str_host, int_port)
def connect_to_host(self, host_name, port = None):
logging.basicConfig(level=logging.FATAL)
logging.getLogger('suds.client').setLevel(logging.FATAL)
logging.getLogger('suds.transport').setLevel(logging.FATAL)
logging.getLogger('suds.transport.http').setLevel(logging.FATAL)
logging.getLogger('suds.umx.typed').setLevel(logging.ERROR)
clear_suds_cache()
# not twice connect to localhost
try:
if (host_name in ['127.0.0.1', 'localhost']) and \
self.ClientObj._parent.localhost_ClientObj.client:
show_msg(_('You already connected to localhost!'), \
'cl-conlose-gui')
return 1
except AttributeError:
pass
if not port:
port = 8888
# get server hostname
url = "https://%s:%d/?wsdl" %(host_name, port)
self.ClientObj.port = 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, timeout = 5))
except AttributeError, e:
show_msg (_('This server is not trusted') ,_('Not connected!'))
return 1
except Exception, e:
show_msg (e, _("Not connected!"))
return 1
client.wsdl.services[0].setlocation(url)
try:
server_host_name = client.service.get_server_host_name()
except urllib2.URLError, e:
show_msg (e, _("Not connected!"))
return 1
except Exception as e:
print 'connect_to_host Exception: ', e
return 1
del (client)
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:
print 'url = ', url
self.ClientObj.client = Client_suds(url,\
transport = HTTPSClientCertTransport(CERT_KEY, \
CERT_FILE, path_to_cert, parent = self.ClientObj, \
timeout = 5))
self.ClientObj.client.wsdl.services[0].setlocation(url)
self.ClientObj.client.set_parameters \
(path_to_cert, CERT_FILE, CERT_KEY)
self.ClientObj.client.server_host_name = host_name
client_post_cert(self.ClientObj.client, \
self.ClientObj.lang)
Connect_Error = 0
except VerifyError, e:
show_msg (e.value, _("Verify Error"))
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
print 'url = ', url
self.ClientObj.client = Client_suds(url,\
transport = HTTPSClientCertTransport(CERT_KEY, CERT_FILE,\
path_to_cert, parent = self.ClientObj, timeout = 5))
self.ClientObj.client.wsdl.services[0].setlocation(url)
self.ClientObj.client.set_parameters (path_to_cert, CERT_FILE,\
CERT_KEY)
try:
self.ClientObj.client.frame_period = \
int(self.ClientObj.VarsApi.Get \
('cl_core_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.setDaemon(True)
self.ClientObj.host_name = host_name
print 'host_name', host_name
if CERT_FILE:
if hasattr (self.ClientObj._parent, 'rename_tab'):
self.ClientObj._parent.rename_tab(host_name)
if host_name in ['127.0.0.1', 'localhost']:
self.ClientObj._parent.set_localhost(self.ClientObj)
# localhost_ClientObj = self.ClientObj
self.ClientObj.client.server_host_name = host_name
post_connect_action(self.ClientObj.client, self.ClientObj)
#----------------------------------------------------
except WebFault, f:
show_msg ("Exception: %s" %f)
print f.fault
self.close()