Skip to content

Commit 92dbca6

Browse files
sethmlarsonpquentin
authored andcommitted
Allow custom directory in place of '_async' (#54)
1 parent 2c2c9b7 commit 92dbca6

File tree

10 files changed

+59
-7
lines changed

10 files changed

+59
-7
lines changed

src/unasync/__init__.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ def unasync_name(name):
6464
if name in ASYNC_TO_SYNC:
6565
return ASYNC_TO_SYNC[name]
6666
# Convert classes prefixed with 'Async' into 'Sync'
67-
elif (
68-
len(name) > 5
69-
and name.startswith("Async")
70-
and name[5].isupper()
71-
):
67+
elif len(name) > 5 and name.startswith("Async") and name[5].isupper():
7268
return "Sync" + name[5:]
7369
return name
7470

@@ -131,7 +127,11 @@ class build_py(orig.build_py):
131127
and saves them in _sync dir.
132128
"""
133129

130+
RENAME_DIR_FROM_TO = ("_async", "_sync") # Controls what directory will be renamed.
131+
134132
def run(self):
133+
dir_from, dir_to = self.RENAME_DIR_FROM_TO
134+
135135
self._updated_files = []
136136

137137
# Base class code
@@ -143,8 +143,8 @@ def run(self):
143143

144144
# Our modification!
145145
for f in self._updated_files:
146-
if os.sep + "_async" + os.sep in f:
147-
unasync_file(f, "_async", "_sync")
146+
if os.sep + dir_from + os.sep in f:
147+
unasync_file(f, dir_from, dir_to)
148148

149149
# Remaining base class code
150150
self.byte_compile(self.get_outputs(include_bytecode=0))
@@ -154,3 +154,10 @@ def build_module(self, module, module_file, package):
154154
if copied:
155155
self._updated_files.append(outfile)
156156
return outfile, copied
157+
158+
159+
def customize_build_py(rename_dir_from_to=("_async", "_sync")):
160+
class _build_py(build_py):
161+
RENAME_DIR_FROM_TO = rename_dir_from_to
162+
163+
return _build_py
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import setuptools
2+
3+
import unasync
4+
5+
setuptools.setup(
6+
name="ahip",
7+
version="0.0.1",
8+
author="Example Author",
9+
author_email="author@example.com",
10+
description="A package used to test customized unasync",
11+
url="https://github.com/pypa/sampleproject",
12+
packages=["ahip", "ahip.some_dir"],
13+
cmdclass={
14+
"build_py": unasync.customize_build_py(rename_dir_from_to=("ahip", "hip"))
15+
},
16+
package_dir={"": "src"},
17+
)

tests/data/example_custom_pkg/src/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def f():
2+
return await 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def f():
2+
return await 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def f():
2+
return await 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def f():
2+
return await 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def f():
2+
return await 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
async def f():
2+
return await 1

tests/test_unasync.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,19 @@ def test_project_structure_after_build_py_packages(tmpdir):
9696
)
9797

9898
assert _async_dir_tree == unasynced_dir_tree
99+
100+
101+
def test_project_structure_after_customized_build_py_packages(tmpdir):
102+
103+
source_pkg_dir = os.path.join(TEST_DIR, "example_custom_pkg")
104+
pkg_dir = str(tmpdir) + "/" + "example_custom_pkg"
105+
shutil.copytree(source_pkg_dir, pkg_dir)
106+
107+
env = copy.copy(os.environ)
108+
env["PYTHONPATH"] = os.path.realpath(os.path.join(TEST_DIR, ".."))
109+
subprocess.check_call(["python", "setup.py", "build"], cwd=pkg_dir, env=env)
110+
111+
_async_dir_tree = list_files(os.path.join(source_pkg_dir, "src/ahip/."))
112+
unasynced_dir_tree = list_files(os.path.join(pkg_dir, "build/lib/hip/."))
113+
114+
assert _async_dir_tree == unasynced_dir_tree

0 commit comments

Comments
 (0)