|
1 | 1 | #!/usr/bin/env python |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
2 | 5 | from glob import glob |
3 | 6 | from os.path import splitext, basename |
4 | 7 |
|
|
10 | 13 | with open("README.md", "r") as fh: |
11 | 14 | long_description = fh.read() |
12 | 15 |
|
| 16 | +here = os.path.abspath(os.path.dirname(__file__)) |
| 17 | + |
| 18 | +codegen_dependencies = [ |
| 19 | + "black==19.10b0", |
| 20 | +] |
| 21 | + |
13 | 22 | test_dependencies = [ |
14 | 23 | "pytest>=5,<6", |
15 | | - "pytest-asyncio<1", |
| 24 | + "pytest-asyncio<1", # for async |
16 | 25 | "aiohttp>=3,<4", # for async |
17 | 26 | ] |
18 | 27 |
|
| 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 | + |
19 | 108 | setuptools.setup( |
20 | 109 | name="slack_bolt", |
21 | 110 | version=__version__, |
|
64 | 153 | "Operating System :: OS Independent", |
65 | 154 | ], |
66 | 155 | python_requires='>=3.6', |
| 156 | + cmdclass={ |
| 157 | + "codegen": CodegenCommand, |
| 158 | + }, |
67 | 159 | ) |
0 commit comments