Skip to content

Commit

Permalink
Suppress exception if download fails and file is present (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
BjoernAtBosch authored Aug 29, 2024
1 parent 7ce1d82 commit 6085c1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- [`velocitas_lib.get_script_path`](./velocitas_lib.md#function-get_script_path): Return the absolute path to the directory the invoked Python script
- [`velocitas_lib.get_valid_arch`](./velocitas_lib.md#function-get_valid_arch): Return a known architecture for the given `arch`.
- [`velocitas_lib.get_workspace_dir`](./velocitas_lib.md#function-get_workspace_dir): Return the workspace directory.
- [`velocitas_lib.have_internet_connection`](./velocitas_lib.md#function-have_internet_connection)
- [`velocitas_lib.is_uri`](./velocitas_lib.md#function-is_uri): Check if the provided path is a URI.
- [`velocitas_lib.obtain_local_file_path`](./velocitas_lib.md#function-obtain_local_file_path): Return the absolute path to the file, specified by a absolute/relative local path or with an URI.
- [`velocitas_lib.require_env`](./velocitas_lib.md#function-require_env): Require and return an environment variable.
Expand Down
19 changes: 17 additions & 2 deletions docs/velocitas_lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ Check if the provided path is a URI.

<a href="../velocitas_lib/__init__.py#L160"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `have_internet_connection`

```python
have_internet_connection() → bool
```






---

<a href="../velocitas_lib/__init__.py#L168"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `obtain_local_file_path`

```python
Expand Down Expand Up @@ -286,7 +301,7 @@ Return the absolute path to the file, specified by a absolute/relative local pat

---

<a href="../velocitas_lib/__init__.py#L195"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L203"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `extract_zip`

Expand Down Expand Up @@ -317,7 +332,7 @@ Extract a zip file.

---

<a href="../velocitas_lib/__init__.py#L217"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../velocitas_lib/__init__.py#L225"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `discover_files_in_filetree`

Expand Down
22 changes: 15 additions & 7 deletions velocitas_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import json
import os
import sys
import re
import sys
import zipfile
from io import TextIOWrapper
from typing import Any, Dict, List, Optional
Expand Down Expand Up @@ -157,6 +157,14 @@ def is_uri(path: str) -> bool:
return re.match(r"(\w+)\:\/\/(\w+)", path) is not None


def have_internet_connection() -> bool:
try:
requests.head("http://www.google.com/", timeout=5)
return True
except requests.ConnectionError:
return False


def obtain_local_file_path(
path_or_uri: str, download_path: Optional[str] = None
) -> str:
Expand All @@ -181,14 +189,14 @@ def obtain_local_file_path(
download_path = os.path.join(
get_project_cache_dir(), "downloads", path_or_uri.split("/")[-1]
)
if os.path.isfile(download_path):
path, file = os.path.split(download_path)
parts = file.split(".", 1)
filename = f"{parts[0]}_1.{parts[1]}" if len(parts) > 1 else f"{parts[0]}_1"

download_path = os.path.join(path, filename)
try:
download_file(path_or_uri, download_path)
except requests.ConnectionError:
if have_internet_connection() or not os.path.exists(download_path):
raise
print(f"[WARING] No internet connection -> using cached file {download_path}")

download_file(path_or_uri, download_path)
return download_path


Expand Down

0 comments on commit 6085c1b

Please sign in to comment.