Skip to content

Commit 620032d

Browse files
committed
Add script for running unasync
1 parent 722e0d4 commit 620032d

File tree

3 files changed

+65
-32
lines changed

3 files changed

+65
-32
lines changed

noxfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def test(session):
5757

5858
@nox.session()
5959
def format(session):
60-
session.install("black", "isort", "flynt")
60+
session.install("black", "isort", "flynt", "unasync")
6161

62+
session.run("python", "utils/run-unasync.py")
6263
session.run("isort", "--profile=black", *SOURCE_FILES)
6364
session.run("flynt", *SOURCE_FILES)
6465
session.run("black", "--target-version=py36", *SOURCE_FILES)

utils/generate-api.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from pathlib import Path
3232

3333
import black
34-
import unasync
3534
import urllib3
3635
from click.testing import CliRunner
3736
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
@@ -392,36 +391,7 @@ def dump_modules(modules):
392391
for mod in modules.values():
393392
mod.dump()
394393

395-
# Unasync all the generated async code
396-
additional_replacements = {
397-
# We want to rewrite to 'Transport' instead of 'SyncTransport', etc
398-
"AsyncTransport": "Transport",
399-
"AsyncElasticsearch": "Elasticsearch",
400-
# We don't want to rewrite this class
401-
"AsyncSearchClient": "AsyncSearchClient",
402-
}
403-
rules = [
404-
unasync.Rule(
405-
fromdir="/elasticsearch/_async/client/",
406-
todir="/elasticsearch/_sync/client/",
407-
additional_replacements=additional_replacements,
408-
),
409-
]
410-
411-
filepaths = []
412-
for root, _, filenames in os.walk(CODE_ROOT / "elasticsearch/_async"):
413-
for filename in filenames:
414-
if (
415-
filename.rpartition(".")[-1]
416-
in (
417-
"py",
418-
"pyi",
419-
)
420-
and not filename.startswith("utils.py")
421-
):
422-
filepaths.append(os.path.join(root, filename))
423-
424-
unasync.unasync_files(filepaths, rules)
394+
os.system("python utils/run-unasync.py")
425395
blacken(CODE_ROOT / "elasticsearch")
426396

427397

utils/run-unasync.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import os
19+
from pathlib import Path
20+
21+
import unasync
22+
23+
24+
def main():
25+
# Unasync all the generated async code
26+
additional_replacements = {
27+
# We want to rewrite to 'Transport' instead of 'SyncTransport', etc
28+
"AsyncTransport": "Transport",
29+
"AsyncElasticsearch": "Elasticsearch",
30+
# We don't want to rewrite this class
31+
"AsyncSearchClient": "AsyncSearchClient",
32+
# Handling typing.Awaitable[...] isn't done yet by unasync.
33+
"_TYPE_ASYNC_SNIFF_CALLBACK": "_TYPE_SYNC_SNIFF_CALLBACK",
34+
}
35+
rules = [
36+
unasync.Rule(
37+
fromdir="/elasticsearch/_async/client/",
38+
todir="/elasticsearch/_sync/client/",
39+
additional_replacements=additional_replacements,
40+
),
41+
]
42+
43+
filepaths = []
44+
for root, _, filenames in os.walk(
45+
Path(__file__).absolute().parent.parent / "elasticsearch/_async"
46+
):
47+
for filename in filenames:
48+
if (
49+
filename.rpartition(".")[-1]
50+
in (
51+
"py",
52+
"pyi",
53+
)
54+
and not filename.startswith("utils.py")
55+
):
56+
filepaths.append(os.path.join(root, filename))
57+
58+
unasync.unasync_files(filepaths, rules)
59+
60+
61+
if __name__ == "__main__":
62+
main()

0 commit comments

Comments
 (0)