diff options
author | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-03-31 21:30:34 +0000 |
---|---|---|
committer | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-03-31 21:30:34 +0000 |
commit | 838bfcd555fd6e4cd7eceaa7ccdbbdc78986cc06 (patch) | |
tree | 5547f10f29e28894ae70ff5b97e858456f94103a /fs/httpfs.py | |
parent | 8c1e089ad130972b7867b393226b276f56c82ecb (diff) | |
download | pyfilesystem-838bfcd555fd6e4cd7eceaa7ccdbbdc78986cc06.tar.gz |
Change of api (fs.open, fs.setcontent, fs.getcontents) to support io module in Py2.6+ and Py3
git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@854 67cdc799-7952-0410-af00-57a81ceafa0f
Diffstat (limited to 'fs/httpfs.py')
-rw-r--r-- | fs/httpfs.py | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/fs/httpfs.py b/fs/httpfs.py index 44c0498..65514f4 100644 --- a/fs/httpfs.py +++ b/fs/httpfs.py @@ -8,41 +8,45 @@ fs.httpfs from fs.base import FS from fs.path import normpath from fs.errors import ResourceNotFoundError, UnsupportedError +from fs.filelike import FileWrapper +from fs import iotools + from urllib2 import urlopen, URLError from datetime import datetime -from fs.filelike import FileWrapper + class HTTPFS(FS): - - """Can barely be called a filesystem, because HTTP servers generally don't support + + """Can barely be called a filesystem, because HTTP servers generally don't support typical filesystem functionality. This class exists to allow the :doc:`opener` system - to read files over HTTP. - + to read files over HTTP. + If you do need filesystem like functionality over HTTP, see :mod:`fs.contrib.davfs`. - + """ - - _meta = {'read_only':True, - 'network':True,} - + + _meta = {'read_only': True, + 'network': True} + def __init__(self, url): """ - + :param url: The base URL - + """ self.root_url = url - + def _make_url(self, path): path = normpath(path) url = '%s/%s' % (self.root_url.rstrip('/'), path.lstrip('/')) return url - def open(self, path, mode="r"): - + @iotools.filelike_to_stream + def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): + if '+' in mode or 'w' in mode or 'a' in mode: raise UnsupportedError('write') - + url = self._make_url(path) try: f = urlopen(url) @@ -50,15 +54,15 @@ class HTTPFS(FS): raise ResourceNotFoundError(path, details=e) except OSError, e: raise ResourceNotFoundError(path, details=e) - + return FileWrapper(f) - + def exists(self, path): return self.isfile(path) - + def isdir(self, path): return False - + def isfile(self, path): url = self._make_url(path) f = None @@ -70,9 +74,9 @@ class HTTPFS(FS): finally: if f is not None: f.close() - + return True - + def listdir(self, path="./", wildcard=None, full=False, |