File: FTPExplorer.py

package info (click to toggle)
boa-constructor 0.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,188 kB
  • ctags: 8,857
  • sloc: python: 54,163; sh: 66; makefile: 36
file content (248 lines) | stat: -rw-r--r-- 9,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#-----------------------------------------------------------------------------
# Name:        FTPExplorer.py
# Purpose:
#
# Author:      Riaan Booysen
#
# Created:     2001
# RCS-ID:      $Id: FTPExplorer.py,v 1.15 2004/08/16 13:22:00 riaan Exp $
# Copyright:   (c) 2001 - 2004
# Licence:     GPL
#-----------------------------------------------------------------------------
print 'importing Explorers.FTPExplorer'

import os

from wxPython.wx import wxMenu, EVT_MENU, wxMessageBox, wxPlatform, wxNewId

import Preferences, Utils

import ExplorerNodes
from Models import Controllers, EditorHelper
import ftplib

true = 1
false = 0

wxID_FTPOPEN = wxNewId()

class FTPController(ExplorerNodes.Controller, ExplorerNodes.ClipboardControllerMix):
    def __init__(self, editor, list, inspector, controllers):
        ExplorerNodes.ClipboardControllerMix.__init__(self)
        ExplorerNodes.Controller.__init__(self, editor)

        self.list = list
        self.menu = wxMenu()

        self.setupMenu(self.menu, self.list,
              [ (wxID_FTPOPEN, 'Open', self.OnOpenItems, '-'),
                (-1, '-', None, '') ] + self.clipMenuDef)
        self.toolbarMenus = [self.clipMenuDef]

    def destroy(self):
        ExplorerNodes.ClipboardControllerMix.destroy(self)
        self.toolbarMenus = ()
        self.menu.Destroy()


class FTPCatNode(ExplorerNodes.CategoryNode):
    itemProtocol = 'ftp'
    defName = 'FTP'
    defaultStruct = {'username': 'anonymous',
                     'passwd': '',
                     'host': 'localhost',
                     'port': 21,
                     'path': '/',
                     'passive': 0}
    def __init__(self, clipboard, config, parent, bookmarks):
        ExplorerNodes.CategoryNode.__init__(self, 'FTP', ('explorer', 'ftp'),
              clipboard, config, parent)
        self.bookmarks = bookmarks

    def createParentNode(self):
        return self

    def createChildNode(self, name, props):
        ftpcn = FTPConnectionNode(name, props, props['path'], self.clipboard, self)
        ftpcn.bookmarks = self.bookmarks
        return ftpcn

    def createCatCompanion(self, catNode):
        comp = ExplorerNodes.CategoryDictCompanion(catNode.treename, self)
        return comp

class FTPItemNode(ExplorerNodes.ExplorerNode):
    protocol = 'ftp'
    connection = true
    def __init__(self, name, props, resourcepath, clipboard, isFolder, imgIdx, parent, ftpConn, ftpObj, root):
        ExplorerNodes.ExplorerNode.__init__(self, name, resourcepath, clipboard, imgIdx,
              parent, props)
        self.isFolder = isFolder
        self.ftpConn = ftpConn
        self.ftpObj = ftpObj
        self.root = root
        self.cache = {}

    def destroy(self):
        pass#self.cache = {}

    def isFolderish(self):
        return self.ftpObj.isFolder()

    def getURI(self):
        return '%s://%s%s%s' %(self.protocol, self.category,
              self.ftpObj.whole_name(), self.isFolderish() and '/' or '')

    def createChildNode(self, obj, root, respath=None, createConnection=false):
        if respath is None:
            respath=self.resourcepath+'/'+obj.name
        elif respath[0] != '/':
            respath = '/'+respath

        if createConnection:
            item = FTPConnectionNode(obj.name, self.properties, respath,
                self.clipboard, self)
        else:
            item = FTPItemNode(obj.name, self.properties, respath,
                  self.clipboard, false, -1 , self, self.ftpConn, obj, root)

        if item.isFolderish():
            item.imgIdx = EditorHelper.imgFolder
        else:
            item.imgIdx = Controllers.identifyFile(obj.name, localfs=false)[0].imgIdx
        item.category = self.category
        item.bookmarks = self.bookmarks
        return item

    def openList(self, root=None):
        items = self.ftpConn.dir(self.ftpObj.whole_name())

        if not root: root = self.root
        self.cache = {}
        result = []
        for obj in items:
            if obj.name in ('', '.', '..'):
                continue
            z = self.createChildNode(obj, self.root)
            if z:
                result.append(z)
                self.cache[obj.name] = z

        return result

    def deleteItems(self, names):
        for item in names:
            self.ftpConn.delete(self.cache[item].ftpObj)

    def renameItem(self, name, newName):
        self.ftpConn.rename(self.cache[name].ftpObj, newName)

    def newFolder(self, name):
        self.ftpConn.add_folder(name, self.resourcepath)

    def newBlankDocument(self, name):
        self.ftpConn.upload(name, self.resourcepath, ' ')

    def load(self, mode='rb'):
        try:
            return self.ftpConn.load(self.ftpObj)
        except Exception, error:
            raise ExplorerNodes.TransportLoadError(error, self.ftpObj.whole_name())

    def save(self, filename, data, mode='wb', overwriteNewer=false):
        if filename != self.currentFilename():
            self.ftpObj.path = os.path.dirname(filename)
            self.ftpObj.name = os.path.basename(filename)
        try:
            self.ftpConn.save(self.ftpObj, data)
        except Exception, error:
            raise ExplorerNodes.TransportSaveError(error, self.ftpObj.whole_name())

    def getNodeFromPath(self, respath):
        if not respath: respath = '/'

        isFolder = respath[-1] == '/'
        if isFolder:
            if respath != '/':
                respath = respath[:-1]
            return self.createChildNode(self.ftpConn.folder_item(os.path.dirname(respath),
                os.path.basename(respath)), self.root, respath)
        else:
            return self.createChildNode(self.ftpConn.add_doc(os.path.dirname(respath),
                os.path.basename(respath)), self.root, respath)


class FTPConnectionNode(FTPItemNode):
    def __init__(self, name, properties, respath, clipboard, parent):
        from ZopeLib import ZopeFTP

        ftpConn = ZopeFTP.ZopeFTP()
        if respath and respath[-1] == '/':
            ftpObj = ftpConn.folder_item(os.path.basename(respath),
                                         os.path.dirname(respath))
            isFolder = true
        else:
            ftpObj = ftpConn.add_doc(os.path.basename(respath),
                                         os.path.dirname(respath))
            isFolder = false

        FTPItemNode.__init__(self, '', properties, ftpObj.path, clipboard,
            isFolder, EditorHelper.imgNetDrive, parent, ftpConn, ftpObj, self)
        self.connected = false
        self.treename = name
        self.category = name

    def openList(self):
        self.testConnect()
        return FTPItemNode.openList(self, self)

    def load(self, mode='rb'):
        self.testConnect()
        return FTPItemNode.load(self, mode)

    def save(self, filename, data, mode='wb', overwriteNewer=false):
        self.testConnect()
        FTPItemNode.save(self, filename, data, mode, overwriteNewer)

    def createChildNode(self, obj, root, respath=None):
        return FTPItemNode.createChildNode(self, obj, root, respath, not self.connected)

    def testConnect(self):
        if not self.connected:
            try:
                props = self.properties
                self.ftpConn.connect(props['username'], props['passwd'],
                                     props['host'], props['port'],
                                     props['passive'])
            except Exception, message:
                wxMessageBox(`message.args`, 'Error on connect')
                raise
            else:
                self.connected = true


class FTPExpClipboard(ExplorerNodes.ExplorerClipboard):
    def pasteFileSysFolder(self, folderpath, nodepath, ftpConn):
        ftpConn.add_folder(os.path.basename(folderpath), nodepath)
        files = os.listdir(folderpath)
        folder = os.path.basename(folderpath)
        newNodepath = nodepath+'/'+folder
        for file in files:
            file = os.path.join(folderpath, file)
            if os.path.isdir(file):
                self.pasteFileSysFolder(file, newNodepath, ftpConn)
            else:
                ftpConn.upload(file, newNodepath)

    def clipPaste_FileSysExpClipboard(self, node, nodes, mode):
        nodepath = node.resourcepath
        for file in nodes:
            if file.isDir():
                self.pasteFileSysFolder(file.resourcepath, nodepath, node.ftpConn)
            else:
                node.ftpConn.upload(file.resourcepath, nodepath)

#-------------------------------------------------------------------------------
ExplorerNodes.register(FTPItemNode, clipboard=FTPExpClipboard,
      confdef=('explorer', 'ftp'), controller=FTPController, category=FTPCatNode)
ExplorerNodes.fileOpenDlgProtReg.append('ftp')