flask_server: Move the server to its own package.

This commit is contained in:
Eeshan Garg 2017-07-18 01:31:54 -02:30
parent 9d1253ff0d
commit 928d5ca16d
7 changed files with 96 additions and 10 deletions

16
zulip_botserver/README.md Normal file
View file

@ -0,0 +1,16 @@
```
zulip-bot-server --config-file <path to flaskbotrc> --hostname <address> --port <port>
```
Example: `zulip-bot-server --config-file ~/flaskbotrc`
This program loads the bot configurations from the
config file (flaskbotrc here) and loads the bot modules.
It then starts the server and fetches the requests to the
above loaded modules and returns the success/failure result.
Please make sure you have a current flaskbotrc file with the
configurations of the required bots.
Hostname and Port are optional arguments. Default hostname is
127.0.0.1 and default port is 5002.

70
zulip_botserver/setup.py Executable file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
# We should be installable with either setuptools or distutils.
package_info = dict(
name='zulip_botserver',
version='0.3.1',
description='Zulip\'s Flask server for running bots',
author='Zulip Open Source Project',
author_email='zulip-devel@googlegroups.com',
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Topic :: Communications :: Chat',
],
url='https://www.zulip.org/',
entry_points={
'console_scripts': [
'zulip-bot-server=zulip_botserver.server:main',
],
},
test_suite='tests',
) # type: Dict[str, Any]
setuptools_info = dict(
install_requires=[
'zulip>=0.3.1',
'zulip_bots>=0.3.1',
'flask>=0.12.2',
],
)
try:
from setuptools import setup, find_packages
package_info.update(setuptools_info)
package_info['packages'] = find_packages(exclude=['tests'])
except ImportError:
from distutils.core import setup
from distutils.version import LooseVersion
from importlib import import_module
# Manual dependency check
def check_dependency_manually(module_name, version=None):
try:
module = import_module(module_name)
if version is not None:
assert(LooseVersion(module.__version__) >= LooseVersion(version))
except (ImportError, AssertionError):
if version is not None:
print("{name}>={version} is not installed.".format(
req=req, version=version), file=sys.stderr)
else:
print("{name} is not installed.".format(name=module_name), file=sys.stderr)
sys.exit(1)
check_dependency_manually('zulip', '0.3.1')
check_dependency_manually('zulip_bots', '0.3.1')
check_dependency_manually('flask', '0.12.2')
package_info['packages'] = ['zulip_botserver']
setup(**package_info)

View file

@ -1,5 +1,5 @@
from unittest import TestCase from unittest import TestCase
import zulip.bot_server import zulip_botserver.server
import json import json
from typing import Any, List, Dict, Mapping from typing import Any, List, Dict, Mapping
@ -7,8 +7,8 @@ class BotServerTestCase(TestCase):
def setUp(self): def setUp(self):
# type: () -> None # type: () -> None
zulip.bot_server.app.testing = True zulip_botserver.server.app.testing = True
self.app = zulip.bot_server.app.test_client() self.app = zulip_botserver.server.app.test_client()
def assert_bot_server_response(self, def assert_bot_server_response(self,
available_bots=None, available_bots=None,
@ -21,13 +21,13 @@ class BotServerTestCase(TestCase):
# type: (List[str], Dict[str, Any], Dict[str, Any], str, Dict[str, Dict[str, Any]], bool) -> None # type: (List[str], Dict[str, Any], Dict[str, Any], str, Dict[str, Dict[str, Any]], bool) -> None
if available_bots is not None: if available_bots is not None:
zulip.bot_server.available_bots = available_bots zulip_botserver.server.available_bots = available_bots
if bots_config is not None: if bots_config is not None:
zulip.bot_server.bots_config = bots_config zulip_botserver.server.bots_config = bots_config
if bots_lib_module is not None: if bots_lib_module is not None:
zulip.bot_server.bots_lib_module = bots_lib_module zulip_botserver.server.bots_lib_module = bots_lib_module
response = self.app.post(payload_url, data=json.dumps(message)) response = self.app.post(payload_url, data=json.dumps(message))

View file

@ -2,7 +2,7 @@ from __future__ import absolute_import
import mock import mock
import unittest import unittest
from typing import Any from typing import Any
from .bot_server_test_lib import BotServerTestCase from .server_test_lib import BotServerTestCase
class BotServerTests(BotServerTestCase): class BotServerTests(BotServerTestCase):
class MockMessageHandler(object): class MockMessageHandler(object):
@ -15,7 +15,7 @@ class BotServerTests(BotServerTestCase):
# type: () -> Any # type: () -> Any
return BotServerTests.MockMessageHandler() return BotServerTests.MockMessageHandler()
@mock.patch('zulip.bot_server.ExternalBotHandler') @mock.patch('zulip_botserver.server.ExternalBotHandler')
def test_successful_request(self, mock_ExternalBotHandler): def test_successful_request(self, mock_ExternalBotHandler):
# type: (mock.Mock) -> None # type: (mock.Mock) -> None
available_bots = ['testbot'] available_bots = ['testbot']

View file

@ -11,7 +11,7 @@ from werkzeug.exceptions import BadRequest
from six.moves.configparser import SafeConfigParser from six.moves.configparser import SafeConfigParser
from zulip import Client from zulip import Client
from bots_api.bot_lib import ExternalBotHandler, StateHandler from zulip_bots.lib import ExternalBotHandler, StateHandler
bots_config = {} # type: Dict[str, Mapping[str, str]] bots_config = {} # type: Dict[str, Mapping[str, str]]
available_bots = [] # type: List[str] available_bots = [] # type: List[str]
@ -33,7 +33,7 @@ def load_lib_modules():
# type: () -> None # type: () -> None
for bot in available_bots: for bot in available_bots:
try: try:
module_name = 'bots.{bot}.{bot}'.format(bot=bot) module_name = 'zulip_bots.{bot}.{bot}'.format(bot=bot)
bots_lib_module[bot] = import_module(module_name) bots_lib_module[bot] = import_module(module_name)
except ImportError: except ImportError:
print("\n Import Error: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc " print("\n Import Error: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc "