Source

storage-ng / stores / fs.py

# Copyright: 2011 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - filesystem store
"""


from __future__ import absolute_import, division

import os
import errno
import shutil

from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase


class _Store(MutableStoreBase):
    """
    A simple filesystem-based store.

    keys are required to be valid filenames.
    """
    def __init__(self, path):
        """
        :param path: base directory used for this store
        """
        self.path = path

    def create(self):
        os.mkdir(self.path)

    def destroy(self):
        shutil.rmtree(self.path)

    def _mkpath(self, key):
        # XXX unsafe keys?
        return os.path.join(self.path, key)

    def __iter__(self):
        for key in os.listdir(self.path):
            yield key

    def __delitem__(self, key):
        os.remove(self._mkpath(key))


class BytesStore(_Store, BytesMutableStoreBase):
    def __getitem__(self, key):
        try:
            with open(self._mkpath(key), 'rb') as f:
                return f.read() # better use get_file() and read smaller blocks for big files
        except IOError as e:
            if e.errno == errno.ENOENT:
                raise KeyError(key)
            raise

    def __setitem__(self, key, value):
        with open(self._mkpath(key), "wb") as f:
            f.write(value)


class FileStore(_Store, FileMutableStoreBase):
    def __getitem__(self, key):
        try:
            return open(self._mkpath(key), 'rb')
        except IOError as e:
            if e.errno == errno.ENOENT:
                raise KeyError(key)
            raise

    def __setitem__(self, key, stream):
        with open(self._mkpath(key), "wb") as f:
            blocksize = 64 * 1024
            shutil.copyfileobj(stream, f, blocksize)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.