Added shelve mixin

pull/8/head
J. Fernando Sánchez 9 years ago
parent bd2e0f0d5c
commit b8993f7d64

@ -71,7 +71,10 @@ class Leaf(dict):
return id
def __delattr__(self, key):
return super(Leaf, self).__delitem__(self._get_key(key))
if key in self.__dict__:
del self.__dict__[key]
else:
super(Leaf, self).__delitem__(self._get_key(key))
def _get_key(self, key):
if key[0] == "_":

@ -1,4 +1,7 @@
import inspect
import os.path
import shelve
import logging
import ConfigParser
from .models import Response, Leaf
@ -85,6 +88,9 @@ class SenpyPlugin(Leaf):
self.is_activated = False
self._info = info
def get_folder(self):
return os.path.dirname(inspect.getfile(self.__class__))
def analyse(self, *args, **kwargs):
logger.debug("Analysing with: {} {}".format(self.name, self.version))
pass
@ -121,3 +127,29 @@ class EmotionPlugin(SenpyPlugin):
resp = super(EmotionPlugin, self).__init__(info, *args, **kwargs)
self.minEmotionValue = float(info.get("minEmotionValue", 0))
self.maxEmotionValue = float(info.get("maxEmotionValue", 0))
class ShelfMixin(object):
@property
def sh(self):
if not hasattr(self, '_sh') or not self._sh:
self._sh = shelve.open(self.shelf_file, writeback=True)
return self._sh
@sh.deleter
def sh(self):
if hasattr(self, '_sh'):
del(self._sh)
@property
def shelf_file(self):
if not hasattr(self, '_shelf_file') or not self._shelf_file:
if hasattr(self, '_info') and 'shelf_file' in self._info:
self._shelf_file = self._info['shelf_file']
else:
self._shelf_file = os.path.join(self.get_folder(), self.name + '.db')
return self._shelf_file
def close(self):
del(self._sh)

@ -0,0 +1,41 @@
import os
import logging
import shelve
try:
import unittest.mock as mock
except ImportError:
import mock
import json
import os
from unittest import TestCase
from senpy.models import Response, Entry
from senpy.plugins import SenpyPlugin, ShelfMixin
class ModelsTest(TestCase):
# def test_shelf(self):
# class ShelfTest(ShelfMixin):
# pass
# a = ShelfTest()
# print(type(a.sh))
# assert(False)
def test_shelf(self):
class ShelfTest(ShelfMixin, SenpyPlugin):
pass
a = ShelfTest({'name': 'shelve', 'version': 'test'})
print(type(a.sh))
a.context = "ohno"
del a.context
print(a)
a.sh['classifier'] = {'name': 'ohno'}
assert a.name == 'shelve'
assert isinstance(a.sh, shelve.Shelf)
a.close()
b = ShelfTest({'name': 'shelve', 'version': 'test'})
assert b.name == 'shelve'
assert b.sh['classifier']
assert b.sh['classifier']['name'] == 'ohno'
assert isinstance(b.sh, shelve.Shelf)
Loading…
Cancel
Save