view MoinMoin/storage/stores/ @ 1917:16ea00a08d51

create mixin classes that transform bytes <-> file stores often (but maybe not always), the underlying storage system just offers either a bytes- or files-oriented interface, not both. but on the backend layer, we need a BytesStore for the metadata and a FileStore for the data.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 20 Jan 2013 21:55:30 +0100
parents 4ac437141bbe
line wrap: on
line source
# Copyright: 2011 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

MoinMoin - filesystem store

Store into filesystem, one file per k/v pair.

from __future__ import absolute_import, division

import os
import errno
import shutil

from . import (BytesMutableStoreBase, FileMutableStoreBase,
               BytesMutableStoreMixin, FileMutableStoreMixin)

class FileStore(FileMutableStoreBase):
    A simple filesystem-based store.

    keys are required to be valid filenames.
    def from_uri(cls, uri):
        return cls(uri)

    def __init__(self, path):
        :param path: base directory used for this store
        self.path = path

    def create(self):
        except OSError as e:
            if e.errno != errno.EEXIST:

    def destroy(self):

    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):

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

    def __setitem__(self, key, stream):
        with open(self._mkpath(key), "wb") as f:
            blocksize = 64 * 1024
            shutil.copyfileobj(stream, f, blocksize)

class BytesStore(BytesMutableStoreMixin, FileStore, BytesMutableStoreBase):
    """filesystem BytesStore"""