-
Notifications
You must be signed in to change notification settings - Fork 853
Expand file tree
/
Copy pathcodegen.py
More file actions
153 lines (146 loc) · 5.75 KB
/
codegen.py
File metadata and controls
153 lines (146 loc) · 5.75 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import argparse
import sys
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,
)
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(
"from slack_sdk.web.chat_stream import ChatStream",
"from slack_sdk.web.async_chat_stream import AsyncChatStream",
async_source,
)
async_source = re.sub(r"ChatStream:", "AsyncChatStream:", async_source)
async_source = re.sub(r"ChatStream\(", "AsyncChatStream(", async_source)
async_source = re.sub(
r" client.chat_stream\(",
" await client.chat_stream(",
async_source,
)
async_source = re.sub(
r" streamer.append\(",
" await streamer.append(",
async_source,
)
async_source = re.sub(
r" streamer.stop\(",
" await streamer.stop(",
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)
legacy_source = re.sub(r"^from slack_sdk.web.chat_stream import ChatStream\n", "", legacy_source, flags=re.MULTILINE)
legacy_source = re.sub(r"(?s)def chat_stream.*?(?=def)", "", legacy_source)
with open(f"{args.path}/slack_sdk/web/legacy_client.py", "w") as output:
output.write(legacy_source)
with open(f"{args.path}/slack_sdk/web/chat_stream.py", "r") as original:
source = original.read()
import re
async_source = header + source
async_source = re.sub(
"from slack_sdk.web.slack_response import SlackResponse",
"from slack_sdk.web.async_slack_response import AsyncSlackResponse",
async_source,
)
async_source = re.sub(
r"from slack_sdk import WebClient",
"from slack_sdk.web.async_client import AsyncWebClient",
async_source,
)
async_source = re.sub("class ChatStream", "class AsyncChatStream", async_source)
async_source = re.sub('"WebClient"', '"AsyncWebClient"', async_source)
async_source = re.sub(r"Optional\[SlackResponse\]", "Optional[AsyncSlackResponse]", async_source)
async_source = re.sub(r"SlackResponse ", "AsyncSlackResponse ", async_source)
async_source = re.sub(r"SlackResponse:", "AsyncSlackResponse:", async_source)
async_source = re.sub(r"def append\(", "async def append(", async_source)
async_source = re.sub(r"def stop\(", "async def stop(", async_source)
async_source = re.sub(r"def _flush_buffer\(", "async def _flush_buffer(", async_source)
async_source = re.sub("self._client.chat_", "await self._client.chat_", async_source)
async_source = re.sub("self._flush_buffer", "await self._flush_buffer", async_source)
with open(f"{args.path}/slack_sdk/web/async_chat_stream.py", "w") as output:
output.write(async_source)