[generator] download_file() now allows URLs with "file" protocol #3382

Merged
mrater merged 6 commits from test6 into master 2022-09-13 21:30:54 +00:00
2 changed files with 22 additions and 0 deletions

View file

@ -13,6 +13,7 @@ from typing import Optional
from urllib.parse import unquote
from urllib.parse import urljoin
from urllib.parse import urlparse
from urllib.request import url2pathname
import requests
from bs4 import BeautifulSoup
@ -24,6 +25,20 @@ from maps_generator.utils.md5 import md5_ext
logger = logging.getLogger("maps_generator")
def is_file_uri(url: AnyStr) -> bool:
return urlparse(url).scheme == "file"
def file_uri_to_path(url : AnyStr) -> AnyStr:
file_uri = urlparse(url)
file_path = file_uri.path
# URI is something like "file://~/..."
if file_uri.netloc == '~':
file_path = f'~{file_uri.path}'
return os.path.expanduser(file_path)
return file_path
def is_executable(fpath: AnyStr) -> bool:
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
@ -48,6 +63,12 @@ def download_file(url: AnyStr, name: AnyStr, download_if_exists: bool = True):
logger.info(f"File {name} already exists.")
return
if is_file_uri(url):
# url uses 'file://' scheme
copy_overwrite(file_uri_to_path(url), name)
logger.info(f"File {name} was copied from {url}.")
return
tmp_name = f"{name}__"
os.makedirs(os.path.dirname(tmp_name), exist_ok=True)
with requests.Session() as session:

View file

@ -46,6 +46,7 @@ DIFF_VERSION_DEPTH: 2
[External]
# Note: If you want to set a directory name you have to add "/" to the end of url.
# In each field where you need to specify a URL, you can specify the path to the file system using file:///path/to/file
# The url to the planet file.
# PLANET_URL: