# Path: simplekv/idgen.py
# class UUIDDecorator(StoreDecorator):
#     """UUID generating decorator
# 
#     Overrides :meth:`.KeyValueStore.put` and :meth:`.KeyValueStore.put_file`.
#     If a key of *None* is passed, a new UUID will be generated as the key. The
#     attribute `uuidfunc` determines which UUID-function to use and defaults to
#     'uuid1'.
# 
#     .. note::
#        There seems to be a bug in the uuid module that prevents initializing
#        `uuidfunc` too early. For that reason, it is a string that will be
#        looked up using :func:`getattr` on the :mod:`uuid` module.
#     """
#     # for strange reasons, this needs to be looked up as late as possible
#     uuidfunc = 'uuid1'
# 
#     def __init__(self, store, template=u'{}'):
#         super(UUIDDecorator, self).__init__(store)
#         self._template = template
# 
#     def put(self, key, data, *args, **kwargs):
#         if not key:
#             key = text_type(getattr(uuid, self.uuidfunc)())
# 
#         return self._dstore.put(
#             self._template.format(key), data, *args, **kwargs
#         )
# 
#     def put_file(self, key, file, *args, **kwargs):
#         if not key:
#             key = text_type(getattr(uuid, self.uuidfunc)())
# 
#         return self._dstore.put_file(
#             self._template.format(key), file, *args, **kwargs
#         )
# 
# class HashDecorator(StoreDecorator):
#     """Hash function decorator
# 
#     Overrides :meth:`.KeyValueStore.put` and :meth:`.KeyValueStore.put_file`.
#     If a key of *None* is passed, the data/file is hashed using
#     ``hashfunc``, which defaults to *hashlib.sha1*. """
# 
#     def __init__(self, decorated_store, hashfunc=hashlib.sha1, template=u'{}'):
#         self.hashfunc = hashfunc
#         self._template = template
#         super(HashDecorator, self).__init__(decorated_store)
# 
#     def put(self, key, data, *args, **kwargs):
#         if not key:
#             key = self._template.format(self.hashfunc(data).hexdigest())
# 
#         return self._dstore.put(key, data, *args, **kwargs)
# 
#     def put_file(self, key, file, *args, **kwargs):
#         bufsize = 1024 * 1024
#         phash = self.hashfunc()
# 
#         if not key:
#             if isinstance(file, str):
#                 with open(file, 'rb') as source:
#                     while True:
#                         buf = source.read(bufsize)
#                         phash.update(buf)
# 
#                         if len(buf) < bufsize:
#                             break
# 
#                     return self._dstore.put_file(
#                         self._template.format(phash.hexdigest()),
#                         file, *args, **kwargs)
#             else:
#                 tmpfile = tempfile.NamedTemporaryFile(delete=False)
#                 try:
#                     while True:
#                         buf = file.read(bufsize)
#                         phash.update(buf)
#                         tmpfile.write(buf)
# 
#                         if len(buf) < bufsize:
#                             break
# 
#                     tmpfile.close()
#                     return self._dstore.put_file(
#                         self._template.format(phash.hexdigest()),
#                         tmpfile.name, *args, **kwargs
#                     )
#                 finally:
#                     try:
#                         os.unlink(tmpfile.name)
#                     except OSError as e:
#                         if 2 == e.errno:
#                             pass  # file already gone
#                         else:
#                             raise
#         return self._dstore.put_file(key, file, *args, **kwargs)
# 
# Path: simplekv/_compat.py
# PY2 = sys.version_info[0] == 2


import os
import re
import tempfile
import uuid
import pytest
from simplekv.idgen import UUIDDecorator, HashDecorator
from simplekv._compat import text_type
        key = uuidstore.put(None, value)
        uuid.UUID(hex=key)

    def test_put_file_generates_valid_uuid(self, uuidstore):
        key = uuidstore.put_file(None, open('/dev/null', 'rb'))
        uuid.UUID(hex=key)

        tmpfile = tempfile.NamedTemporaryFile(delete=False)
        try:
            tmpfile.close()
            key2 = uuidstore.put_file(None, tmpfile.name)
            uuid.UUID(hex=key2)
        finally:
            if os.path.exists(tmpfile.name):
                os.unlink(tmpfile.name)

    def test_templates_work(self, templated_uuidstore, value, idgen_template):
        key = templated_uuidstore.put(None, value)

        # should not be a valid UUID
        assert not UUID_REGEXP.match(key)


class HashGen(IDGen):
    @pytest.fixture
    def hashstore(self, store, hashfunc):
        return HashDecorator(store, hashfunc)

    @pytest.fixture
    def templated_hashstore(self, store, hashfunc, idgen_template):

# Path: simplekv/idgen.py
# class UUIDDecorator(StoreDecorator):
#     """UUID generating decorator
# 
#     Overrides :meth:`.KeyValueStore.put` and :meth:`.KeyValueStore.put_file`.
#     If a key of *None* is passed, a new UUID will be generated as the key. The
#     attribute `uuidfunc` determines which UUID-function to use and defaults to
#     'uuid1'.
# 
#     .. note::
#        There seems to be a bug in the uuid module that prevents initializing
#        `uuidfunc` too early. For that reason, it is a string that will be
#        looked up using :func:`getattr` on the :mod:`uuid` module.
#     """
#     # for strange reasons, this needs to be looked up as late as possible
#     uuidfunc = 'uuid1'
# 
#     def __init__(self, store, template=u'{}'):
#         super(UUIDDecorator, self).__init__(store)
#         self._template = template
# 
#     def put(self, key, data, *args, **kwargs):
#         if not key:
#             key = text_type(getattr(uuid, self.uuidfunc)())
# 
#         return self._dstore.put(
#             self._template.format(key), data, *args, **kwargs
#         )
# 
#     def put_file(self, key, file, *args, **kwargs):
#         if not key:
#             key = text_type(getattr(uuid, self.uuidfunc)())
# 
#         return self._dstore.put_file(
#             self._template.format(key), file, *args, **kwargs
#         )
# 
# class HashDecorator(StoreDecorator):
#     """Hash function decorator
# 
#     Overrides :meth:`.KeyValueStore.put` and :meth:`.KeyValueStore.put_file`.
#     If a key of *None* is passed, the data/file is hashed using
#     ``hashfunc``, which defaults to *hashlib.sha1*. """
# 
#     def __init__(self, decorated_store, hashfunc=hashlib.sha1, template=u'{}'):
#         self.hashfunc = hashfunc
#         self._template = template
#         super(HashDecorator, self).__init__(decorated_store)
# 
#     def put(self, key, data, *args, **kwargs):
#         if not key:
#             key = self._template.format(self.hashfunc(data).hexdigest())
# 
#         return self._dstore.put(key, data, *args, **kwargs)
# 
#     def put_file(self, key, file, *args, **kwargs):
#         bufsize = 1024 * 1024
#         phash = self.hashfunc()
# 
#         if not key:
#             if isinstance(file, str):
#                 with open(file, 'rb') as source:
#                     while True:
#                         buf = source.read(bufsize)
#                         phash.update(buf)
# 
#                         if len(buf) < bufsize:
#                             break
# 
#                     return self._dstore.put_file(
#                         self._template.format(phash.hexdigest()),
#                         file, *args, **kwargs)
#             else:
#                 tmpfile = tempfile.NamedTemporaryFile(delete=False)
#                 try:
#                     while True:
#                         buf = file.read(bufsize)
#                         phash.update(buf)
#                         tmpfile.write(buf)
# 
#                         if len(buf) < bufsize:
#                             break
# 
#                     tmpfile.close()
#                     return self._dstore.put_file(
#                         self._template.format(phash.hexdigest()),
#                         tmpfile.name, *args, **kwargs
#                     )
#                 finally:
#                     try:
#                         os.unlink(tmpfile.name)
#                     except OSError as e:
#                         if 2 == e.errno:
#                             pass  # file already gone
#                         else:
#                             raise
#         return self._dstore.put_file(key, file, *args, **kwargs)
# 
# Path: simplekv/_compat.py
# PY2 = sys.version_info[0] == 2


# Path: tests/idgens.py
import os
import re
import tempfile
import uuid
import pytest
from simplekv.idgen import UUIDDecorator, HashDecorator
from simplekv._compat import text_type

        key = uuidstore.put(None, value)
        uuid.UUID(hex=key)

    def test_put_file_generates_valid_uuid(self, uuidstore):
        key = uuidstore.put_file(None, open('/dev/null', 'rb'))
        uuid.UUID(hex=key)

        tmpfile = tempfile.NamedTemporaryFile(delete=False)
        try:
            tmpfile.close()
            key2 = uuidstore.put_file(None, tmpfile.name)
            uuid.UUID(hex=key2)
        finally:
            if os.path.exists(tmpfile.name):
                os.unlink(tmpfile.name)

    def test_templates_work(self, templated_uuidstore, value, idgen_template):
        key = templated_uuidstore.put(None, value)

        # should not be a valid UUID
        assert not UUID_REGEXP.match(key)


class HashGen(IDGen):
    @pytest.fixture
    def hashstore(self, store, hashfunc):
        return HashDecorator(store, hashfunc)

    @pytest.fixture
    def templated_hashstore(self, store, hashfunc, idgen_template):

        return HashDecorator(store, hashfunc, idgen_template)