2022-02-12 23:47:23 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-04-29 10:52:48 -07:00
|
|
|
import os
|
2022-02-12 23:47:23 +08:00
|
|
|
from typing import Iterable
|
|
|
|
|
2019-01-10 17:06:22 -08:00
|
|
|
import numpy as np
|
2021-01-06 10:39:34 -08:00
|
|
|
import validators
|
|
|
|
|
2018-04-29 10:52:48 -07:00
|
|
|
|
2022-02-12 23:47:23 +08:00
|
|
|
def add_extension_if_not_present(file_name: str, extension: str) -> str:
|
2018-04-29 10:52:48 -07:00
|
|
|
# This could conceivably be smarter about handling existing differing extensions
|
|
|
|
if(file_name[-len(extension):] != extension):
|
|
|
|
return file_name + extension
|
|
|
|
else:
|
|
|
|
return file_name
|
|
|
|
|
|
|
|
|
2022-02-12 23:47:23 +08:00
|
|
|
def guarantee_existence(path: str) -> str:
|
2018-05-01 01:53:59 -07:00
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
2019-01-13 23:29:46 +08:00
|
|
|
return os.path.abspath(path)
|
2018-05-01 01:53:59 -07:00
|
|
|
|
|
|
|
|
2022-02-12 23:47:23 +08:00
|
|
|
def find_file(
|
|
|
|
file_name: str,
|
|
|
|
directories: Iterable[str] | None = None,
|
|
|
|
extensions: Iterable[str] | None = None
|
|
|
|
) -> str:
|
2021-01-06 10:39:34 -08:00
|
|
|
# Check if this is a file online first, and if so, download
|
|
|
|
# it to a temporary directory
|
|
|
|
if validators.url(file_name):
|
2021-02-11 12:21:06 -08:00
|
|
|
import urllib.request
|
2021-02-03 00:04:30 +05:30
|
|
|
from manimlib.utils.directories import get_downloads_dir
|
2021-01-06 10:39:34 -08:00
|
|
|
stem, name = os.path.split(file_name)
|
2021-02-03 00:04:30 +05:30
|
|
|
folder = get_downloads_dir()
|
2021-01-06 10:39:34 -08:00
|
|
|
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 = (
|
2021-01-05 23:14:16 -08:00
|
|
|
os.path.join(directory, file_name + extension)
|
|
|
|
for directory in directories
|
2021-01-06 10:39:34 -08:00
|
|
|
for extension in extensions
|
|
|
|
)
|
2019-01-24 22:24:01 -08:00
|
|
|
for path in possible_paths:
|
|
|
|
if os.path.exists(path):
|
|
|
|
return path
|
2021-01-06 10:39:34 -08:00
|
|
|
raise IOError(f"{file_name} not Found")
|
2019-01-10 17:06:22 -08:00
|
|
|
|
|
|
|
|
2022-02-12 23:47:23 +08:00
|
|
|
def get_sorted_integer_files(
|
|
|
|
directory: str,
|
|
|
|
min_index: float = 0,
|
|
|
|
max_index: float = np.inf,
|
|
|
|
remove_non_integer_files: bool = False,
|
|
|
|
remove_indices_greater_than: float | None = None,
|
|
|
|
extension: str | None = None,
|
|
|
|
) -> list[str]:
|
2019-01-10 17:06:22 -08:00
|
|
|
indexed_files = []
|
|
|
|
for file in os.listdir(directory):
|
|
|
|
if '.' in file:
|
|
|
|
index_str = file[:file.index('.')]
|
|
|
|
else:
|
|
|
|
index_str = file
|
|
|
|
|
|
|
|
full_path = os.path.join(directory, file)
|
|
|
|
if index_str.isdigit():
|
|
|
|
index = int(index_str)
|
|
|
|
if remove_indices_greater_than is not None:
|
|
|
|
if index > remove_indices_greater_than:
|
|
|
|
os.remove(full_path)
|
|
|
|
continue
|
2019-01-10 17:29:16 -08:00
|
|
|
if extension is not None and not file.endswith(extension):
|
|
|
|
continue
|
2019-01-10 17:06:22 -08:00
|
|
|
if index >= min_index and index < max_index:
|
|
|
|
indexed_files.append((index, file))
|
|
|
|
elif remove_non_integer_files:
|
|
|
|
os.remove(full_path)
|
|
|
|
indexed_files.sort(key=lambda p: p[0])
|
2019-01-13 23:29:46 +08:00
|
|
|
return list(map(lambda p: os.path.join(directory, p[1]), indexed_files))
|