zulip_bots: Python3 style for types, obey pep8.

This commit is contained in:
dkvasov 2018-05-16 19:15:06 +03:00 committed by Tim Abbott
parent ce95f9f4d5
commit f6b981b8d5
2 changed files with 19 additions and 25 deletions

View file

@ -6,8 +6,7 @@ import sys
import os import os
from os.path import basename, splitext from os.path import basename, splitext
if False: from typing import Any, Optional, Text
from typing import Any, Optional, Text
from zulip_bots.lib import ( from zulip_bots.lib import (
run_message_handler_for_bot, run_message_handler_for_bot,
@ -18,8 +17,8 @@ from zulip_bots.provision import provision_bot
current_dir = os.path.dirname(os.path.abspath(__file__)) current_dir = os.path.dirname(os.path.abspath(__file__))
def import_module_from_source(path, name=None):
# type: (Text, Optional[Text]) -> Any def import_module_from_source(path: Text, name: Optional[Text]=None) -> Any:
if not name: if not name:
name = splitext(basename(path))[0] name = splitext(basename(path))[0]
@ -39,8 +38,8 @@ def import_module_from_source(path, name=None):
return module return module
def parse_args():
# type: () -> argparse.Namespace def parse_args() -> argparse.Namespace:
usage = ''' usage = '''
zulip-run-bot <bot_name> --config-file ~/zuliprc zulip-run-bot <bot_name> --config-file ~/zuliprc
zulip-run-bot --help zulip-run-bot --help
@ -76,8 +75,7 @@ def parse_args():
return args return args
def exit_gracefully_if_zulip_config_file_does_not_exist(config_file): def exit_gracefully_if_zulip_config_file_does_not_exist(config_file: str) -> None:
# type: (str) -> None
if not os.path.exists(config_file): if not os.path.exists(config_file):
print(''' print('''
ERROR: %s does not exist. ERROR: %s does not exist.
@ -88,8 +86,8 @@ def exit_gracefully_if_zulip_config_file_does_not_exist(config_file):
''' % (config_file,)) ''' % (config_file,))
sys.exit(1) sys.exit(1)
def exit_gracefully_if_bot_config_file_does_not_exist(bot_config_file):
# type: (str) -> None def exit_gracefully_if_bot_config_file_does_not_exist(bot_config_file: str) -> None:
if bot_config_file is None: if bot_config_file is None:
# This is a common case, just so succeed quietly. (Some # This is a common case, just so succeed quietly. (Some
# bots don't have third party configuration.) # bots don't have third party configuration.)
@ -103,8 +101,8 @@ def exit_gracefully_if_bot_config_file_does_not_exist(bot_config_file):
''' % (bot_config_file,)) ''' % (bot_config_file,))
sys.exit(1) sys.exit(1)
def main():
# type: () -> None def main() -> None:
args = parse_args() args = parse_args()
if os.path.isfile(args.bot): if os.path.isfile(args.bot):
bot_path = os.path.abspath(args.bot) bot_path = os.path.abspath(args.bot)

View file

@ -10,6 +10,7 @@ from unittest import TestCase
from unittest import mock from unittest import mock
from unittest.mock import patch from unittest.mock import patch
class TestDefaultArguments(TestCase): class TestDefaultArguments(TestCase):
our_dir = os.path.dirname(__file__) our_dir = os.path.dirname(__file__)
@ -17,8 +18,7 @@ class TestDefaultArguments(TestCase):
@patch('sys.argv', ['zulip-run-bot', 'giphy', '--config-file', '/foo/bar/baz.conf']) @patch('sys.argv', ['zulip-run-bot', 'giphy', '--config-file', '/foo/bar/baz.conf'])
@patch('zulip_bots.run.run_message_handler_for_bot') @patch('zulip_bots.run.run_message_handler_for_bot')
def test_argument_parsing_with_bot_name(self, mock_run_message_handler_for_bot): def test_argument_parsing_with_bot_name(self, mock_run_message_handler_for_bot: mock.Mock) -> None:
# type: (mock.Mock) -> None
with patch('zulip_bots.run.exit_gracefully_if_zulip_config_file_does_not_exist'): with patch('zulip_bots.run.exit_gracefully_if_zulip_config_file_does_not_exist'):
zulip_bots.run.main() zulip_bots.run.main()
@ -30,8 +30,7 @@ class TestDefaultArguments(TestCase):
@patch('sys.argv', ['zulip-run-bot', path_to_bot, '--config-file', '/foo/bar/baz.conf']) @patch('sys.argv', ['zulip-run-bot', path_to_bot, '--config-file', '/foo/bar/baz.conf'])
@patch('zulip_bots.run.run_message_handler_for_bot') @patch('zulip_bots.run.run_message_handler_for_bot')
def test_argument_parsing_with_bot_path(self, mock_run_message_handler_for_bot): def test_argument_parsing_with_bot_path(self, mock_run_message_handler_for_bot: mock.Mock) -> None:
# type: (mock.Mock) -> None
with patch('zulip_bots.run.exit_gracefully_if_zulip_config_file_does_not_exist'): with patch('zulip_bots.run.exit_gracefully_if_zulip_config_file_does_not_exist'):
zulip_bots.run.main() zulip_bots.run.main()
@ -42,8 +41,7 @@ class TestDefaultArguments(TestCase):
lib_module=mock.ANY, lib_module=mock.ANY,
quiet=False) quiet=False)
def test_adding_bot_parent_dir_to_sys_path_when_bot_name_specified(self): def test_adding_bot_parent_dir_to_sys_path_when_bot_name_specified(self) -> None:
# type: () -> None
bot_name = 'any_bot_name' bot_name = 'any_bot_name'
expected_bot_dir_path = os.path.join( expected_bot_dir_path = os.path.join(
os.path.dirname(zulip_bots.run.__file__), os.path.dirname(zulip_bots.run.__file__),
@ -53,8 +51,7 @@ class TestDefaultArguments(TestCase):
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_name, bot_dir_path=expected_bot_dir_path) self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_name, bot_dir_path=expected_bot_dir_path)
@patch('os.path.isfile', return_value=True) @patch('os.path.isfile', return_value=True)
def test_adding_bot_parent_dir_to_sys_path_when_bot_path_specified(self, mock_os_path_isfile): def test_adding_bot_parent_dir_to_sys_path_when_bot_path_specified(self, mock_os_path_isfile: mock.Mock) -> None:
# type: (mock.Mock) -> None
bot_path = '/path/to/bot' bot_path = '/path/to/bot'
expected_bot_dir_path = '/path/to' expected_bot_dir_path = '/path/to'
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_path, bot_dir_path=expected_bot_dir_path) self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_path, bot_dir_path=expected_bot_dir_path)
@ -69,12 +66,11 @@ class TestDefaultArguments(TestCase):
self.assertIn(bot_dir_path, sys.path) self.assertIn(bot_dir_path, sys.path)
class TestBotLib(TestCase):
def test_extract_query_without_mention(self):
# type: () -> None
def test_message(name, message, expected_return): class TestBotLib(TestCase):
# type: (str, str, Optional[str]) -> None def test_extract_query_without_mention(self) -> None:
def test_message(name: str, message: str, expected_return: Optional[str]) -> None:
mock_client = mock.MagicMock() mock_client = mock.MagicMock()
mock_client.full_name = name mock_client.full_name = name
mock_message = {'content': message} mock_message = {'content': message}