from PyKDE4 . Kdeui import KApplication , KMainWindow , KPushButton , KHBox , KVBox , KLineEdit , KListWidget
from PyKDE4 . kdecore import i18n , ki18n , KAboutData , KCmdLineArgs
from PyQt4 import QtCore
from PyQt4 . QtGui import Qlabel
import sys
class pyksshfsWindow ( KMainWindow ) :
selected_name = False
def __init__ (self, parent = None) : # constructor
KMainWindow . __init__ ( self , parent ) #call parent constructor
appName = "pyksshfs"
catalog = ""
programName = ki18n ( "PyKSshfs" )
version = "0.1"
description = ki18n ( "Gui application for using sshfs" )
license = KAboutData . License_GPL
copyright = ki18n ( "© Akademic" )
text = ki18n ( "none" )
homePage = "program site"
bugEmail = "email to communicate with the author on the subject of errors"
aboutData = KAboutData ( appName , catalog , programName , version , description , license , copyright , text , homePage , bugEmail )
KCmdLineArgs . init ( sys . argv , aboutData )
app = KApplication ( )
w = pyksshfsWindow ( )
w . show ( )
app . exec_ ( )
def __init__ ( self , parent = None ) : # constructor
KMainWindow . __init__ ( self , parent ) #call parent constructor
hbox = KHBox ( self ) # create a horizontal layer
hbox setMargin ( 10 ) # indents 10 pixels
self . setCentralWidget ( hbox ) # make it prime
# two vertical layers inside the main horizontal
vbox_left = KVBox ( hbox )
vbox_right = KVBox ( hbox )
# align the right layer to the top
hbox layout ( ) . setAlignment ( vbox_right , QtCore . Qt . AlignTop )
# data entry fields for mount
entry_name_label = QLabel ( 'Name:' , vbox_right )
self . entry_name = KLineEdit ( vbox_right )
server_address_label = QLabel ( 'Server address:' , vbox_right )
self . server_address = KLineEdit ( vbox_right )
server_port_label = QLabel ( 'Server port:' , vbox_right )
self . server_port = KLineEdit ( vbox_right )
user_name_label = QLabel ( 'Username:' , vbox_right )
self . user_name = KLineEdit ( vbox_right )
remote_path_label = QLabel ( 'Remote path:' , vbox_right )
self . remote_path = KLineEdit ( vbox_right )
local_path_label = QLabel ( 'Local path:' , vbox_right )
self . local_path = KLineEdit ( vbox_right )
# mount and unmount buttons
# create a separate layer for them
btn_hbox_right = KHBox ( vbox_right )
connect_btn = KPushButton ( btn_hbox_right )
connect_btn . setText ( i18n ( 'Connect' ) )
disconnect_btn = KPushButton ( btn_hbox_right )
disconnect_btn . setText ( i18n ( 'Disconnect' ) )
# list for saved profiles
saved_list_label = QLabel ( 'Stored connections:' , vbox_left )
self . saved_list = KListWidget ( vbox_left )
self . saved_list . setMaximumWidth ( 150 )
# buttons for saving and deleting profiles
btn_hbox_left = KHBox ( vbox_left )
save_btn = KPushButton ( btn_hbox_left )
save_btn . setText ( i18n ( 'Save' ) )
delete_btn = KPushButton ( btn_hbox_left )
delete_btn . setText ( i18n ( 'Delete' ) )
# binding event handlers to buttons
# here save_btn is a variable containing the save button object
# QtCore.SIGNAL ('clicked ()') - “click on the button” signal
# self.onSave - the method called to handle the click
self . connect ( save_btn , QtCore . SIGNAL ( 'clicked ()' ) , self . onSave
self . connect ( delete_btn , QtCore . SIGNAL ( 'clicked ()' ) , self . onDelete
self . connect ( connect_btn , QtCore . SIGNAL ( 'clicked ()' ) , self . onConnect )
self . connect ( disconnect_btn , QtCore . SIGNAL ( 'clicked ()' ) , self . onDisconnect )
# the most difficult was to find in the documentation as the signal called “clicked on an element in the list”
self . connect ( self . saved_list , QtCore . SIGNAL ( 'itemClicked (QListWidgetItem *)' ) , self . onSelectServer )
config_path = os . getenv ( 'HOME' ) + '/.pyksshfs/hosts'
if not os . path . isdir ( config_path ) :
os . makedirs ( config_path , 0700 )
def onSave ( self ) :
'' '
save settings
' ' '
if self . entry_name . text ( ) : # If there is a profile name
config = ConfigParser . RawConfigParser ( ) # then create and fill the config
config . add_section ( 'Connection' )
config . set ( 'Connection' , 'host' , self . server_address . text ( ) )
config . set ( 'Connection' , 'port' , self . server_port . text ( ) )
config . set ( 'Connection' , 'user_name' , self . user_name . text ( ) )
config . set ( 'Connection' , 'remote_path' , self . remote_path . text ( ) )
config . set ( 'Connection' , 'local_path' , self . local_path . text ( ) )
if self . selected_name :
os . unlink ( self . config_path + '/' + self . selected_name )
path = self . config_path + '/' + self . entry_name . text ( )
file = open ( path , 'w' )
config . write ( file ) # save the config
file . close ( )
self . selected_name = self . entry_name . text ( )
self . listServers ( ) # update list of profiles
def listServers ( self ) :
self . saved_list . clear ( )
hosts = os . listdir ( self . config_path )
self . saved_list . insertItems ( O , hosts ) # With this call, add the file list to the list widget
if self . selected_name : # If we have already selected a profile, then it should be selected in the list
item = self . saved_list . findItems ( self . selected_name , QtCore . Qt . MatchExactly )
self . saved_list . setItemSelected ( item [ O ] , True )
def onDisconnect ( self ) :
if ( self . local_path . text ( ) ) :
os . system ( 'fusermount -u' + str ( self . local_path . text ( ) ) )
def onConnect ( self ) :
command = 'sshfs'
if self . user_name . text ( ) :
command + = self . user_name . text ( ) + '@'
command + = self . server_address . text ( )
if self . remote_path text ( ) :
command + = ':' + self . remote_path text ( )
else :
command + = ': /'
if self . server_port . text ( ) :
command + = '-p' + self . server_port . text ( )
command + = '' + self . local_path . text ( )
sshfs = pexpect . spawn ( str ( command ) , env = { 'SSH_ASKPASS' : '/ dev / null' } )
ssh_newkey = 'Do you want to continue connecting'
i = sshfs . expect ( [ ssh_newkey , 'assword:' , pexpect . EOF , pexpect . TIMEOUT ] )
if i = = 0 :
sshfs sendline ( 'yes' )
i = sshfs . expect ( [ ssh_newkey , 'assword:' , pexpect . EOF ] )
if i = = 1 :
#If no password ask for it
askpasscmd = 'ksshaskpass% s' % self . entry_name . text ( )
password = pexpect . run ( askpasscmd ) . split ( '\ n' ) [ 1 ]
sshfs sendline ( password )
j = sshfs . expect ( [ pexpect . EOF , 'assword:' ] )
if j = = 1 :
#Password incorrect, force the connection close
print "Password incorrect"
sshfs close ( true )
# p.terminate (True)
elif i = = 2 :
#Any problem
print "Error found:% s" % sshfs . before
elif i = = 3 :
#Timeout
print "Timeout:% s" % sshfs . before
print sshfs . before
def onSelectServer ( self , item ) :
"" "
get settings from file when item selected in seved_list
" " "
name = item . text ( ) # filename
self . selected_name = name # remember selection
config = ConfigParser . RawConfigParser ( )
config . readfp ( open ( self . config_path + '/' + name ) ) # open the config
# fill the form fields from the config
self . entry_name . setText ( name )
self . server_address . setText ( config . get ( 'Connection' , 'host' ) )
self . server_port . setText ( config . get ( 'Connection' , 'port' ) )
self . user_name . setText ( config . get ( 'Connection' , 'user_name' ) )
self . remote_path setText ( config . get ( 'Connection' , 'remote_path' ) )
self . local_path . setText ( config . get ( 'Connection' , 'local_path' ) )
Source: https://habr.com/ru/post/52217/
All Articles