Package modules :: Module generic_remote
[hide private]
[frames] | no frames]

Source Code for Module modules.generic_remote

 1  """ 
 2  Abstract class for providing remote features (download, upload) for file based modules 
 3   
 4  This file is part of Pisi. 
 5   
 6  Only HTTP (Webdav) is supported. 
 7   
 8  Urllib2 (read, D{http://www.python.org/doc/2.5/lib/module-urllib2.html}) and Pythonwebdavlib (write, D{http://sourceforge.net/projects/pythonwebdavlib} are used for implementation.  
 9   
10  Pisi is free software: you can redistribute it and/or modify 
11  it under the terms of the GNU General Public License as published by 
12  the Free Software Foundation, either version 3 of the License, or 
13  (at your option) any later version. 
14   
15  Pisi is distributed in the hope that it will be useful, 
16  but WITHOUT ANY WARRANTY; without even the implied warranty of 
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18  GNU General Public License for more details. 
19   
20  You should have received a copy of the GNU General Public License 
21  along with Pisi.  If not, see <http://www.gnu.org/licenses/>. 
22  """ 
23   
24  import socket 
25  import urllib2 
26  import webdav.WebdavClient 
27  import os 
28  from pisiconstants import * 
29   
30 -class RemoteRessourceHandler:
31 """ 32 Generic class for handling remote resources. 33 34 Your specific class (a handler for a certain PIM type) should inherit from this class and just use upload and download methods. 35 """ 36
37 - def __init__(self, url, remoteFilename, localFilename = FILEDOWNLOAD_TMPFILE, username = None, password = None):
38 """ 39 Constructor 40 41 Initializes instance variables. 42 """ 43 self._url = url 44 self._remoteFilename = remoteFilename 45 self._localFile = localFilename 46 self._user = username 47 self._password = password 48 49 self._completeUrl = self._url + self._remoteFilename 50 socket.setdefaulttimeout(FILEDOWNLOAD_TIMEOUT)
51
52 - def download(self):
53 """ 54 Downloads remote file and overwrites local file 55 """ 56 if self._user and self._password: 57 passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 58 passman.add_password(None, self._url, self._user, self._password) 59 authhandler = urllib2.HTTPBasicAuthHandler(passman) 60 opener = urllib2.build_opener(authhandler) 61 response = opener.open(self._completeUrl) 62 else: 63 response = urllib2.urlopen(self._completeUrl) 64 data = response.read() 65 file = open(self._localFile, 'w') 66 file.write(data) 67 file.close()
68
69 - def upload(self):
70 """ 71 Uploads local file and overwrites remote file 72 """ 73 c = webdav.WebdavClient.CollectionStorer(self._url, None) 74 c.connection.addBasicAuthorization(self._user, self._password) 75 child= c.addResource(self._remoteFilename) 76 f = open(self._localFile, "r") 77 child.uploadFile(f) 78 f.close()
79
80 - def cleanup(self):
81 """ 82 Remove traces 83 """ 84 try: 85 os.remove(self._localFile) 86 except BaseException: 87 pass
88