在本地映射远程ssh主机的目录树,效果如下:
源码如下:
#!/usr/bin/env python
#coding:utf8
#############################################################################
u'''
# Copyright (C) 2011-2013 xgtiger <xgtiger@163.com>
#
# This file is my some tool classes and functions,written by wyk(xgtiger)
# this util is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this module; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
'''
import sip
sip.setapi('QString', 1)
from PyQt4 import QtCore, QtGui
from my_util import *
class TreeDialog(QtGui.QDialog):
def __init__(self,ftp,encoding='utf-8',res_list=[],parent=None,window_name='treedialog'):
QtGui.QDialog.__init__(self,parent=parent)
self.setWindowTitle(window_name)
self.tree=QtGui.QTreeWidget()
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.tree)
self.setLayout(mainLayout)
self.encoding=encoding
self.sftp=ftp
self.res_list=res_list
self.sftp.chdir('/')
self.cur_dir_dict={}
self.parent_dict={}
self.children_dict={}
self.tree.setColumnCount(1);
self.tree.setSortingEnabled(1)
self.tree.sortItems(0,0)
self.root = QtGui.QTreeWidgetItem(self.tree, QtCore.QStringList(QtCore.QString(u"/")));
self.tree.insertTopLevelItem(0, self.root);
self.itemClicked(self.root)
QtCore.QObject.connect(self.tree, QtCore.SIGNAL("itemClicked (QTreeWidgetItem *,int)"), self.itemClicked)
QtCore.QObject.connect(self.tree, QtCore.SIGNAL("itemDoubleClicked (QTreeWidgetItem *,int)"), self.itemDoubleClicked)
def get_path(self,item):
me=qstr_to_str(item.text(0))
if self.parent_dict[item] is not None:#不是根节点
parent=self.parent_dict[item]
path=join_path(self.get_path(parent),me)
else:
if me!='/':
path='/'+me
else:
path=me
return path
def itemDoubleClicked(self,item,i=0):
if self.children_dict.has_key(item) :
if self.children_dict[item] is None:#文件
path={'path':self.get_path(item),'type_t':'file','file_list':[qstr_to_str(item.text(0))]}
else:
file_list=[]
for child in self.children_dict[item]:
file=qstr_to_str(child)
file_list.append(file)################
path={'path':self.get_path(item),'type_t':'dir','file_list':file_list}
self.res_list.append(path)
self.accept()
def itemClicked(self,item,i=0):
if item.isExpanded():
pass
else:
if self.children_dict.has_key(item):
pass
else:
self.walk_one_step(item)
item.setExpanded (1)
def _decode(self,s,encoding):
'''
将指定的encoding编码的字符串转换为unicode
其它编码的不转换
'''
if type(s) in [unicode]:
return s
elif type(s) is str:
try:
return s.decode(encoding)
except:
return s
else:
return s
def _encode(self,s,encoding):
'''
将unicode字符串转换为encoding编码
'''
if type(s) is unicode:
return s.encode(encoding)
else:
return s
def walk_one_step(self,item):
if self.parent_dict.has_key(item):
parent=self.parent_dict[item]
else:#根节点
parent=None
self.parent_dict[item]=parent
if parent is None:
cur_dir='/'
else:
if parent.text(0)=='/':
cur_dir='/'+qstr_to_str(item.text(0))
else:
cur_dir=join_path(self.cur_dir_dict[item],qstr_to_str(item.text(0)))
leaf_list=self.sftp.listdir(cur_dir,encoding=None)
self.children_dict[item]=leaf_list
if leaf_list==ERROR:
self.children_dict[item]=None
else:
decoded=[]
for v in leaf_list:
aa=self._decode(v,self.encoding)
decoded.append(aa)
node_list=[]
for leaf in decoded:
node=QtGui.QTreeWidgetItem(item, QtCore.QStringList(QtCore.QString(leaf)))
self.cur_dir_dict[node]=cur_dir
node_list.append(node)
self.parent_dict[node]=item
item.addChildren(node_list)
def sizeHint(self):
return QtCore.QSize(400, 500)
if __name__ == '__main__':
import sys
ftp=SFtp(hostname='192.168.88.1', port=22, username='root', password='1')
ftp.connect()
app = QtGui.QApplication(sys.argv)
window = TreeDialog(ftp,encoding='gbk')
window.exec_()
sys.exit(app.exec_())