Exemplo n.º 1
0
 def test_adding_folder(self):
     """ It should be possible for senpy to look for plugins in more folders. """
     senpy = Senpy(plugin_folder=None, app=self.app, default_plugins=False)
     assert not senpy.analysis_plugins()
     senpy.add_folder(self.examples_dir)
     assert senpy.plugins(plugin_type=plugins.Analyser, is_activated=False)
     self.assertRaises(AttributeError, senpy.add_folder, 'DOES NOT EXIST')
Exemplo n.º 2
0
 def test_adding_folder(self):
     """ It should be possible for senpy to look for plugins in more folders. """
     senpy = Senpy(plugin_folder=None,
                   app=self.app,
                   default_plugins=False)
     assert not senpy.analysis_plugins()
     senpy.add_folder(self.examples_dir)
     assert senpy.plugins(plugin_type=plugins.Analyser, is_activated=False)
     self.assertRaises(AttributeError, senpy.add_folder, 'DOES NOT EXIST')
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description='Run a Senpy server')
    parser.add_argument(
        '--level',
        '-l',
        metavar='logging_level',
        type=str,
        default="INFO",
        help='Logging level')
    parser.add_argument(
        '--log-format',
        metavar='log_format',
        type=str,
        default='%(asctime)s %(levelname)-10s %(name)-30s \t %(message)s',
        help='Logging format')
    parser.add_argument(
        '--debug',
        '-d',
        action='store_true',
        default=False,
        help='Run the application in debug mode')
    parser.add_argument(
        '--no-default-plugins',
        action='store_true',
        default=False,
        help='Do not load the default plugins')
    parser.add_argument(
        '--host',
        type=str,
        default="0.0.0.0",
        help='Use 0.0.0.0 to accept requests from any host.')
    parser.add_argument(
        '--port',
        '-p',
        type=int,
        default=SERVER_PORT,
        help='Port to listen on.')
    parser.add_argument(
        '--plugins-folder',
        '-f',
        type=str,
        action='append',
        help='Where to look for plugins.')
    parser.add_argument(
        '--only-install',
        '-i',
        action='store_true',
        default=False,
        help='Do not run a server, only install plugin dependencies')
    parser.add_argument(
        '--only-test',
        action='store_true',
        default=False,
        help='Do not run a server, just test all plugins')
    parser.add_argument(
        '--test',
        '-t',
        action='store_true',
        default=False,
        help='Test all plugins before launching the server')
    parser.add_argument(
        '--only-list',
        '--list',
        action='store_true',
        default=False,
        help='Do not run a server, only list plugins found')
    parser.add_argument(
        '--data-folder',
        '--data',
        type=str,
        default=None,
        help='Where to look for data. It be set with the SENPY_DATA environment variable as well.')
    parser.add_argument(
        '--no-threaded',
        action='store_true',
        default=False,
        help='Run a single-threaded server')
    parser.add_argument(
        '--no-deps',
        '-n',
        action='store_true',
        default=False,
        help='Skip installing dependencies')
    parser.add_argument(
        '--version',
        '-v',
        action='store_true',
        default=False,
        help='Output the senpy version and exit')
    parser.add_argument(
        '--allow-fail',
        '--fail',
        action='store_true',
        default=False,
        help='Do not exit if some plugins fail to activate')
    args = parser.parse_args()
    print('Senpy version {}'.format(senpy.__version__))
    print(sys.version)
    if args.version:
        exit(1)
    rl = logging.getLogger()
    rl.setLevel(getattr(logging, args.level))
    logger_handler = rl.handlers[0]

    # First, generic formatter:
    logger_handler.setFormatter(logging.Formatter(args.log_format))

    app = Flask(__name__)
    app.debug = args.debug
    sp = Senpy(app,
               plugin_folder=None,
               default_plugins=not args.no_default_plugins,
               data_folder=args.data_folder)
    folders = list(args.plugins_folder) if args.plugins_folder else []
    if not folders:
        folders.append(".")
    for p in folders:
        sp.add_folder(p)

    plugins = sp.plugins(plugin_type=None, is_activated=False)
    maxname = max(len(x.name) for x in plugins)
    maxversion = max(len(str(x.version)) for x in plugins)
    print('Found {} plugins:'.format(len(plugins)))
    for plugin in plugins:
        import inspect
        fpath = inspect.getfile(plugin.__class__)
        print('\t{: <{maxname}} @ {: <{maxversion}} -> {}'.format(plugin.name,
                                                                  plugin.version,
                                                                  fpath,
                                                                  maxname=maxname,
                                                                  maxversion=maxversion))
    if args.only_list:
        return
    if not args.no_deps:
        sp.install_deps()
    if args.only_install:
        return
    sp.activate_all(allow_fail=args.allow_fail)
    if args.test or args.only_test:
        easy_test(sp.plugins(), debug=args.debug)
        if args.only_test:
            return
    print('Senpy version {}'.format(senpy.__version__))
    print('Server running on port %s:%d. Ctrl+C to quit' % (args.host,
                                                            args.port))
    try:
        app.run(args.host,
                args.port,
                threaded=not args.no_threaded,
                debug=app.debug)
    except KeyboardInterrupt:
        print('Bye!')
    sp.deactivate_all()
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(description='Run a Senpy server')
    parser.add_argument('--level',
                        '-l',
                        metavar='logging_level',
                        type=str,
                        default="INFO",
                        help='Logging level')
    parser.add_argument(
        '--log-format',
        metavar='log_format',
        type=str,
        default='%(asctime)s %(levelname)-10s %(name)-30s \t %(message)s',
        help='Logging format')
    parser.add_argument('--debug',
                        '-d',
                        action='store_true',
                        default=False,
                        help='Run the application in debug mode')
    parser.add_argument('--no-default-plugins',
                        action='store_true',
                        default=False,
                        help='Do not load the default plugins')
    parser.add_argument('--host',
                        type=str,
                        default="0.0.0.0",
                        help='Use 0.0.0.0 to accept requests from any host.')
    parser.add_argument('--port',
                        '-p',
                        type=int,
                        default=SERVER_PORT,
                        help='Port to listen on.')
    parser.add_argument('--plugins-folder',
                        '-f',
                        type=str,
                        action='append',
                        help='Where to look for plugins.')
    parser.add_argument(
        '--only-install',
        '-i',
        action='store_true',
        default=False,
        help='Do not run a server, only install plugin dependencies')
    parser.add_argument('--only-test',
                        action='store_true',
                        default=False,
                        help='Do not run a server, just test all plugins')
    parser.add_argument('--test',
                        '-t',
                        action='store_true',
                        default=False,
                        help='Test all plugins before launching the server')
    parser.add_argument('--only-list',
                        '--list',
                        action='store_true',
                        default=False,
                        help='Do not run a server, only list plugins found')
    parser.add_argument(
        '--data-folder',
        '--data',
        type=str,
        default=None,
        help=
        'Where to look for data. It be set with the SENPY_DATA environment variable as well.'
    )
    parser.add_argument('--no-threaded',
                        action='store_true',
                        default=False,
                        help='Run a single-threaded server')
    parser.add_argument('--no-deps',
                        '-n',
                        action='store_true',
                        default=False,
                        help='Skip installing dependencies')
    parser.add_argument('--version',
                        '-v',
                        action='store_true',
                        default=False,
                        help='Output the senpy version and exit')
    parser.add_argument('--allow-fail',
                        '--fail',
                        action='store_true',
                        default=False,
                        help='Do not exit if some plugins fail to activate')
    args = parser.parse_args()
    print('Senpy version {}'.format(senpy.__version__))
    print(sys.version)
    if args.version:
        exit(1)
    rl = logging.getLogger()
    rl.setLevel(getattr(logging, args.level))
    logger_handler = rl.handlers[0]

    # First, generic formatter:
    logger_handler.setFormatter(logging.Formatter(args.log_format))

    app = Flask(__name__)
    app.debug = args.debug
    sp = Senpy(app,
               plugin_folder=None,
               default_plugins=not args.no_default_plugins,
               data_folder=args.data_folder)
    folders = list(args.plugins_folder) if args.plugins_folder else []
    if not folders:
        folders.append(".")
    for p in folders:
        sp.add_folder(p)

    plugins = sp.plugins(plugin_type=None, is_activated=False)
    maxname = max(len(x.name) for x in plugins)
    maxversion = max(len(str(x.version)) for x in plugins)
    print('Found {} plugins:'.format(len(plugins)))
    for plugin in plugins:
        import inspect
        fpath = inspect.getfile(plugin.__class__)
        print('\t{: <{maxname}} @ {: <{maxversion}} -> {}'.format(
            plugin.name,
            plugin.version,
            fpath,
            maxname=maxname,
            maxversion=maxversion))
    if args.only_list:
        return
    if not args.no_deps:
        sp.install_deps()
    if args.only_install:
        return
    sp.activate_all(allow_fail=args.allow_fail)
    if args.test or args.only_test:
        easy_test(sp.plugins(), debug=args.debug)
        if args.only_test:
            return
    print('Senpy version {}'.format(senpy.__version__))
    print('Server running on port %s:%d. Ctrl+C to quit' %
          (args.host, args.port))
    try:
        app.run(args.host,
                args.port,
                threaded=not args.no_threaded,
                debug=app.debug)
    except KeyboardInterrupt:
        print('Bye!')
    sp.deactivate_all()
Exemplo n.º 5
0
class BlueprintsTest(TestCase):
    def setUp(self):
        self.app = Flask("test_extensions")
        self.client = self.app.test_client()
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
        self.senpy.activate_plugin("Dummy", sync=True)
        self.senpy.activate_plugin("DummyRequired", sync=True)
        self.senpy.default_plugin = 'Dummy'

    def assertCode(self, resp, code):
        self.assertEqual(resp.status_code, code)

    def test_home(self):
        """
        Calling with no arguments should ask the user for more arguments
        """
        resp = self.client.get("/api/")
        self.assertCode(resp, 400)
        js = parse_resp(resp)
        logging.debug(js)
        assert js["status"] == 400
        atleast = {
            "status": 400,
            "message": "Missing or invalid parameters",
        }
        assert check_dict(js, atleast)

    def test_analysis(self):
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
        resp = self.client.get("/api/?i=My aloha mohame")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert "@context" in js
        assert "entries" in js

    def test_analysis_extra(self):
        """
        Extra params that have a default should
        """
        resp = self.client.get("/api/?i=My aloha mohame&algo=Dummy")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert "@context" in js
        assert "entries" in js

    def test_analysis_extra_required(self):
        """
        Extra params that have a required argument that does not
        have a default should raise an error.
        """
        resp = self.client.get("/api/?i=My aloha mohame&algo=DummyRequired")
        self.assertCode(resp, 400)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert isinstance(js, models.Error)

    def test_error(self):
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
        resp = self.client.get("/api/?i=My aloha mohame&algo=DOESNOTEXIST")
        self.assertCode(resp, 404)
        js = parse_resp(resp)
        logging.debug("Got response: %s", js)
        assert isinstance(js, models.Error)

    def test_list(self):
        """ List the plugins """
        resp = self.client.get("/api/plugins/")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert 'plugins' in js
        plugins = js['plugins']
        assert len(plugins) > 1
        assert list(p for p in plugins if p['name'] == "Dummy")
        assert "@context" in js

    def test_headers(self):
        for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
                            ["inHeaders"]):
            resp = self.client.get("%s" % (i))
            js = parse_resp(resp)
            assert "@context" in js
            resp = self.client.get("%s&%s=0" % (i, j))
            js = parse_resp(resp)
            assert "@context" in js
            resp = self.client.get("%s&%s=1" % (i, j))
            js = parse_resp(resp)
            assert "@context" not in js
            resp = self.client.get("%s&%s=true" % (i, j))
            js = parse_resp(resp)
            assert "@context" not in js

    def test_detail(self):
        """ Show only one plugin"""
        resp = self.client.get("/api/plugins/Dummy/")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert "@id" in js
        assert js["@id"] == "plugins/Dummy_0.1"

    def test_default(self):
        """ Show only one plugin"""
        resp = self.client.get("/api/plugins/default/")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        logging.debug(js)
        assert "@id" in js
        assert js["@id"] == "plugins/Dummy_0.1"

    def test_context(self):
        resp = self.client.get("/api/contexts/context.jsonld")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        assert "@context" in js
        assert check_dict(
            js["@context"],
            {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})

    def test_schema(self):
        resp = self.client.get("/api/schemas/definitions.json")
        self.assertCode(resp, 200)
        js = parse_resp(resp)
        assert "$schema" in js
Exemplo n.º 6
0
class BlueprintsTest(TestCase):
    def create_app(self):
        self.app = Flask("test_extensions")
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
        self.senpy.activate_plugin("Dummy", sync=True)
        return self.app

    def test_home(self):
        """
        Calling with no arguments should ask the user for more arguments
        """
        resp = self.client.get("/")
        self.assert404(resp)
        logging.debug(resp.json)
        assert resp.json["status"] == 404
        atleast = {
            "status": 404,
            "message": "Missing or invalid parameters",
        }
        assert check_dict(resp.json, atleast)

    def test_analysis(self):
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
        resp = self.client.get("/?i=My aloha mohame")
        self.assert200(resp)
        logging.debug("Got response: %s", resp.json)
        assert "@context" in resp.json
        assert check_dict(
            resp.json["@context"],
            {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
        assert "entries" in resp.json

    def test_list(self):
        """ List the plugins """
        resp = self.client.get("/plugins/")
        self.assert200(resp)
        logging.debug(resp.json)
        assert "Dummy" in resp.json
        assert "@context" in resp.json

    def test_headers(self):
        for i, j in product(["/plugins/?nothing=", "/?i=test&"],
                            ["headers", "inHeaders"]):
            resp = self.client.get("%s" % (i))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=0" % (i, j))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=1" % (i, j))
            assert "@context" not in resp.json
            resp = self.client.get("%s&%s=true" % (i, j))
            assert "@context" not in resp.json

    def test_detail(self):
        """ Show only one plugin"""
        resp = self.client.get("/plugins/Dummy")
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"

    def test_activate(self):
        """ Activate and deactivate one plugin """
        resp = self.client.get("/plugins/Dummy/deactivate")
        self.assert200(resp)
        sleep(0.5)
        resp = self.client.get("/plugins/Dummy")
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == False
        resp = self.client.get("/plugins/Dummy/activate")
        self.assert200(resp)
        sleep(0.5)
        resp = self.client.get("/plugins/Dummy")
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == True

    def test_default(self):
        """ Show only one plugin"""
        resp = self.client.get("/default")
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"
        resp = self.client.get("/plugins/Dummy/deactivate")
        self.assert200(resp)
        sleep(0.5)
        resp = self.client.get("/default")
        self.assert404(resp)
Exemplo n.º 7
0
class BlueprintsTest(TestCase):

    def create_app(self):
        self.app = Flask("test_extensions")
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
        self.senpy.activate_plugin("Dummy", sync=True)
        return self.app

    def test_home(self):
        """
        Calling with no arguments should ask the user for more arguments
        """
        resp = self.client.get("/api/")
        self.assert404(resp)
        logging.debug(resp.json)
        assert resp.json["status"] == 404
        atleast = {
            "status": 404,
            "message": "Missing or invalid parameters",
        }
        assert check_dict(resp.json, atleast)

    def test_analysis(self):
        """
        The dummy plugin returns an empty response,\
        it should contain the context
        """
        resp = self.client.get("/api/?i=My aloha mohame")
        self.assert200(resp)
        logging.debug("Got response: %s", resp.json)
        assert "@context" in resp.json
        assert "entries" in resp.json

    def test_list(self):
        """ List the plugins """
        resp = self.client.get("/api/plugins/")
        self.assert200(resp)
        logging.debug(resp.json)
        assert 'plugins' in resp.json
        plugins = resp.json['plugins']
        assert len(plugins) > 1
        assert list(p for p in plugins if p['name'] == "Dummy")
        assert "@context" in resp.json

    def test_headers(self):
        for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
                            ["inHeaders"]):
            resp = self.client.get("%s" % (i))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=0" % (i, j))
            assert "@context" in resp.json
            resp = self.client.get("%s&%s=1" % (i, j))
            assert "@context" not in resp.json
            resp = self.client.get("%s&%s=true" % (i, j))
            assert "@context" not in resp.json

    def test_detail(self):
        """ Show only one plugin"""
        resp = self.client.get("/api/plugins/Dummy/")
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"

    def test_activate(self):
        """ Activate and deactivate one plugin """
        resp = self.client.get("/api/plugins/Dummy/deactivate")
        self.assert200(resp)
        sleep(0.5)
        resp = self.client.get("/api/plugins/Dummy/")
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == False
        resp = self.client.get("/api/plugins/Dummy/activate")
        self.assert200(resp)
        sleep(0.5)
        resp = self.client.get("/api/plugins/Dummy/")
        self.assert200(resp)
        assert "is_activated" in resp.json
        assert resp.json["is_activated"] == True

    def test_default(self):
        """ Show only one plugin"""
        resp = self.client.get("/api/plugins/default/")
        self.assert200(resp)
        logging.debug(resp.json)
        assert "@id" in resp.json
        assert resp.json["@id"] == "Dummy_0.1"
        resp = self.client.get("/api/plugins/Dummy/deactivate")
        self.assert200(resp)
        sleep(0.5)
        resp = self.client.get("/api/plugins/default/")
        self.assert404(resp)

    def test_context(self):
        resp = self.client.get("/api/contexts/context.jsonld")
        self.assert200(resp)
        assert "@context" in resp.json
        assert check_dict(
            resp.json["@context"],
            {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})

    def test_schema(self):
        resp = self.client.get("/api/schemas/definitions.json")
        self.assert200(resp)
        assert "$schema" in resp.json