1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-24 17:12:29 +00:00

Flake8, Semver, Pre-commit

* Added pre-commit: http://pre-commit.com
* Fixed flake8 errors
* Added flake8 pre-commit hooks
* Added pre-commit to Makefile
* Changed VERSION numbering
* Changed versioning to match PEP-0440
This commit is contained in:
J. Fernando Sánchez 2017-01-10 11:10:10 +01:00
parent 7fd69cc690
commit db30257373
21 changed files with 59 additions and 102 deletions

View File

@ -11,6 +11,10 @@ yapf:
yapf -i -r senpy
yapf -i -r tests
dev:
pip install --user pre-commit
pre-commit install
dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS))
ln -s Dockerfile-$(PYMAIN) Dockerfile
@ -75,4 +79,4 @@ pip_test: $(addprefix pip_test-,$(PYVERSIONS))
run: build
docker run --rm -p 5000:5000 -ti '$(REPO)/$(NAME):$(VERSION)-python$(PYMAIN)'
.PHONY: test test-% build-% build test test_pip run yapf
.PHONY: test test-% build-% build test test_pip run yapf dev

43
app.py
View File

@ -1,43 +0,0 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes
# DIT, UPM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This is a helper for development. If you want to run Senpy use:
python -m senpy
"""
from gevent.monkey import patch_all; patch_all()
import gevent
import config
from flask import Flask
from senpy.extensions import Senpy
import logging
import os
from gevent.wsgi import WSGIServer
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
mypath = os.path.dirname(os.path.realpath(__file__))
sp = Senpy(app, os.path.join(mypath, "plugins"), default_plugins=True)
sp.activate_all()
if __name__ == '__main__':
import logging
logging.basicConfig(level=config.DEBUG)
app.debug = config.DEBUG
http_server = WSGIServer(('', config.SERVER_PORT), app)
http_server.serve_forever()

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# flake8: noqa
#
# Senpy documentation build configuration file, created by
# sphinx-quickstart on Tue Feb 24 08:57:32 2015.

View File

@ -9,3 +9,4 @@ future
jsonschema
jsonref
PyYAML
semver

View File

@ -1 +1 @@
pre-0.7.0
0.7.0-dev1

View File

@ -17,9 +17,24 @@
"""
Sentiment analysis server in Python
"""
import os
from __future__ import print_function
from .version import __version__
try:
import semver
__version_info__ = semver.parse_version_info(__version__)
if __version_info__.prerelease:
import logging
logger = logging.getLogger(__name__)
msg = 'WARNING: You are using a pre-release version of {} ({})'.format(
__name__, __version__)
if len(logging.root.handlers) > 0:
logger.info(msg)
else:
import sys
print(msg, file=sys.stderr)
except ImportError:
print('semver not installed. Not doing version checking')
__all__ = ['api', 'blueprints', 'cli', 'extensions', 'models', 'plugins']

View File

@ -24,7 +24,6 @@ from flask import Flask
from senpy.extensions import Senpy
from gevent.wsgi import WSGIServer
from gevent.monkey import patch_all
import gevent
import logging
import os
import argparse
@ -77,7 +76,7 @@ def main():
'-i',
action='store_true',
default=False,
help='Do not run a server, only install the dependencies of the plugins.'
help='Do not run a server, only install plugin dependencies'
)
args = parser.parse_args()
logging.basicConfig()

View File

@ -1,9 +1,8 @@
from future.utils import iteritems
from .models import Error
import logging
logger = logging.getLogger(__name__)
from .models import Error
API_PARAMS = {
"algorithm": {
"aliases": ["algorithm", "a", "algo"],

View File

@ -17,12 +17,12 @@
"""
Blueprints for Senpy
"""
from flask import Blueprint, request, current_app, render_template, url_for, jsonify
from flask import (Blueprint, request, current_app,
render_template, url_for, jsonify)
from .models import Error, Response, Plugins, read_schema
from .api import NIF_PARAMS, WEB_PARAMS, parse_params
from .api import WEB_PARAMS, parse_params
from functools import wraps
import json
import logging
logger = logging.getLogger(__name__)
@ -105,9 +105,7 @@ def plugins():
@api_blueprint.route('/plugins/<plugin>/<action>', methods=['POST', 'GET'])
@basic_api
def plugin(plugin=None, action="list"):
filt = {}
sp = current_app.senpy
plugs = sp.filter_plugins(name=plugin)
if plugin == 'default' and sp.default_plugin:
response = sp.default_plugin
plugin = response.name
@ -123,11 +121,3 @@ def plugin(plugin=None, action="list"):
return Response(message="Ok")
else:
return Error(message="action '{}' not allowed".format(action))
if __name__ == '__main__':
import config
app.register_blueprint(api_blueprint)
app.debug = config.DEBUG
app.run(host='0.0.0.0', port=5000)

View File

@ -6,7 +6,7 @@ import gevent
from gevent import monkey
monkey.patch_all()
from .plugins import SenpyPlugin, SentimentPlugin, EmotionPlugin
from .plugins import SentimentPlugin
from .models import Error
from .blueprints import api_blueprint, demo_blueprint
from .api import API_PARAMS, NIF_PARAMS, parse_params
@ -21,7 +21,6 @@ import sys
import imp
import logging
import traceback
import gevent
import yaml
import pip
@ -230,7 +229,6 @@ class Senpy(object):
return None, None
module = info["module"]
name = info["name"]
requirements = info.get("requirements", [])
sys.path.append(root)
(fp, pathname, desc) = imp.find_module(module, [root, ])
try:

View File

@ -2,8 +2,8 @@
Senpy Models.
This implementation should mirror the JSON schema definition.
For compatibility with Py3 and for easier debugging, this new version drops introspection
and adds all arguments to the models.
For compatibility with Py3 and for easier debugging, this new version drops
introspection and adds all arguments to the models.
'''
from __future__ import print_function
from six import string_types
@ -124,9 +124,9 @@ class SenpyMixin(object):
context = self.context.copy()
if hasattr(self, 'prefix'):
# This sets @base for the document, which will be used in
# all relative URIs will. For example, if a uri is "Example" and
# prefix =s "http://example.com", the absolute URI after expanding
# with JSON-LD will be "http://example.com/Example"
# all relative URIs. For example, if a uri is "Example" and
# prefix =s "http://example.com", the absolute URI after
# expanding with JSON-LD will be "http://example.com/Example"
prefix_context = {"@base": self.prefix}
if isinstance(context, list):

View File

@ -6,7 +6,7 @@ import os.path
import pickle
import logging
import tempfile
from .models import Response, PluginModel, Error
from .models import PluginModel, Error
logger = logging.getLogger(__name__)
@ -50,7 +50,6 @@ class SentimentPlugin(SenpyPlugin):
class EmotionPlugin(SenpyPlugin):
def __init__(self, info, *args, **kwargs):
resp = super(EmotionPlugin, self).__init__(info, *args, **kwargs)
self.minEmotionValue = float(info.get("minEmotionValue", 0))
self.maxEmotionValue = float(info.get("maxEmotionValue", 0))
self["@type"] = "onyx:EmotionAnalysis"

View File

@ -1,4 +1,3 @@
import json
import random
from senpy.plugins import SentimentPlugin

View File

@ -2,3 +2,8 @@
description-file = README.rst
[aliases]
test=pytest
[flake8]
# because of the way that future works, we need to call install_aliases before
# finishing the imports. flake8 thinks that we're doing the imports too late,
# but it's actually ok
ignore = E402

View File

@ -4,8 +4,10 @@ from pip.req import parse_requirements
# parse_requirements() returns generator of pip.req.InstallRequirement objects
try:
install_reqs = parse_requirements("requirements.txt", session=pip.download.PipSession())
test_reqs = parse_requirements("test-requirements.txt", session=pip.download.PipSession())
install_reqs = parse_requirements(
"requirements.txt", session=pip.download.PipSession())
test_reqs = parse_requirements(
"test-requirements.txt", session=pip.download.PipSession())
except AttributeError:
install_reqs = parse_requirements("requirements.txt")
test_reqs = parse_requirements("test-requirements.txt")
@ -28,17 +30,15 @@ extendable, so new algorithms and sources can be used.
author='J. Fernando Sanchez',
author_email='balkian@gmail.com',
url='https://github.com/gsi-upm/senpy', # use the URL to the github repo
download_url='https://github.com/gsi-upm/senpy/archive/{}.tar.gz' .format(__version__),
download_url='https://github.com/gsi-upm/senpy/archive/{}.tar.gz'.format(
__version__),
keywords=['eurosentiment', 'sentiment', 'emotions', 'nif'],
classifiers=[],
install_requires=install_reqs,
tests_require=test_reqs,
setup_requires=['pytest-runner',],
setup_requires=['pytest-runner', ],
include_package_data=True,
entry_points={
'console_scripts': [
'senpy = senpy.__main__:main',
'senpy-cli = senpy.cli:main'
]
}
)
'console_scripts':
['senpy = senpy.__main__:main', 'senpy-cli = senpy.cli:main']
})

View File

@ -103,7 +103,7 @@ class BlueprintsTest(TestCase):
self.assertCode(resp, 200)
js = parse_resp(resp)
assert "is_activated" in js
assert js["is_activated"] == False
assert not js["is_activated"]
resp = self.client.get("/api/plugins/Dummy/activate")
self.assertCode(resp, 200)
sleep(0.5)
@ -111,7 +111,7 @@ class BlueprintsTest(TestCase):
self.assertCode(resp, 200)
js = parse_resp(resp)
assert "is_activated" in js
assert js["is_activated"] == True
assert js["is_activated"]
def test_default(self):
""" Show only one plugin"""

View File

@ -1,4 +1,3 @@
import os
import logging
from functools import partial

View File

@ -44,6 +44,7 @@ class ExtensionsTest(TestCase):
assert name == 'TestPip'
assert module
import noop
dir(noop)
def test_installing(self):
""" Enabling a plugin """

View File

@ -1,21 +1,16 @@
import os
import logging
import jsonschema
import json
import os
from unittest import TestCase
from senpy.models import Response, Entry, Results, Sentiment, EmotionSet, Emotion, Error
from senpy.models import Entry, Results, Sentiment, EmotionSet, Error
from senpy.plugins import SenpyPlugin
from pprint import pprint
class ModelsTest(TestCase):
def test_jsonld(self):
ctx = os.path.normpath(
os.path.join(__file__, "..", "..", "..", "senpy", "schemas",
"context.jsonld"))
prueba = {"id": "test", "analysis": [], "entries": []}
r = Results(**prueba)
print("Response's context: ")

View File

@ -1,15 +1,11 @@
#!/bin/env python
import os
import logging
import pickle
import shutil
import tempfile
import json
import os
from unittest import TestCase
from flask import Flask
from senpy.models import Results, Entry
from senpy.plugins import SentimentPlugin, ShelfMixin

View File

@ -6,8 +6,7 @@ import os
from os import path
from fnmatch import fnmatch
import pyld
from jsonschema import validate, RefResolver, Draft4Validator, ValidationError
from jsonschema import RefResolver, Draft4Validator, ValidationError
root_path = path.join(path.dirname(path.realpath(__file__)), '..')
schema_folder = path.join(root_path, 'senpy', 'schemas')