Skip to content

Commit

Permalink
feat: read styles from relative paths on URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Mar 10, 2019
1 parent 55634e1 commit 46d1b84
Showing 6 changed files with 410 additions and 295 deletions.
13 changes: 13 additions & 0 deletions flake8_nitpick/generic.py
Original file line number Diff line number Diff line change
@@ -156,3 +156,16 @@ def version_to_tuple(version: str = None) -> Tuple[int, ...]:
if not clean_version:
return ()
return tuple(int(part) for part in clean_version.split("."))


def is_url(url: str) -> bool:
"""Return True if a string is a URL.
>>> is_url("")
False
>>> is_url(" ")
False
>>> is_url("http://example.com")
True
"""
return url.startswith("http")
17 changes: 13 additions & 4 deletions flake8_nitpick/style.py
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
UNIQUE_SEPARATOR,
)
from flake8_nitpick.files.pyproject_toml import PyProjectTomlFile
from flake8_nitpick.generic import climb_directory_tree, flatten, search_dict, unflatten
from flake8_nitpick.generic import climb_directory_tree, flatten, is_url, search_dict, unflatten
from flake8_nitpick.types import JsonDict, StrOrList

if TYPE_CHECKING:
@@ -31,7 +31,7 @@ def __init__(self, config: "NitpickConfig") -> None:
self.config = config
self._all_flattened: JsonDict = {}
self._already_included: Set[str] = set()
self._first_full_path: Optional[Path] = None
self._first_full_path: str = ""

def find_initial_styles(self, configured_styles: StrOrList):
"""Find the initial style(s) and include them."""
@@ -70,14 +70,19 @@ def get_style_path(self, style_uri: str) -> Optional[Path]:
"""Get the style path from the URI."""
clean_style_uri = style_uri.strip()
style_path = None
if clean_style_uri.startswith("http"):
if is_url(clean_style_uri) or is_url(self._first_full_path):
style_path = self.fetch_style_from_url(clean_style_uri)
elif clean_style_uri:
style_path = self.fetch_style_from_local_path(clean_style_uri)
return style_path

def fetch_style_from_url(self, url: str) -> Optional[Path]:
"""Fetch a style file from a URL, saving the contents in the cache dir."""
if self._first_full_path and not is_url(url):
prefix, rest = self._first_full_path.split(":/")
resolved = (Path(rest) / url).resolve()
url = f"{prefix}:/{resolved}"

if url in self._already_included:
return None

@@ -88,6 +93,10 @@ def fetch_style_from_url(self, url: str) -> Optional[Path]:
if not response.ok:
raise FileNotFoundError(f"Error {response} fetching style URL {url}")

# Save the first full path to be used by the next files without parent.
if not self._first_full_path:
self._first_full_path = url.rsplit("/", 1)[0]

contents = response.text
style_path = self.config.cache_dir / f"{slugify(url)}.toml"
self.config.cache_dir.mkdir(parents=True, exist_ok=True)
@@ -111,7 +120,7 @@ def fetch_style_from_local_path(self, partial_file_name: str) -> Optional[Path]:

# Save the first full path to be used by the next files without parent.
if not self._first_full_path:
self._first_full_path = style_path.resolve().parent
self._first_full_path = str(style_path.resolve().parent)

if str(style_path) in self._already_included:
return None
27 changes: 20 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@ black = {version = "*", allows-prereleases = true}
pylint = "*"
mypy = "*"
pre-commit = {version = "*", allows-prereleases = true}
responses = "*"

[build-system]
requires = ["poetry>=0.12"]
Loading

0 comments on commit 46d1b84

Please sign in to comment.