diff options
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, |