mirror of
https://github.com/LmeSzinc/StarRailCopilot.git
synced 2024-11-28 10:45:19 +00:00
55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
import os
|
|
from typing import Callable, Generic, TypeVar
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class cached_property(Generic[T]):
|
|
"""
|
|
cached-property from https://github.com/pydanny/cached-property
|
|
Add typing support
|
|
|
|
A property that is only computed once per instance and then replaces itself
|
|
with an ordinary attribute. Deleting the attribute resets the property.
|
|
Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
|
|
"""
|
|
|
|
def __init__(self, func: Callable[..., T]):
|
|
self.func = func
|
|
|
|
def __get__(self, obj, cls) -> T:
|
|
if obj is None:
|
|
return self
|
|
|
|
value = obj.__dict__[self.func.__name__] = self.func(obj)
|
|
return value
|
|
|
|
|
|
def iter_folder(folder, is_dir=False, ext=None):
|
|
"""
|
|
Args:
|
|
folder (str):
|
|
is_dir (bool): True to iter directories only
|
|
ext (str): File extension, such as `.yaml`
|
|
|
|
Yields:
|
|
str: Absolute path of files
|
|
"""
|
|
try:
|
|
files = os.listdir(folder)
|
|
except FileNotFoundError:
|
|
return
|
|
|
|
for file in files:
|
|
sub = os.path.join(folder, file)
|
|
if is_dir:
|
|
if os.path.isdir(sub):
|
|
yield sub.replace('\\\\', '/').replace('\\', '/')
|
|
elif ext is not None:
|
|
if not os.path.isdir(sub):
|
|
_, extension = os.path.splitext(file)
|
|
if extension == ext:
|
|
yield os.path.join(folder, file).replace('\\\\', '/').replace('\\', '/')
|
|
else:
|
|
yield os.path.join(folder, file).replace('\\\\', '/').replace('\\', '/')
|