Commit d72a995f authored by J. Fernando Sánchez's avatar J. Fernando Sánchez
Browse files

New shelf location and better shelf tests

parent 40b67503
...@@ -4,5 +4,4 @@ ...@@ -4,5 +4,4 @@
dist dist
build build
README.html README.html
__pycache__ __pycache__
Dockerfile-* \ No newline at end of file
Dockerfile-3.4
\ No newline at end of file
...@@ -8,7 +8,7 @@ VERSION=$(shell cat $(NAME)/VERSION) ...@@ -8,7 +8,7 @@ VERSION=$(shell cat $(NAME)/VERSION)
all: build run all: build run
dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS)) dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS))
ln -s Dockerfile-{PYMAIN} Dockerfile ln -s Dockerfile-$(PYMAIN) Dockerfile
Dockerfile-%: Dockerfile.template Dockerfile-%: Dockerfile.template
sed "s/{{PYVERSION}}/$*/" Dockerfile.template > Dockerfile-$* sed "s/{{PYVERSION}}/$*/" Dockerfile.template > Dockerfile-$*
...@@ -24,8 +24,11 @@ test: $(addprefix test-,$(PYMAIN)) ...@@ -24,8 +24,11 @@ test: $(addprefix test-,$(PYMAIN))
testall: $(addprefix test-,$(PYVERSIONS)) testall: $(addprefix test-,$(PYVERSIONS))
debug-%: build-%
docker run --rm -w /usr/src/app/ -v $$PWD:/usr/src/app --entrypoint=/bin/bash -ti '$(REPO)/$(NAME):$(VERSION)-python$*' ;
test-%: build-% test-%: build-%
docker run --rm -w /usr/src/app/ --entrypoint=/usr/local/bin/python -ti '$(REPO)/$(NAME):$(VERSION)-python$*' setup.py test --addopts "-vvv -s --pdb" ; docker run --rm -w /usr/src/app/ --entrypoint=/usr/local/bin/python -ti '$(REPO)/$(NAME):$(VERSION)-python$*' setup.py test --addopts "-vvv -s" ;
dist/$(NAME)-$(VERSION).tar.gz: dist/$(NAME)-$(VERSION).tar.gz:
docker run --rm -ti -v $$PWD:/usr/src/app/ -w /usr/src/app/ python:$(PYMAIN) python setup.py sdist; docker run --rm -ti -v $$PWD:/usr/src/app/ -w /usr/src/app/ python:$(PYMAIN) python setup.py sdist;
...@@ -38,7 +41,7 @@ pip_test-%: sdist ...@@ -38,7 +41,7 @@ pip_test-%: sdist
pip_test: $(addprefix pip_test-,$(PYVERSIONS)) pip_test: $(addprefix pip_test-,$(PYVERSIONS))
upload-%: test-% upload-%: test-%
docker push '$(REPO)/$(NAME):$(VERSION)-python$(PYMAIN)' docker push '$(REPO)/$(NAME):$(VERSION)-python$*'
upload: testall $(addprefix upload-,$(PYVERSIONS)) upload: testall $(addprefix upload-,$(PYVERSIONS))
docker tag '$(REPO)/$(NAME):$(VERSION)-python$(PYMAIN)' '$(REPO)/$(NAME):$(VERSION)' docker tag '$(REPO)/$(NAME):$(VERSION)-python$(PYMAIN)' '$(REPO)/$(NAME):$(VERSION)'
......
...@@ -19,7 +19,8 @@ Sentiment analysis server in Python ...@@ -19,7 +19,8 @@ Sentiment analysis server in Python
""" """
import os import os
VFILE = os.path.join(os.path.dirname(__file__), "VERSION")
with open(VFILE, 'r') as f: from .version import __version__
__version__ = f.read().strip()
__all__ = ['api', 'blueprints', 'cli', 'extensions', 'models', 'plugins']
...@@ -88,8 +88,9 @@ def main(): ...@@ -88,8 +88,9 @@ def main():
args.port)) args.port))
http_server.serve_forever() http_server.serve_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
http_server.stop()
print('Bye!') print('Bye!')
http_server.stop()
sp.deactivate_all()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -47,4 +47,3 @@ def main(): ...@@ -47,4 +47,3 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -5,6 +5,7 @@ import inspect ...@@ -5,6 +5,7 @@ import inspect
import os.path import os.path
import pickle import pickle
import logging import logging
import tempfile
from .models import Response, PluginModel, Error from .models import Response, PluginModel, Error
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -83,7 +84,7 @@ class ShelfMixin(object): ...@@ -83,7 +84,7 @@ class ShelfMixin(object):
if hasattr(self, '_info') and 'shelf_file' in self._info: if hasattr(self, '_info') and 'shelf_file' in self._info:
self.__dict__['_shelf_file'] = self._info['shelf_file'] self.__dict__['_shelf_file'] = self._info['shelf_file']
else: else:
self._shelf_file = os.path.join(self.get_folder(), self.name + '.p') self._shelf_file = os.path.join(tempfile.gettempdir(), self.name + '.p')
return self._shelf_file return self._shelf_file
def save(self): def save(self):
......
import os
with open(os.path.join(os.path.dirname(__file__), 'VERSION')) as f:
__version__ = f.read().strip()
...@@ -15,8 +15,7 @@ except AttributeError: ...@@ -15,8 +15,7 @@ except AttributeError:
install_reqs = [str(ir.req) for ir in install_reqs] install_reqs = [str(ir.req) for ir in install_reqs]
test_reqs = [str(ir.req) for ir in test_reqs] test_reqs = [str(ir.req) for ir in test_reqs]
with open('senpy/VERSION') as f: from senpy import __version__
__version__ = f.read().strip()
setup( setup(
name='senpy', name='senpy',
......
...@@ -9,22 +9,29 @@ import tempfile ...@@ -9,22 +9,29 @@ import tempfile
import json import json
import os import os
from unittest import TestCase from unittest import TestCase
from flask import Flask
from senpy.models import Results, Entry from senpy.models import Results, Entry
from senpy.plugins import SenpyPlugin, ShelfMixin from senpy.plugins import SentimentPlugin, ShelfMixin
class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
class ShelfTest(ShelfMixin, SenpyPlugin): def activate(self, *args, **kwargs):
if 'counter' not in self.sh:
def test(self, key=None, value=None): self.sh['counter'] = 0
assert key in self.sh self.save()
print('Checking: sh[{}] == {}'.format(key, value))
print('SH[{}]: {}'.format(key, self.sh[key]))
assert self.sh[key] == value
def deactivate(self, *args, **kwargs):
self.save()
class ModelsTest(TestCase): def analyse(self, *args, **kwargs):
self.sh['counter'] = self.sh['counter']+1
e = Entry()
e.nif__isString = self.sh['counter']
r = Results()
r.entries.append(e)
return r
class PluginsTest(TestCase):
def tearDown(self): def tearDown(self):
if os.path.exists(self.shelf_dir): if os.path.exists(self.shelf_dir):
...@@ -37,16 +44,26 @@ class ModelsTest(TestCase): ...@@ -37,16 +44,26 @@ class ModelsTest(TestCase):
self.shelf_dir = tempfile.mkdtemp() self.shelf_dir = tempfile.mkdtemp()
self.shelf_file = os.path.join(self.shelf_dir, "shelf") self.shelf_file = os.path.join(self.shelf_dir, "shelf")
def test_shelf_file(self):
a = ShelfDummyPlugin(info={'name': 'default_shelve_file',
'version': 'test'})
assert os.path.dirname(a.shelf_file) == tempfile.gettempdir()
a.activate()
assert os.path.isfile(a.shelf_file)
os.remove(a.shelf_file)
def test_shelf(self): def test_shelf(self):
''' A shelf is created and the value is stored ''' ''' A shelf is created and the value is stored '''
a = ShelfTest(info={'name': 'shelve', a = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test', 'version': 'test',
'shelf_file': self.shelf_file}) 'shelf_file': self.shelf_file})
assert a.sh == {} assert a.sh == {}
a.activate()
assert a.sh == {'counter': 0}
assert a.shelf_file == self.shelf_file assert a.shelf_file == self.shelf_file
a.sh['a'] = 'fromA' a.sh['a'] = 'fromA'
a.test(key='a', value='fromA') assert a.sh['a'] == 'fromA'
a.save() a.save()
...@@ -54,19 +71,31 @@ class ModelsTest(TestCase): ...@@ -54,19 +71,31 @@ class ModelsTest(TestCase):
assert sh['a'] == 'fromA' assert sh['a'] == 'fromA'
def test_dummy_shelf(self):
a = ShelfDummyPlugin(info={'name': 'DummyShelf',
'shelf_file': self.shelf_file,
'version': 'test'})
a.activate()
res1 = a.analyse(input=1)
assert res1.entries[0].nif__isString == 1
res2 = a.analyse(input=1)
assert res2.entries[0].nif__isString == 2
def test_two(self): def test_two(self):
''' Reusing the values of a previous shelf ''' ''' Reusing the values of a previous shelf '''
a = ShelfTest(info={'name': 'shelve', a = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test', 'version': 'test',
'shelf_file': self.shelf_file}) 'shelf_file': self.shelf_file})
a.activate()
print('Shelf file: %s' % a.shelf_file) print('Shelf file: %s' % a.shelf_file)
a.sh['a'] = 'fromA' a.sh['a'] = 'fromA'
a.save() a.save()
b = ShelfTest(info={'name': 'shelve', b = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test', 'version': 'test',
'shelf_file': self.shelf_file}) 'shelf_file': self.shelf_file})
b.test(key='a', value='fromA') b.activate()
assert b.sh['a'] == 'fromA'
b.sh['a'] = 'fromB' b.sh['a'] = 'fromB'
assert b.sh['a'] == 'fromB' assert b.sh['a'] == 'fromB'
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment