def test05_delete(self): client = WebDAVClient("127.0.0.1", 8080) response = client.delete(FILE_PATH) self.assertEqual(response.statusline, 'HTTP/1.1 204 No Content') with self.assertRaises(HTTPUserError): response = client.delete(FILE_PATH) response = client.delete('/foo/') self.assertEqual(response.statusline, 'HTTP/1.1 204 No Content') with self.assertRaises(HTTPUserError): client.propfind("/foo")
class WebdavClient(object): def __init__(self,): settings = Settings() self.url = settings.get('WebDav', 'url') self.login = settings.get('WebDav', 'login') self.passwd = settings.get('WebDav', 'password') self.basepath = requests.utils.urlparse(self.url).path self.remotefolder = settings.get('WebDav', 'remoteFolder') self.nosslcheck = settings.get('WebDav', 'nosslcheck') self.timedelta = None self.wc = None self.locktoken = None def connect(self,): urlparsed = requests.utils.urlparse(self.url) self.wc = WebDAVClient(host=urlparsed.netloc, protocol=urlparsed.scheme) self.wc.setbasicauth(self.login.encode('utf-8'), self.passwd.encode('utf-8')) time_delta = None local_time = datetime.datetime.utcnow() response = self.wc.options('/').headers.get('date') if response is None: response = self.wc.options('/').headers.get('Date') remote_datetime = \ rfc822.parsedate(response) self.timedelta = time.mktime(local_time.utctimetuple()) \ - time.mktime(remote_datetime) self._check_notes_folder() return time_delta def _check_notes_folder(self,): response = self.wc.propfind(uri=self.basepath, names=True, depth=1) print type(response) print dir(response) ownnotes_folder_exists = False ownnotes_remote_folder = self.get_abspath('') if response.real != 207: is_connected = False else: is_connected = True for res in response: if (res.href == ownnotes_remote_folder): ownnotes_folder_exists = True if not ownnotes_folder_exists: self.wc.mkcol(ownnotes_remote_folder) return is_connected def exists_or_create(self, relpath): response = self.wc.propfind(uri=self.get_abspath(''), names=True, depth=1) if response.real != 207: return False else: for res in response: if (res.href == self.get_abspath(relpath)): return True self.wc.mkcol(self.get_abspath(relpath)) def upload(self, fh, relpath): self.wc.put(self.get_abspath(relpath), fh) def download(self, relpath, fh): fh.write(self.wc.get(self.get_abspath(relpath)).content) def rm(self, relpath): self.wc.delete(self.get_abspath(relpath)) def get_abspath(self, relpath, asFolder=False): abspath = self.basepath if abspath.endswith('/'): abspath += self.remotefolder else: abspath += '/' + self.remotefolder if abspath.endswith('/'): abspath += relpath else: abspath += '/' + relpath if asFolder and not abspath.endswith('/'): abspath += '/' return abspath def get_relpath(self, abspath): basepath = self.basepath if basepath.endswith('/'): basepath += self.remotefolder else: basepath += '/' + self.remotefolder return os.path.relpath(abspath, basepath) def get_mtime(self, relpath): response = self.wc.propfind(uri=self.get_abspath(relpath)) if response.real != 207: raise NetworkError('Can\'t get mtime file on webdav host') else: for res in response: return round(time.mktime(rfc822.parsedate( res.get('getlastmodified').text))) def get_files_index(self,): index = {} abspath = self.get_abspath('', asFolder=True) response = self.wc.propfind(uri=abspath, names=True, depth='infinity') print response.real if response.real != 207: raise NetworkError('Can\'t list file on webdav host') else: for res in response: print(res.href) if len(res.get('resourcetype').getchildren()) == 0: index[self.get_relpath(res.href)] = \ round(time.mktime(rfc822.parsedate( res.get('getlastmodified').text))) return index def move(self, srcrelpath, dstrelpath): '''Move/Rename a note on webdav''' self.wc.move(self.get_abspath(srcrelpath), self.get_abspath(dstrelpath), depth='infinity', overwrite=True) def lock(self, relpath=''): abspath = self.get_abspath(relpath) if relpath: self.locktoken = self.wc.lock(uri=abspath, timeout=60) else: self.locktoken = self.wc.lock(uri=abspath, depth='infinity', timeout=300) def unlock(self, relpath=None): if self.locktoken: self.wc.unlock(uri_or_lock=self.locktoken)
class WebdavClient(object): def __init__(self, ): settings = Settings() self.url = settings.get('WebDav', 'url') self.login = settings.get('WebDav', 'login') self.passwd = settings.get('WebDav', 'password') self.basepath = requests.utils.urlparse(self.url).path self.remotefolder = settings.get('WebDav', 'remoteFolder') self.nosslcheck = settings.get('WebDav', 'nosslcheck') self.timedelta = None self.wc = None self.locktoken = None def connect(self, ): urlparsed = requests.utils.urlparse(self.url) self.wc = WebDAVClient(host=urlparsed.netloc, protocol=urlparsed.scheme) self.wc.setbasicauth(self.login.encode('utf-8'), self.passwd.encode('utf-8')) time_delta = None local_time = datetime.datetime.utcnow() response = self.wc.options('/').headers.get('date') if response is None: response = self.wc.options('/').headers.get('Date') remote_datetime = \ rfc822.parsedate(response) self.timedelta = time.mktime(local_time.utctimetuple()) \ - time.mktime(remote_datetime) self._check_notes_folder() return time_delta def _check_notes_folder(self, ): response = self.wc.propfind(uri=self.basepath, names=True, depth=1) print type(response) print dir(response) ownnotes_folder_exists = False ownnotes_remote_folder = self.get_abspath('') if response.real != 207: is_connected = False else: is_connected = True for res in response: if (res.href == ownnotes_remote_folder): ownnotes_folder_exists = True if not ownnotes_folder_exists: self.wc.mkcol(ownnotes_remote_folder) return is_connected def exists_or_create(self, relpath): response = self.wc.propfind(uri=self.get_abspath(''), names=True, depth=1) if response.real != 207: return False else: for res in response: if (res.href == self.get_abspath(relpath)): return True self.wc.mkcol(self.get_abspath(relpath)) def upload(self, fh, relpath): self.wc.put(self.get_abspath(relpath), fh) def download(self, relpath, fh): fh.write(self.wc.get(self.get_abspath(relpath)).content) def rm(self, relpath): self.wc.delete(self.get_abspath(relpath)) def get_abspath(self, relpath, asFolder=False): abspath = self.basepath if abspath.endswith('/'): abspath += self.remotefolder else: abspath += '/' + self.remotefolder if abspath.endswith('/'): abspath += relpath else: abspath += '/' + relpath if asFolder and not abspath.endswith('/'): abspath += '/' return abspath def get_relpath(self, abspath): basepath = self.basepath if basepath.endswith('/'): basepath += self.remotefolder else: basepath += '/' + self.remotefolder return os.path.relpath(abspath, basepath) def get_mtime(self, relpath): response = self.wc.propfind(uri=self.get_abspath(relpath)) if response.real != 207: raise NetworkError('Can\'t get mtime file on webdav host') else: for res in response: return round( time.mktime( rfc822.parsedate(res.get('getlastmodified').text))) def get_files_index(self, ): index = {} abspath = self.get_abspath('', asFolder=True) response = self.wc.propfind(uri=abspath, names=True, depth='infinity') print response.real if response.real != 207: raise NetworkError('Can\'t list file on webdav host') else: for res in response: print(res.href) if len(res.get('resourcetype').getchildren()) == 0: index[self.get_relpath(res.href)] = \ round(time.mktime(rfc822.parsedate( res.get('getlastmodified').text))) return index def move(self, srcrelpath, dstrelpath): '''Move/Rename a note on webdav''' self.wc.move(self.get_abspath(srcrelpath), self.get_abspath(dstrelpath), depth='infinity', overwrite=True) def lock(self, relpath=''): abspath = self.get_abspath(relpath) if relpath: self.locktoken = self.wc.lock(uri=abspath, timeout=60) else: self.locktoken = self.wc.lock(uri=abspath, depth='infinity', timeout=300) def unlock(self, relpath=None): if self.locktoken: self.wc.unlock(uri_or_lock=self.locktoken)