forked from andreoliwa/nitpick
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read style from Python package (thanks to @isac322) (andreoliwa…
…#407) * build: add importlib-resources as conditional dependency * feat: implement Python package fetcher * docs: style inside Python package * test: long Python package prefix Co-authored-by: W. Augusto Andreoli <andreoliwa@gmail.com>
- Loading branch information
1 parent
dc2794b
commit 0a3c95d
Showing
12 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
nitpick.style.fetchers.pypackage module | ||
======================================= | ||
|
||
.. automodule:: nitpick.style.fetchers.pypackage | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Support for ``py`` schemes.""" | ||
from dataclasses import dataclass | ||
from itertools import chain | ||
from typing import Tuple | ||
from urllib.parse import urlparse | ||
|
||
from nitpick.style.fetchers.base import StyleFetcher | ||
|
||
try: | ||
from importlib.abc import Traversable # type: ignore[attr-defined] | ||
from importlib.resources import files # type: ignore[attr-defined] | ||
except ImportError: | ||
from importlib_resources import files | ||
from importlib_resources.abc import Traversable | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class PythonPackageURL: | ||
"""Represent a resource file in installed Python package.""" | ||
|
||
import_path: str | ||
resource_name: str | ||
|
||
@classmethod | ||
def parse_url(cls, url: str) -> "PythonPackageURL": | ||
"""Create an instance by parsing a URL string in any accepted format. | ||
See the code for ``test_parsing_python_package_urls()`` for more examples. | ||
""" | ||
parsed_url = urlparse(url) | ||
package_name = parsed_url.netloc | ||
resource_path = parsed_url.path.strip("/").split("/") | ||
|
||
import_path = ".".join(chain((package_name,), resource_path[:-1])) | ||
resource_name = resource_path[-1] | ||
|
||
return cls(import_path=import_path, resource_name=resource_name) | ||
|
||
@property | ||
def raw_content_url(self) -> Traversable: | ||
"""Raw path of resource file.""" | ||
return files(self.import_path).joinpath(self.resource_name) | ||
|
||
|
||
@dataclass(repr=True, unsafe_hash=True) | ||
class PythonPackageFetcher(StyleFetcher): # pylint: disable=too-few-public-methods | ||
""" | ||
Fetch a style from an installed Python package. | ||
URL schemes: | ||
- ``py://import/path/of/style/file/<style_file_name>`` | ||
- ``pypackage://import/path/of/style/file/<style_file_name>`` | ||
E.g. ``py://some_package/path/nitpick.toml``. | ||
""" | ||
|
||
protocols: Tuple[str, ...] = ("py", "pypackage") | ||
|
||
def _do_fetch(self, url): | ||
package_url = PythonPackageURL.parse_url(url) | ||
return package_url.raw_content_url.read_text() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""To test ``PythonPackageURL``.""" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""To test ``PythonPackageURL``.""" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters