1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-23 08:32:29 +00:00

New shelf location and better shelf tests

This commit is contained in:
J. Fernando Sánchez 2016-12-19 18:18:47 +01:00
parent 40b67503ce
commit d72a995fa9
11 changed files with 66 additions and 29 deletions

1
.gitignore vendored
View File

@ -5,4 +5,3 @@ dist
build
README.html
__pycache__
Dockerfile-*

1
Dockerfile Symbolic link
View File

@ -0,0 +1 @@
Dockerfile-3.4

View File

@ -8,7 +8,7 @@ VERSION=$(shell cat $(NAME)/VERSION)
all: build run
dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS))
ln -s Dockerfile-{PYMAIN} Dockerfile
ln -s Dockerfile-$(PYMAIN) Dockerfile
Dockerfile-%: Dockerfile.template
sed "s/{{PYVERSION}}/$*/" Dockerfile.template > Dockerfile-$*
@ -24,8 +24,11 @@ test: $(addprefix test-,$(PYMAIN))
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-%
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:
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
pip_test: $(addprefix pip_test-,$(PYVERSIONS))
upload-%: test-%
docker push '$(REPO)/$(NAME):$(VERSION)-python$(PYMAIN)'
docker push '$(REPO)/$(NAME):$(VERSION)-python$*'
upload: testall $(addprefix upload-,$(PYVERSIONS))
docker tag '$(REPO)/$(NAME):$(VERSION)-python$(PYMAIN)' '$(REPO)/$(NAME):$(VERSION)'

View File

@ -1 +1 @@
0.6.1
0.6.2

View File

@ -19,7 +19,8 @@ Sentiment analysis server in Python
"""
import os
VFILE = os.path.join(os.path.dirname(__file__), "VERSION")
with open(VFILE, 'r') as f:
__version__ = f.read().strip()
from .version import __version__
__all__ = ['api', 'blueprints', 'cli', 'extensions', 'models', 'plugins']

View File

@ -88,8 +88,9 @@ def main():
args.port))
http_server.serve_forever()
except KeyboardInterrupt:
http_server.stop()
print('Bye!')
http_server.stop()
sp.deactivate_all()
if __name__ == '__main__':
main()

View File

@ -47,4 +47,3 @@ def main():
if __name__ == '__main__':
main()

View File

@ -5,6 +5,7 @@ import inspect
import os.path
import pickle
import logging
import tempfile
from .models import Response, PluginModel, Error
logger = logging.getLogger(__name__)
@ -83,7 +84,7 @@ class ShelfMixin(object):
if hasattr(self, '_info') and 'shelf_file' in self._info:
self.__dict__['_shelf_file'] = self._info['shelf_file']
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
def save(self):

4
senpy/version.py Normal file
View File

@ -0,0 +1,4 @@
import os
with open(os.path.join(os.path.dirname(__file__), 'VERSION')) as f:
__version__ = f.read().strip()

View File

@ -15,8 +15,7 @@ except AttributeError:
install_reqs = [str(ir.req) for ir in install_reqs]
test_reqs = [str(ir.req) for ir in test_reqs]
with open('senpy/VERSION') as f:
__version__ = f.read().strip()
from senpy import __version__
setup(
name='senpy',

View File

@ -9,22 +9,29 @@ import tempfile
import json
import os
from unittest import TestCase
from flask import Flask
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:
self.sh['counter'] = 0
self.save()
def test(self, key=None, value=None):
assert key in self.sh
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()
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 ModelsTest(TestCase):
class PluginsTest(TestCase):
def tearDown(self):
if os.path.exists(self.shelf_dir):
@ -37,16 +44,26 @@ class ModelsTest(TestCase):
self.shelf_dir = tempfile.mkdtemp()
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):
''' A shelf is created and the value is stored '''
a = ShelfTest(info={'name': 'shelve',
a = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test',
'shelf_file': self.shelf_file})
assert a.sh == {}
a.activate()
assert a.sh == {'counter': 0}
assert a.shelf_file == self.shelf_file
a.sh['a'] = 'fromA'
a.test(key='a', value='fromA')
assert a.sh['a'] == 'fromA'
a.save()
@ -54,19 +71,31 @@ class ModelsTest(TestCase):
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):
''' Reusing the values of a previous shelf '''
a = ShelfTest(info={'name': 'shelve',
a = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test',
'shelf_file': self.shelf_file})
a.activate()
print('Shelf file: %s' % a.shelf_file)
a.sh['a'] = 'fromA'
a.save()
b = ShelfTest(info={'name': 'shelve',
b = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test',
'shelf_file': self.shelf_file})
b.test(key='a', value='fromA')
b.activate()
assert b.sh['a'] == 'fromA'
b.sh['a'] = 'fromB'
assert b.sh['a'] == 'fromB'