X Tutup
Skip to content

Commit 3ecccc9

Browse files
authored
Add legacy classes and slack package migration (slackapi#28)
1 parent 3cf2e86 commit 3ecccc9

File tree

254 files changed

+22532
-1182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

254 files changed

+22532
-1182
lines changed

.travis.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ python:
88
- "nightly" # nightly build
99
install:
1010
- python setup.py install
11-
script:
1211
- pip install "pytest>=5,<6"
13-
- pytest tests/scenario_tests/
14-
- pip install "pytest-asyncio<1"
15-
- pip install "aiohttp>=3,<4"
16-
- pytest tests/scenario_tests_async/
17-
- python setup.py test
12+
script:
13+
# codegen for slack_sdk
14+
- python setup.py codegen
15+
# FIXME: the result can be different depending on OS platforms (e.g., macOS vs Linux)?
16+
# - if git status --porcelain | grep .; then git --no-pager diff; exit 1; fi
17+
# testing without aiohttp
18+
- travis_retry pytest tests/scenario_tests/
19+
- travis_retry pytest tests/slack_sdk_tests/
20+
# testing with aiohttp
21+
- pip install "pytest-asyncio<1" "aiohttp>=3,<4"
22+
- travis_retry pytest tests/scenario_tests_async/
23+
- travis_retry pytest tests/slack_sdk_tests_async/
24+
- export SLACKCLIENT_SKIP_DEPRECATION=1
25+
- travis_retry pytest tests/slack_sdk_tests_legacy/
26+
# run all tests just in case
27+
- travis_retry python setup.py test

setup.py

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/env python
2+
import os
3+
import subprocess
4+
import sys
25
from glob import glob
36
from os.path import splitext, basename
47

@@ -10,12 +13,98 @@
1013
with open("README.md", "r") as fh:
1114
long_description = fh.read()
1215

16+
here = os.path.abspath(os.path.dirname(__file__))
17+
18+
codegen_dependencies = [
19+
"black==19.10b0",
20+
]
21+
1322
test_dependencies = [
1423
"pytest>=5,<6",
15-
"pytest-asyncio<1",
24+
"pytest-asyncio<1", # for async
1625
"aiohttp>=3,<4", # for async
1726
]
1827

28+
29+
class CodegenCommand(setuptools.Command):
30+
user_options = []
31+
32+
@staticmethod
33+
def status(s):
34+
"""Prints things in bold."""
35+
print("\033[1m{0}\033[0m".format(s))
36+
37+
def initialize_options(self):
38+
pass
39+
40+
def finalize_options(self):
41+
pass
42+
43+
def _run(self, s, command):
44+
try:
45+
self.status(s + "\n" + " ".join(command))
46+
subprocess.check_call(command)
47+
except subprocess.CalledProcessError as error:
48+
sys.exit(error.returncode)
49+
50+
def run(self):
51+
self._run("Installing required dependencies ...",
52+
[sys.executable, "-m", "pip", "install"] + codegen_dependencies)
53+
54+
header = "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" \
55+
"#\n" \
56+
"# *** DO NOT EDIT THIS FILE ***\n" \
57+
"#\n" \
58+
"# 1) Modify slack/web/client.py\n" \
59+
"# 2) Run `python setup.py codegen`\n" \
60+
"#\n" \
61+
"# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" \
62+
"\n"
63+
with open("./src/slack_sdk/web/client.py", "r") as original:
64+
source = original.read()
65+
import re
66+
async_source = header + source
67+
async_source = re.sub(" def ", " async def ", async_source)
68+
async_source = re.sub("from asyncio import Future\n", "", async_source)
69+
async_source = re.sub("return self.api_call\(", "return await self.api_call(", async_source)
70+
async_source = re.sub("-> SlackResponse", "-> AsyncSlackResponse", async_source)
71+
async_source = re.sub(
72+
"from .base_client import BaseClient, SlackResponse",
73+
"from .async_base_client import AsyncBaseClient, AsyncSlackResponse", async_source)
74+
# from slack_sdk import WebClient
75+
async_source = re.sub(
76+
"class WebClient\(BaseClient\):",
77+
"class AsyncWebClient(AsyncBaseClient):", async_source)
78+
async_source = re.sub(
79+
"from slack_sdk import WebClient",
80+
"from slack_sdk.web.async_client import AsyncWebClient", async_source)
81+
async_source = re.sub(
82+
"= WebClient\(",
83+
"= AsyncWebClient(", async_source)
84+
with open('./src/slack_sdk/web/async_client.py', 'w') as output:
85+
output.write(async_source)
86+
87+
legacy_source = header + "from asyncio import Future\n" + source
88+
legacy_source = re.sub("-> SlackResponse", "-> Union[Future, SlackResponse]", legacy_source)
89+
legacy_source = re.sub(
90+
"from .base_client import BaseClient, SlackResponse",
91+
"from .legacy_base_client import LegacyBaseClient, SlackResponse", legacy_source)
92+
legacy_source = re.sub(
93+
"class WebClient\(BaseClient\):",
94+
"class LegacyWebClient(LegacyBaseClient):", legacy_source)
95+
legacy_source = re.sub(
96+
"from slack_sdk import WebClient",
97+
"from slack_sdk.web.legacy_client import LegacyWebClient", legacy_source)
98+
legacy_source = re.sub(
99+
"= WebClient\(",
100+
"= LegacyWebClient(", legacy_source)
101+
with open('./src/slack_sdk/web/legacy_client.py', 'w') as output:
102+
output.write(legacy_source)
103+
104+
self._run("Running black (code formatter) ... ",
105+
[sys.executable, "-m", "black", f"{here}/src"])
106+
107+
19108
setuptools.setup(
20109
name="slack_bolt",
21110
version=__version__,
@@ -64,4 +153,7 @@
64153
"Operating System :: OS Independent",
65154
],
66155
python_requires='>=3.6',
156+
cmdclass={
157+
"codegen": CodegenCommand,
158+
},
67159
)

src/slack/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
from logging import NullHandler
3+
4+
from slack_sdk.rtm import RTMClient # noqa
5+
from slack_sdk.web.async_client import AsyncWebClient # noqa
6+
from slack_sdk.web.legacy_client import LegacyWebClient as WebClient # noqa
7+
from slack_sdk.webhook.async_client import AsyncWebhookClient # noqa
8+
from slack_sdk.webhook.client import WebhookClient # noqa
9+
10+
# Set default logging handler to avoid "No handler found" warnings.
11+
logging.getLogger(__name__).addHandler(NullHandler())
12+
13+
from slack import deprecation
14+
15+
deprecation.show_message(__name__, "slack_sdk.web/webhook/rtm")

src/slack/deprecation.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import warnings
3+
4+
5+
def show_message(old: str, new: str) -> None:
6+
skip_deprecation = os.environ.get(
7+
"SLACKCLIENT_SKIP_DEPRECATION"
8+
) # for unit tests etc.
9+
if skip_deprecation:
10+
return
11+
12+
message = (
13+
f"{old} package is deprecated. Please use {new} package instead. "
14+
"For more info, go to https://slack.dev/python-slack-sdk/v3-migration/"
15+
)
16+
warnings.warn(message)

src/slack/errors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from slack_sdk.errors import BotUserAccessError # noqa
2+
from slack_sdk.errors import SlackApiError # noqa
3+
from slack_sdk.errors import SlackClientError # noqa
4+
from slack_sdk.errors import SlackClientNotConnectedError # noqa
5+
from slack_sdk.errors import SlackObjectFormationError # noqa
6+
from slack_sdk.errors import SlackRequestError # noqa
7+
8+
from slack import deprecation
9+
10+
deprecation.show_message(__name__, "slack_sdk.errors")

src/slack/py.typed

Whitespace-only changes.

src/slack/rtm/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from slack_sdk.rtm import RTMClient # noqa
2+
from slack_sdk.web.legacy_client import LegacyWebClient as WebClient # noqa
3+
4+
from slack import deprecation
5+
6+
deprecation.show_message(__name__, "slack_sdk.web/rtm")

src/slack/rtm/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from slack_sdk.rtm import RTMClient # noqa
2+
from slack_sdk.web.legacy_client import LegacyWebClient as WebClient # noqa
3+
4+
from slack import deprecation
5+
6+
deprecation.show_message(__name__, "slack_sdk.rtm.client")

src/slack/signature/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from slack_sdk.signature import SignatureVerifier # noqa
2+
3+
from slack import deprecation
4+
5+
deprecation.show_message(__name__, "slack_sdk.signature")

src/slack/web/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import slack_sdk.version as slack_version # noqa
2+
from slack import deprecation
3+
from slack_sdk.web.async_client import AsyncSlackResponse # noqa
4+
from slack_sdk.web.async_client import AsyncWebClient # noqa
5+
from slack_sdk.web.internal_utils import _to_0_or_1_if_bool # noqa
6+
from slack_sdk.web.internal_utils import convert_bool_to_0_or_1 # noqa
7+
from slack_sdk.web.internal_utils import get_user_agent # noqa
8+
from slack_sdk.web.legacy_client import LegacyWebClient as WebClient # noqa
9+
from slack_sdk.web.slack_response import SlackResponse # noqa
10+
11+
deprecation.show_message(__name__, "slack_sdk.web")

0 commit comments

Comments
 (0)
X Tutup