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