Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ customized :code:`unasync.Rule` instances to :code:`unasync.cmdclass_build_py()`
...
)

---------------------------
Usage outside of setuptools
---------------------------

You can also use unasync without setuptools, to run unasync on tests, for example.

.. code-block:: python

import unasync

unasync.unasync_files(
[file1, file2, ...],
rules=[
unasync.Rule("tests/", "tests_sync/", replacements={"ahip": "hip"}),
]
)


.. toctree::
:maxdepth: 2
Expand Down
28 changes: 16 additions & 12 deletions src/unasync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ def _unasync_name(self, name):
return name


def unasync_files(fpath_list, rules):
for f in fpath_list:
found_rule = None
found_weight = None

for rule in rules:
weight = rule._match(f)
if weight and (found_weight is None or weight > found_weight):
found_rule = rule
found_weight = weight

if found_rule:
found_rule.unasync_file(f)


Token = collections.namedtuple("Token", ["type", "string", "start", "end", "line"])


Expand Down Expand Up @@ -179,18 +194,7 @@ def run(self):
self.build_package_data()

# Our modification!
for f in self._updated_files:
found_rule = None
found_weight = None

for rule in rules:
weight = rule._match(f)
if weight and (found_weight is None or weight > found_weight):
found_rule = rule
found_weight = weight

if found_rule:
found_rule.unasync_file(f)
unasync_files(self._updated_files, rules)

# Remaining base class code
self.byte_compile(self.get_outputs(include_bytecode=0))
Expand Down
16 changes: 16 additions & 0 deletions tests/test_unasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ def test_unasync(tmpdir, source_file):
assert unasynced_code == truth


def test_unasync_files(tmpdir):
"""Test the unasync_files API, not tied by a Rule or to setuptools."""
unasync.unasync_files(
[os.path.join(ASYNC_DIR, fpath) for fpath in TEST_FILES],
rules=[unasync.Rule(fromdir=ASYNC_DIR, todir=str(tmpdir))],
)

for source_file in TEST_FILES:
encoding = "latin-1" if "encoding" in source_file else "utf-8"
with io.open(os.path.join(SYNC_DIR, source_file), encoding=encoding) as f:
truth = f.read()
with io.open(os.path.join(str(tmpdir), source_file), encoding=encoding) as f:
unasynced_code = f.read()
assert unasynced_code == truth


def test_build_py_modules(tmpdir):

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