From 39b6a3e5e009721d4639c20997a91191bdda0598 Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Sat, 2 May 2020 20:58:31 +0400 Subject: [PATCH] Add unasync.unasync_files() API It allows to run unasync on multiple files using multiples rules if needed, without being tied to setuptools. --- docs/source/index.rst | 17 +++++++++++++++++ src/unasync/__init__.py | 28 ++++++++++++++++------------ tests/test_unasync.py | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index a5f09fc..0341734 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -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 diff --git a/src/unasync/__init__.py b/src/unasync/__init__.py index 0406fe6..21a79e5 100644 --- a/src/unasync/__init__.py +++ b/src/unasync/__init__.py @@ -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"]) @@ -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)) diff --git a/tests/test_unasync.py b/tests/test_unasync.py index fd8abf5..9e3bb59 100644 --- a/tests/test_unasync.py +++ b/tests/test_unasync.py @@ -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")