|
| 1 | +import sys |
| 2 | +import argparse |
| 3 | + |
| 4 | +parser = argparse.ArgumentParser() |
| 5 | +parser.add_argument("-p", "--path", help="Path to the project source code.", type=str) |
| 6 | +if len(sys.argv) == 1: |
| 7 | + parser.print_help(sys.stderr) |
| 8 | + sys.exit(1) |
| 9 | +args = parser.parse_args() |
| 10 | + |
| 11 | +header = ( |
| 12 | + "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" |
| 13 | + "#\n" |
| 14 | + "# *** DO NOT EDIT THIS FILE ***\n" |
| 15 | + "#\n" |
| 16 | + "# 1) Modify slack_sdk/web/client.py\n" |
| 17 | + "# 2) Run `python scripts/codegen.py`\n" |
| 18 | + "# 3) Run `black slack_sdk/`\n" |
| 19 | + "#\n" |
| 20 | + "# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" |
| 21 | + "\n" |
| 22 | +) |
| 23 | + |
| 24 | +with open(f"{args.path}/slack_sdk/web/client.py", "r") as original: |
| 25 | + source = original.read() |
| 26 | + import re |
| 27 | + |
| 28 | + async_source = header + source |
| 29 | + async_source = re.sub(" def ", " async def ", async_source) |
| 30 | + async_source = re.sub("from asyncio import Future\n", "", async_source) |
| 31 | + async_source = re.sub(r"return self.api_call\(", "return await self.api_call(", async_source) |
| 32 | + async_source = re.sub("-> SlackResponse", "-> AsyncSlackResponse", async_source) |
| 33 | + async_source = re.sub( |
| 34 | + "from .base_client import BaseClient, SlackResponse", |
| 35 | + "from .async_base_client import AsyncBaseClient, AsyncSlackResponse", |
| 36 | + async_source, |
| 37 | + ) |
| 38 | + # from slack_sdk import WebClient |
| 39 | + async_source = re.sub( |
| 40 | + r"class WebClient\(BaseClient\):", |
| 41 | + "class AsyncWebClient(AsyncBaseClient):", |
| 42 | + async_source, |
| 43 | + ) |
| 44 | + async_source = re.sub( |
| 45 | + "from slack_sdk import WebClient", |
| 46 | + "from slack_sdk.web.async_client import AsyncWebClient", |
| 47 | + async_source, |
| 48 | + ) |
| 49 | + async_source = re.sub(r"= WebClient\(", "= AsyncWebClient(", async_source) |
| 50 | + async_source = re.sub( |
| 51 | + r" self.files_getUploadURLExternal\(", |
| 52 | + " await self.files_getUploadURLExternal(", |
| 53 | + async_source, |
| 54 | + ) |
| 55 | + async_source = re.sub( |
| 56 | + r" self._upload_file\(", |
| 57 | + " await self._upload_file(", |
| 58 | + async_source, |
| 59 | + ) |
| 60 | + async_source = re.sub( |
| 61 | + r" self.files_completeUploadExternal\(", |
| 62 | + " await self.files_completeUploadExternal(", |
| 63 | + async_source, |
| 64 | + ) |
| 65 | + async_source = re.sub( |
| 66 | + r" self.files_info\(", |
| 67 | + " await self.files_info(", |
| 68 | + async_source, |
| 69 | + ) |
| 70 | + async_source = re.sub( |
| 71 | + "_attach_full_file_metadata", |
| 72 | + "_attach_full_file_metadata_async", |
| 73 | + async_source, |
| 74 | + ) |
| 75 | + async_source = re.sub( |
| 76 | + r" _attach_full_file_metadata_async\(", |
| 77 | + " await _attach_full_file_metadata_async(", |
| 78 | + async_source, |
| 79 | + ) |
| 80 | + with open(f"{args.path}/slack_sdk/web/async_client.py", "w") as output: |
| 81 | + output.write(async_source) |
| 82 | + |
| 83 | + legacy_source = header + "from asyncio import Future\n" + source |
| 84 | + legacy_source = re.sub("-> SlackResponse", "-> Union[Future, SlackResponse]", legacy_source) |
| 85 | + legacy_source = re.sub( |
| 86 | + "from .base_client import BaseClient, SlackResponse", |
| 87 | + "from .legacy_base_client import LegacyBaseClient, SlackResponse", |
| 88 | + legacy_source, |
| 89 | + ) |
| 90 | + legacy_source = re.sub( |
| 91 | + r"class WebClient\(BaseClient\):", |
| 92 | + "class LegacyWebClient(LegacyBaseClient):", |
| 93 | + legacy_source, |
| 94 | + ) |
| 95 | + legacy_source = re.sub( |
| 96 | + "from slack_sdk import WebClient", |
| 97 | + "from slack_sdk.web.legacy_client import LegacyWebClient", |
| 98 | + legacy_source, |
| 99 | + ) |
| 100 | + legacy_source = re.sub(r"= WebClient\(", "= LegacyWebClient(", legacy_source) |
| 101 | + with open(f"{args.path}/slack_sdk/web/legacy_client.py", "w") as output: |
| 102 | + output.write(legacy_source) |
0 commit comments