Skip to content

Commit

Permalink
Add script for running unasync
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson committed Oct 26, 2021
1 parent 722e0d4 commit 620032d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
3 changes: 2 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ def test(session):

@nox.session()
def format(session):
session.install("black", "isort", "flynt")
session.install("black", "isort", "flynt", "unasync")

session.run("python", "utils/run-unasync.py")
session.run("isort", "--profile=black", *SOURCE_FILES)
session.run("flynt", *SOURCE_FILES)
session.run("black", "--target-version=py36", *SOURCE_FILES)
Expand Down
32 changes: 1 addition & 31 deletions utils/generate-api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from pathlib import Path

import black
import unasync
import urllib3
from click.testing import CliRunner
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
Expand Down Expand Up @@ -392,36 +391,7 @@ def dump_modules(modules):
for mod in modules.values():
mod.dump()

# Unasync all the generated async code
additional_replacements = {
# We want to rewrite to 'Transport' instead of 'SyncTransport', etc
"AsyncTransport": "Transport",
"AsyncElasticsearch": "Elasticsearch",
# We don't want to rewrite this class
"AsyncSearchClient": "AsyncSearchClient",
}
rules = [
unasync.Rule(
fromdir="/elasticsearch/_async/client/",
todir="/elasticsearch/_sync/client/",
additional_replacements=additional_replacements,
),
]

filepaths = []
for root, _, filenames in os.walk(CODE_ROOT / "elasticsearch/_async"):
for filename in filenames:
if (
filename.rpartition(".")[-1]
in (
"py",
"pyi",
)
and not filename.startswith("utils.py")
):
filepaths.append(os.path.join(root, filename))

unasync.unasync_files(filepaths, rules)
os.system("python utils/run-unasync.py")
blacken(CODE_ROOT / "elasticsearch")


Expand Down
62 changes: 62 additions & 0 deletions utils/run-unasync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
from pathlib import Path

import unasync


def main():
# Unasync all the generated async code
additional_replacements = {
# We want to rewrite to 'Transport' instead of 'SyncTransport', etc
"AsyncTransport": "Transport",
"AsyncElasticsearch": "Elasticsearch",
# We don't want to rewrite this class
"AsyncSearchClient": "AsyncSearchClient",
# Handling typing.Awaitable[...] isn't done yet by unasync.
"_TYPE_ASYNC_SNIFF_CALLBACK": "_TYPE_SYNC_SNIFF_CALLBACK",
}
rules = [
unasync.Rule(
fromdir="/elasticsearch/_async/client/",
todir="/elasticsearch/_sync/client/",
additional_replacements=additional_replacements,
),
]

filepaths = []
for root, _, filenames in os.walk(
Path(__file__).absolute().parent.parent / "elasticsearch/_async"
):
for filename in filenames:
if (
filename.rpartition(".")[-1]
in (
"py",
"pyi",
)
and not filename.startswith("utils.py")
):
filepaths.append(os.path.join(root, filename))

unasync.unasync_files(filepaths, rules)


if __name__ == "__main__":
main()

0 comments on commit 620032d

Please sign in to comment.