moin-2.0 / build / lib / MoinMoin / storage / stores / memory.py

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

"""
MoinMoin - memory store

Stores k/v pairs into memory (RAM, non-persistent!).

Note: likely this is mostly useful for unit tests.
"""


from __future__ import absolute_import, division

from StringIO import StringIO

from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase


class _Store(MutableStoreBase):
    """
    A simple dict-based in-memory store. No persistence!
    """
    @classmethod
    def from_uri(cls, uri):
        return cls()

    def __init__(self):
        self._st = None
        self.__st = None

    def create(self):
        self.__st = {}

    def destroy(self):
        self.__st = None

    def open(self):
        self._st = self.__st

    def close(self):
        self._st = None

    def __iter__(self):
        for key in self._st:
            yield key

    def __delitem__(self, key):
        del self._st[key]


class BytesStore(_Store, BytesMutableStoreBase):
    def __getitem__(self, key):
        return self._st[key]

    def __setitem__(self, key, value):
        self._st[key] = value


class FileStore(_Store, FileMutableStoreBase):
    def __getitem__(self, key):
        return StringIO(self._st[key])

    def __setitem__(self, key, stream):
        self._st[key] = stream.read()
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.