Skip to content

Commit

Permalink
Merge pull request #53 from RasaHQ/cli
Browse files Browse the repository at this point in the history
prepare for rasa cli
  • Loading branch information
wochinge authored Feb 25, 2019
2 parents c40b2d1 + 7057920 commit fc12d54
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Changed
-------
- ``self._deactivate()`` method from the ``FormAction`` class has been
renamed to ``self.deactivate()``
- changed endpoint function so that it is now accessible with Python as well

[0.12.1] - 2018-11-11
^^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions rasa_core_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Dict, Text, Any, Optional, Iterator, List

import rasa_core_sdk.version
import rasa_core_sdk.cli

logger = logging.getLogger(__name__)

Expand Down
Empty file added rasa_core_sdk/cli/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions rasa_core_sdk/cli/arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import argparse

from rasa_core_sdk.constants import DEFAULT_SERVER_PORT


def action_arg(action):
if "/" in action:
raise argparse.ArgumentTypeError(
'Invalid actions format. Actions file should be a python module '
'and passed with module notation (e.g. directory.actions).')
else:
return action


def add_endpoint_arguments(parser):
parser.add_argument(
'-p', '--port',
default=DEFAULT_SERVER_PORT,
type=int,
help="port to run the server at")
parser.add_argument(
'--cors',
nargs='*',
type=str,
help="enable CORS for the passed origin. "
"Use * to whitelist all origins")
parser.add_argument(
'--actions',
type=action_arg,
default=None,
help="name of action package to be loaded"
)
1 change: 1 addition & 0 deletions rasa_core_sdk/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEFAULT_SERVER_PORT = 5055
66 changes: 25 additions & 41 deletions rasa_core_sdk/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,24 @@
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin
from gevent.pywsgi import WSGIServer

from rasa_core_sdk.cli.arguments import add_endpoint_arguments
from rasa_core_sdk.constants import DEFAULT_SERVER_PORT
from rasa_core_sdk.executor import ActionExecutor
from rasa_core_sdk import ActionExecutionRejection
import rasa_core_sdk

from rasa_core_sdk import utils

DEFAULT_SERVER_PORT = 5055

logger = logging.getLogger(__name__)


def action_arg(action):
if "/" in action:
raise argparse.ArgumentTypeError(
'Invalid actions format. Actions file should be a python module '
'and passed with module notation (e.g. directory.actions).')
else:
return action


def create_argument_parser():
"""Parse all the command line arguments for the run script."""

parser = argparse.ArgumentParser(
description='starts the action endpoint')
parser.add_argument(
'-p', '--port',
default=DEFAULT_SERVER_PORT,
type=int,
help="port to run the server at")
parser.add_argument(
'--cors',
nargs='*',
type=str,
help="enable CORS for the passed origin. "
"Use * to whitelist all origins")
parser.add_argument(
'--actions',
type=action_arg,
default=None,
help="name of action package to be loaded"
)
add_endpoint_arguments(parser)
utils.add_logging_option_arguments(parser)
return parser

Expand Down Expand Up @@ -135,24 +111,32 @@ def check_version_compatibility(core_version):
"".format(core_version, rasa_core_sdk.__version__))


if __name__ == '__main__':
# Running as standalone python application
arg_parser = create_argument_parser()
cmdline_args = arg_parser.parse_args()

logging.basicConfig(level=logging.DEBUG)
logging.getLogger('matplotlib').setLevel(logging.WARN)

utils.configure_colored_logging(cmdline_args.loglevel)

def run(actions, port=DEFAULT_SERVER_PORT, cors='*'):
logger.info("Starting action endpoint server...")
edp_app = endpoint_app(cors_origins=cmdline_args.cors,
action_package_name=cmdline_args.actions)
edp_app = endpoint_app(cors_origins=cors,
action_package_name=actions)

http_server = WSGIServer(('0.0.0.0', cmdline_args.port), edp_app)
http_server = WSGIServer(('0.0.0.0', port), edp_app)

http_server.start()
logger.info("Action endpoint is up and running. on {}"
"".format(http_server.address))

http_server.serve_forever()


def main(args):
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('matplotlib').setLevel(logging.WARN)

utils.configure_colored_logging(args.loglevel)

run(args.actions, args.port, args.cors)


if __name__ == '__main__':
# Running as standalone python application
arg_parser = create_argument_parser()
cmdline_args = arg_parser.parse_args()

main(cmdline_args)
6 changes: 1 addition & 5 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
import rasa_core_sdk.endpoint as ep


def test_endpoint():
pass


def test_arg_parser_actions_params_folder_style():
parser = ep.create_argument_parser()
args = ['--actions', 'actions/act']

with pytest.raises(BaseException) as e:
cmdline_args = parser.parse_args(args)
parser.parse_args(args)
if e is not None:
assert True
else:
Expand Down

0 comments on commit fc12d54

Please sign in to comment.