Skip to content

Commit

Permalink
0.30.11:
Browse files Browse the repository at this point in the history
    4. UPDATE: 更新AlistPath.添加新的方法 set_stat,可以自定义设置stat属性,加快速度。
    5. UPDATE: 更新AlistPath.iterdir,在迭代时添加stat数据,加快速度。
  • Loading branch information
lee-cq committed Mar 4, 2024
1 parent 117ffbf commit 0d32f15
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
13 changes: 2 additions & 11 deletions alist_sdk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,13 @@ class Item(BaseModel):
type: int # 类型


class RawItem(BaseModel):
class RawItem(Item):
"""一个对象的全部信息"""

name: str
size: int
is_dir: bool
hashinfo: Optional[str] = "null" # v3.29.0
hash_info: Optional[HashInfo | None] = None # v3.29.0
modified: datetime.datetime
sign: str
thumb: str
type: int
raw_url: str = ""
readme: str = ""
provider: str = ""
related: str | None | list['RawItem'] = None
related: str | None | list["RawItem"] = None


class SearchItem(BaseModel):
Expand Down
30 changes: 20 additions & 10 deletions alist_sdk/path_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import core_schema

from alist_sdk import alistpath
from alist_sdk import alistpath, Item, AlistError, RawItem
from alist_sdk.py312_pathlib import PurePosixPath
from alist_sdk import AlistError, RawItem
from alist_sdk.client import Client


Expand Down Expand Up @@ -143,13 +142,13 @@ def get_download_uri(self) -> str:
raise ValueError("relative path can't be expressed as a file URI")
if self.is_dir():
raise IsADirectoryError()
return self.stat().raw_url
return self.raw_stat().raw_url

def as_download_uri(self):
return self.get_download_uri()

@lru_cache()
def _stat(self) -> RawItem:
def _raw_stat(self) -> RawItem:
_raw = self.client.get_item_info(self.as_posix())
if _raw.code == 200:
data = _raw.data
Expand All @@ -160,20 +159,29 @@ def _stat(self) -> RawItem:
raise FileNotFoundError(_raw.message)
raise AlistError(_raw.message)

def stat(self, force=False, retry=1, timeout=0.1) -> RawItem:
def raw_stat(self, force=False, retry=1, timeout=0.1) -> RawItem:
if force:
self._stat.cache_clear()
self._raw_stat.cache_clear()
try:
return self._stat()
return self._raw_stat()
except FileNotFoundError as _e:
if retry > 0:
time.sleep(timeout)
return self.stat(force, retry - 1)
return self.raw_stat(force, retry - 1)
raise _e

def stat(self) -> RawItem | Item:
return getattr(self, "_stat", self.re_stat())

def set_stat(self, value: RawItem | Item):
# noinspection PyAttributeOutsideInit
self._stat = value

def re_stat(self, retry=2, timeout=1) -> RawItem:
if hasattr(self, "_stat"):
delattr(self, "_stat")
self.client.list_files(self.parent.as_posix(), per_page=1, refresh=True)
return self.stat(force=True, retry=retry, timeout=timeout)
return self.raw_stat(force=True, retry=retry, timeout=timeout)

def is_dir(self):
""""""
Expand Down Expand Up @@ -201,7 +209,9 @@ def iterdir(self) -> Iterator["AlistPath"]:
for item in (
self.client.list_files(self.as_posix(), refresh=True).data.content or []
):
yield self.joinpath(item.name)
_ = self.joinpath(item.name)
_.set_stat(item)
yield _

def read_text(self):
""""""
Expand Down
7 changes: 5 additions & 2 deletions alist_sdk/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@
0.30.11:
1. BUGFIX: 从其他站点导入配置时,数据模型错误。
2. BUGFIX: http://localhost:5244 无法正常识别
3. UPDATE: 更新AplistPath.__repl__方法
3. UPDATE: 更新AplistPath.__repl__方法.
4. UPDATE: 更新AlistPath.添加新的方法 set_stat,可以自定义设置stat属性,加快速度。
5. UPDATE: 更新AlistPath.iterdir,在迭代时添加stat数据,加快速度。
"""

__version__ = "0.30.11a3"
__version__ = "0.30.11a5"

ALIST_VERSION = "v3.31.0"

0 comments on commit 0d32f15

Please sign in to comment.