Skip to content

Commit 6b56fb3

Browse files
unasyncing of examples
1 parent c172281 commit 6b56fb3

File tree

8 files changed

+102
-29
lines changed

8 files changed

+102
-29
lines changed

examples/alias_migration.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def migrate(move_data=True, update_alias=True):
125125
)
126126

127127

128-
if __name__ == "__main__":
128+
def main():
129129
# initiate the default connection to elasticsearch
130130
connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
131131

@@ -143,3 +143,10 @@ def migrate(move_data=True, update_alias=True):
143143

144144
# create new index
145145
migrate()
146+
147+
# close the connection
148+
connections.get_connection().close()
149+
150+
151+
if __name__ == "__main__":
152+
main()

examples/async/alias_migration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
alias.
3737
"""
3838
import asyncio
39+
import os
3940
from datetime import datetime
4041
from fnmatch import fnmatch
4142

@@ -127,7 +128,7 @@ async def migrate(move_data=True, update_alias=True):
127128

128129
async def main():
129130
# initiate the default connection to elasticsearch
130-
async_connections.create_connection(hosts=["http://localhost:9200"])
131+
async_connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
131132

132133
# create the empty index
133134
await setup()

examples/completion.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Index:
7272
settings = {"number_of_shards": 1, "number_of_replicas": 0}
7373

7474

75-
if __name__ == "__main__":
75+
def main():
7676
# initiate the default connection to elasticsearch
7777
connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
7878

@@ -97,3 +97,10 @@ class Index:
9797
# print out all the options we got
9898
for option in response.suggest.auto_complete[0].options:
9999
print("%10s: %25s (%d)" % (text, option._source.name, option._score))
100+
101+
# close the connection
102+
connections.get_connection().close()
103+
104+
105+
if __name__ == "__main__":
106+
main()

examples/composite_agg.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ def run_search(**kwargs):
3636

3737
response = run_search()
3838
while response.aggregations.comp.buckets:
39-
yield from response.aggregations.comp.buckets
39+
for b in response.aggregations.comp.buckets:
40+
yield b
4041
if "after_key" in response.aggregations.comp:
4142
after = response.aggregations.comp.after_key
4243
else:
4344
after = response.aggregations.comp.buckets[-1].key
4445
response = run_search(after=after)
4546

4647

47-
if __name__ == "__main__":
48+
def main():
4849
# initiate the default connection to elasticsearch
4950
connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
5051

@@ -57,3 +58,10 @@ def run_search(**kwargs):
5758
"File %s has been modified %d times, first seen at %s."
5859
% (b.key.files, b.doc_count, b.first_seen.value_as_string)
5960
)
61+
62+
# close the connection
63+
connections.get_connection().close()
64+
65+
66+
if __name__ == "__main__":
67+
main()

examples/parent_child.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def get_answers(self):
165165
"""
166166
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
167167
return self.meta.inner_hits.answer.hits
168-
return list(self.search_answers())
168+
return [a for a in self.search_answers()]
169169

170170
def save(self, **kwargs):
171171
self.question_answer = "question"
@@ -208,7 +208,7 @@ def setup():
208208
index_template.save()
209209

210210

211-
if __name__ == "__main__":
211+
def main():
212212
# initiate the default connection to elasticsearch
213213
connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
214214

@@ -243,3 +243,12 @@ def setup():
243243
)
244244
question.save()
245245
answer = question.add_answer(honza, "Just use `elasticsearch-py`!")
246+
247+
# close the connection
248+
connections.get_connection().close()
249+
250+
return answer
251+
252+
253+
if __name__ == "__main__":
254+
main()

examples/percolate.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,15 @@ def setup():
9191
).save(refresh=True)
9292

9393

94-
if __name__ == "__main__":
94+
def main():
9595
# initiate the default connection to elasticsearch
9696
connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
9797

9898
setup()
99+
100+
# close the connection
101+
connections.get_connection().close()
102+
103+
104+
if __name__ == "__main__":
105+
main()

examples/search_as_you_type.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Index:
5353
settings = {"number_of_shards": 1, "number_of_replicas": 0}
5454

5555

56-
if __name__ == "__main__":
56+
def main():
5757
# initiate the default connection to elasticsearch
5858
connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
5959

@@ -92,3 +92,10 @@ class Index:
9292
# print out all the options we got
9393
for h in response:
9494
print("%15s: %25s" % (text, h.name))
95+
96+
# close the connection
97+
connections.get_connection().close()
98+
99+
100+
if __name__ == "__main__":
101+
main()

utils/run-unasync.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,33 @@
1818
import os
1919
import subprocess
2020
import sys
21+
from glob import glob
2122
from pathlib import Path
2223

2324
import unasync
2425

2526

2627
def main(check=False):
28+
# the list of directories that need to be processed with unasync
29+
# each entry has two paths:
30+
# - the source path with the async sources
31+
# - the destination path where the sync sources should be written
2732
source_dirs = [
28-
"elasticsearch_dsl",
29-
"tests",
30-
"tests/test_integration",
31-
"tests/test_integration/test_examples",
33+
(
34+
"elasticsearch_dsl/_async/",
35+
"elasticsearch_dsl/_sync/",
36+
),
37+
("tests/_async/", "tests/_sync/"),
38+
(
39+
"tests/test_integration/_async/",
40+
"tests/test_integration/_sync/",
41+
),
42+
(
43+
"tests/test_integration/test_examples/_async",
44+
"tests/test_integration/test_examples/_sync/",
45+
),
46+
("examples/async/", "examples/"),
3247
]
33-
output_dir = "_sync" if not check else "_sync_check"
3448

3549
# Unasync all the generated async code
3650
additional_replacements = {
@@ -58,11 +72,11 @@ def main(check=False):
5872
}
5973
rules = [
6074
unasync.Rule(
61-
fromdir=f"{source_dir}/_async/",
62-
todir=f"{source_dir}/{output_dir}/",
75+
fromdir=dir[0],
76+
todir=f"{dir[0]}_sync_check/" if check else dir[1],
6377
additional_replacements=additional_replacements,
6478
)
65-
for source_dir in source_dirs
79+
for dir in source_dirs
6680
]
6781

6882
filepaths = []
@@ -75,24 +89,37 @@ def main(check=False):
7589
filepaths.append(os.path.join(root, filename))
7690

7791
unasync.unasync_files(filepaths, rules)
78-
79-
if check:
80-
# make sure there are no differences between _sync and _sync_check
81-
for source_dir in source_dirs:
92+
for dir in source_dirs:
93+
output_dir = f"{dir[0]}_sync_check/" if check else dir[1]
94+
subprocess.check_call(["black", "--target-version=py38", output_dir])
95+
subprocess.check_call(["isort", output_dir])
96+
for file in glob("*.py", root_dir=dir[0]):
97+
# remove asyncio from sync files
8298
subprocess.check_call(
83-
["black", "--target-version=py38", f"{source_dir}/_sync_check/"]
99+
["sed", "-i.bak", "/^import asyncio$/d", f"{output_dir}{file}"]
84100
)
85-
subprocess.check_call(["isort", f"{source_dir}/_sync_check/"])
86101
subprocess.check_call(
87102
[
88-
"diff",
89-
"-x",
90-
"__pycache__",
91-
f"{source_dir}/_sync",
92-
f"{source_dir}/_sync_check",
103+
"sed",
104+
"-i.bak",
105+
"s/asyncio\\.run(main())/main()/",
106+
f"{output_dir}{file}",
93107
]
94108
)
95-
subprocess.check_call(["rm", "-rf", f"{source_dir}/_sync_check"])
109+
subprocess.check_call(["rm", f"{output_dir}{file}.bak"])
110+
111+
if check:
112+
# make sure there are no differences between _sync and _sync_check
113+
subprocess.check_call(
114+
[
115+
"diff",
116+
f"{dir[1]}{file}",
117+
f"{output_dir}{file}",
118+
]
119+
)
120+
121+
if check:
122+
subprocess.check_call(["rm", "-rf", output_dir])
96123

97124

98125
if __name__ == "__main__":

0 commit comments

Comments
 (0)