def get_style_path(self, style_uri: str) -> Optional[Path]: """Get the style path from the URI. Add the .toml extension if it's missing.""" clean_style_uri = style_uri.strip() style_path = None 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 Nitpick.current_app().offline: # No style will be fetched in offline mode return None if self._first_full_path and not is_url(url): prefix, rest = self._first_full_path.split(":/") domain_plus_url = str(rest).strip("/").rstrip("/") + "/" + url new_url = "{}://{}".format(prefix, domain_plus_url) else: new_url = url parsed_url = list(urlparse(new_url)) if not parsed_url[2].endswith(TOML_EXTENSION): parsed_url[2] += TOML_EXTENSION new_url = urlunparse(parsed_url) if new_url in self._already_included: return None if not Nitpick.current_app().cache_dir: raise FileNotFoundError("Cache dir does not exist") try: response = requests.get(new_url) except requests.ConnectionError: click.secho( "Your network is unreachable. Fix your connection or use {} / {}=1" .format(Nitpick.format_flag(Nitpick.Flags.OFFLINE), Nitpick.format_env(Nitpick.Flags.OFFLINE)), fg="red", err=True, ) return None if not response.ok: raise FileNotFoundError("Error {} fetching style URL {}".format( response, new_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 = new_url.rsplit("/", 1)[0] contents = response.text style_path = Nitpick.current_app().cache_dir / "{}.toml".format( slugify(new_url)) Nitpick.current_app().cache_dir.mkdir(parents=True, exist_ok=True) style_path.write_text(contents) LOGGER.info("Loading style from URL %s into %s", new_url, style_path) self._already_included.add(new_url) 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(":/") domain_plus_url = Path(rest) / url try: resolved = domain_plus_url.resolve() except FileNotFoundError: resolved = domain_plus_url.absolute() new_url = "{}:/{}".format(prefix, resolved) else: new_url = url parsed_url = list(urlparse(new_url)) if not parsed_url[2].endswith(TOML_EXTENSION): parsed_url[2] += TOML_EXTENSION new_url = urlunparse(parsed_url) if new_url in self._already_included: return None if not Nitpick.current_app().cache_dir: raise FileNotFoundError("Cache dir does not exist") response = requests.get(new_url) if not response.ok: raise FileNotFoundError("Error {} fetching style URL {}".format( response, new_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 = new_url.rsplit("/", 1)[0] contents = response.text style_path = Nitpick.current_app().cache_dir / "{}.toml".format( slugify(new_url)) Nitpick.current_app().cache_dir.mkdir(parents=True, exist_ok=True) style_path.write_text(contents) LOGGER.info("Loading style from URL %s into %s", new_url, style_path) self._already_included.add(new_url) return style_path