Repository navigation
Replace rewrite_imports with map_imports #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright (c) 2025-2026 Buf Technologies, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
|
|
||
| MapImports = tuple[tuple[re.Pattern[str], str], ...] | ||
|
|
||
| _OPTION_NAME = "map_imports" | ||
|
|
||
| _TARGET = re.compile(r"[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*") | ||
|
|
||
|
|
||
| def compile_map_imports(mappings: dict[str, str]) -> MapImports: | ||
| compiled: list[tuple[re.Pattern[str], str]] = [] | ||
| for pattern, target in mappings.items(): | ||
| if not pattern: | ||
| msg = f"option '{_OPTION_NAME}': pattern must not be empty" | ||
| raise ValueError(msg) | ||
| # An empty target (or ".") maps to the canonical module path. | ||
| normalized = target.removesuffix(".") | ||
| if normalized and not _TARGET.fullmatch(normalized): | ||
| msg = f"option '{_OPTION_NAME}': target '{target}' must be a Python package path" | ||
| raise ValueError(msg) | ||
| compiled.append((_glob_to_regex(pattern), normalized)) | ||
| return tuple(compiled) | ||
|
|
||
|
|
||
| def map_import_target(proto_name: str, map_imports: MapImports) -> str | None: | ||
| for pattern, target in map_imports: | ||
| if pattern.fullmatch(proto_name): | ||
| return target | ||
| return None | ||
|
|
||
|
|
||
| def _glob_to_regex(pattern: str) -> re.Pattern[str]: | ||
| parts: list[str] = [] | ||
| i = 0 | ||
| while i < len(pattern): | ||
| char = pattern[i] | ||
| if char == "*": | ||
| if pattern[i + 1 : i + 2] == "*": | ||
| if pattern[i + 2 : i + 3] == "/": | ||
| parts.append(r"([^/]+/)*") | ||
| i += 3 | ||
| continue | ||
| parts.append(".*") | ||
| i += 2 | ||
| continue | ||
| parts.append(r"[^/]*") | ||
| elif char == "/" and i == len(pattern) - 1: | ||
| # A trailing slash matches everything in the directory. | ||
| parts.append("/.*") | ||
| else: | ||
| parts.append(re.escape(char)) | ||
| i += 1 | ||
| return re.compile("".join(parts)) | ||
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for not using https://docs.python.org/3/library/glob.html#glob.translate or https://docs.python.org/3/library/fnmatch.html#fnmatch.translate ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wish we could! But
glob.translateis from Python 3.13, several versions above our floor, andfnmatchis only single-level*support, not recursive**