Skip to content

Commit 3ee4a8a

Browse files
authored
Add unasync.unasync_files() API (#58)
1 parent cfd7c7f commit 3ee4a8a

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

docs/source/index.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ customized :code:`unasync.Rule` instances to :code:`unasync.cmdclass_build_py()`
8686
...
8787
)
8888
89+
---------------------------
90+
Usage outside of setuptools
91+
---------------------------
92+
93+
You can also use unasync without setuptools, to run unasync on tests, for example.
94+
95+
.. code-block:: python
96+
97+
import unasync
98+
99+
unasync.unasync_files(
100+
[file1, file2, ...],
101+
rules=[
102+
unasync.Rule("tests/", "tests_sync/", replacements={"ahip": "hip"}),
103+
]
104+
)
105+
89106
90107
.. toctree::
91108
:maxdepth: 2

src/unasync/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ def _unasync_name(self, name):
108108
return name
109109

110110

111+
def unasync_files(fpath_list, rules):
112+
for f in fpath_list:
113+
found_rule = None
114+
found_weight = None
115+
116+
for rule in rules:
117+
weight = rule._match(f)
118+
if weight and (found_weight is None or weight > found_weight):
119+
found_rule = rule
120+
found_weight = weight
121+
122+
if found_rule:
123+
found_rule.unasync_file(f)
124+
125+
111126
Token = collections.namedtuple("Token", ["type", "string", "start", "end", "line"])
112127

113128

@@ -179,18 +194,7 @@ def run(self):
179194
self.build_package_data()
180195

181196
# Our modification!
182-
for f in self._updated_files:
183-
found_rule = None
184-
found_weight = None
185-
186-
for rule in rules:
187-
weight = rule._match(f)
188-
if weight and (found_weight is None or weight > found_weight):
189-
found_rule = rule
190-
found_weight = weight
191-
192-
if found_rule:
193-
found_rule.unasync_file(f)
197+
unasync_files(self._updated_files, rules)
194198

195199
# Remaining base class code
196200
self.byte_compile(self.get_outputs(include_bytecode=0))

tests/test_unasync.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ def test_unasync(tmpdir, source_file):
4747
assert unasynced_code == truth
4848

4949

50+
def test_unasync_files(tmpdir):
51+
"""Test the unasync_files API, not tied by a Rule or to setuptools."""
52+
unasync.unasync_files(
53+
[os.path.join(ASYNC_DIR, fpath) for fpath in TEST_FILES],
54+
rules=[unasync.Rule(fromdir=ASYNC_DIR, todir=str(tmpdir))],
55+
)
56+
57+
for source_file in TEST_FILES:
58+
encoding = "latin-1" if "encoding" in source_file else "utf-8"
59+
with io.open(os.path.join(SYNC_DIR, source_file), encoding=encoding) as f:
60+
truth = f.read()
61+
with io.open(os.path.join(str(tmpdir), source_file), encoding=encoding) as f:
62+
unasynced_code = f.read()
63+
assert unasynced_code == truth
64+
65+
5066
def test_build_py_modules(tmpdir):
5167

5268
source_modules_dir = os.path.join(TEST_DIR, "example_mod")

0 commit comments

Comments
 (0)