From 7f1a17764691afaad643b40eedc5f28905a79885 Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Sat, 2 May 2020 22:35:12 +0400 Subject: [PATCH 1/2] Mark Rule.unasync_file as private The public API is unasync.unasync_files now. --- src/unasync/__init__.py | 4 ++-- tests/test_unasync.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/unasync/__init__.py b/src/unasync/__init__.py index 21a79e5..5fcbd71 100644 --- a/src/unasync/__init__.py +++ b/src/unasync/__init__.py @@ -63,7 +63,7 @@ def _match(self, filepath): return False - def unasync_file(self, filepath): + def _unasync_file(self, filepath): with open(filepath, "rb") as f: write_kwargs = {} if sys.version_info[0] >= 3: @@ -120,7 +120,7 @@ def unasync_files(fpath_list, rules): found_weight = weight if found_rule: - found_rule.unasync_file(f) + found_rule._unasync_file(f) Token = collections.namedtuple("Token", ["type", "string", "start", "end", "line"]) diff --git a/tests/test_unasync.py b/tests/test_unasync.py index 9e3bb59..d4c810e 100644 --- a/tests/test_unasync.py +++ b/tests/test_unasync.py @@ -37,7 +37,7 @@ def test_rule_on_short_path(): def test_unasync(tmpdir, source_file): rule = unasync.Rule(fromdir=ASYNC_DIR, todir=str(tmpdir)) - rule.unasync_file(os.path.join(ASYNC_DIR, source_file)) + rule._unasync_file(os.path.join(ASYNC_DIR, source_file)) 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: From c70abc60292d870b4088f14d79137f17dd6774e2 Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Sat, 2 May 2020 22:39:52 +0400 Subject: [PATCH 2/2] Rename replacements to additional_replacements It clarifies that we're only adding replacement, not replacing the existing ones. Also, black tends to put this on a single line, so we the additional characters are not a problem in practice. --- docs/source/index.rst | 4 ++-- src/unasync/__init__.py | 4 ++-- tests/data/example_custom_pkg/setup.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index 0341734..d5d6a9d 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -81,7 +81,7 @@ customized :code:`unasync.Rule` instances to :code:`unasync.cmdclass_build_py()` # This rule's 'fromdir' is more specific so will take precedent # over the above rule if the path is within /ahip/tests/... # This rule adds an additional token replacement over the default replacements. - unasync.Rule("/ahip/tests/", "/hip/tests/", replacements={"ahip": "hip"}), + unasync.Rule("/ahip/tests/", "/hip/tests/", additional_replacements={"ahip": "hip"}), ])}, ... ) @@ -99,7 +99,7 @@ You can also use unasync without setuptools, to run unasync on tests, for exampl unasync.unasync_files( [file1, file2, ...], rules=[ - unasync.Rule("tests/", "tests_sync/", replacements={"ahip": "hip"}), + unasync.Rule("tests/", "tests_sync/", additional_replacements={"ahip": "hip"}), ] ) diff --git a/src/unasync/__init__.py b/src/unasync/__init__.py index 5fcbd71..7ea5e80 100644 --- a/src/unasync/__init__.py +++ b/src/unasync/__init__.py @@ -37,13 +37,13 @@ class Rule: """A single set of rules for 'unasync'ing file(s)""" - def __init__(self, fromdir, todir, replacements=None): + def __init__(self, fromdir, todir, additional_replacements=None): self.fromdir = fromdir.replace("/", os.sep) self.todir = todir.replace("/", os.sep) # Add any additional user-defined token replacements to our list. self.token_replacements = _ASYNC_TO_SYNC.copy() - for key, val in (replacements or {}).items(): + for key, val in (additional_replacements or {}).items(): self.token_replacements[key] = val def _match(self, filepath): diff --git a/tests/data/example_custom_pkg/setup.py b/tests/data/example_custom_pkg/setup.py index bea55bf..0cfa1ee 100644 --- a/tests/data/example_custom_pkg/setup.py +++ b/tests/data/example_custom_pkg/setup.py @@ -17,7 +17,7 @@ unasync.Rule( fromdir="/ahip/tests/", todir="/hip/tests/", - replacements={"ahip": "hip"}, + additional_replacements={"ahip": "hip"}, ), ] )