Change seek_full_path_from_defaults to find_file, and make it a more general function which will find and download urls if needed. This means for images/textures/etc. one can point online

This commit is contained in:
Grant Sanderson
2021-01-06 10:39:34 -08:00
parent 065df65cf1
commit 62bb40a7df
7 changed files with 54 additions and 29 deletions

View File

@ -1,6 +1,10 @@
import os
import numpy as np
import validators
import urllib.request
import tempfile
def add_extension_if_not_present(file_name, extension):
# This could conceivably be smarter about handling existing differing extensions
@ -16,17 +20,34 @@ def guarantee_existence(path):
return os.path.abspath(path)
def seek_full_path_from_defaults(file_name, directories, extensions):
possible_paths = [file_name]
possible_paths += [
def find_file(file_name, directories=None, extensions=None):
# Check if this is a file online first, and if so, download
# it to a temporary directory
if validators.url(file_name):
stem, name = os.path.split(file_name)
folder = guarantee_existence(
os.path.join(tempfile.gettempdir(), "manim_downloads")
)
path = os.path.join(folder, name)
urllib.request.urlretrieve(file_name, path)
return path
# Check if what was passed in is already a valid path to a file
if os.path.exists(file_name):
return file_name
# Otherwise look in local file system
directories = directories or [""]
extensions = extensions or [""]
possible_paths = (
os.path.join(directory, file_name + extension)
for directory in directories
for extension in ["", *extensions]
]
for extension in extensions
)
for path in possible_paths:
if os.path.exists(path):
return path
raise IOError("File {} not Found".format(file_name))
raise IOError(f"{file_name} not Found")
def get_sorted_integer_files(directory,