Skip to content

Commit

Permalink
0.30.11a6. UPDATE: 更新AlistPath.stat,不再使用/api/fs/get 接口
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-cq committed Mar 5, 2024
1 parent 0d32f15 commit d16042a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
21 changes: 20 additions & 1 deletion alist_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,23 @@ class Client(
_SyncAdminMeta,
_SyncAdminTask,
):
pass
_cached_path_list = dict()

def dict_files_item(self, path: str | PurePosixPath, password="", refresh=False):
"""列出文件目录"""

def _dict_files(_path: str | PurePosixPath, _password="") -> dict[str, Item]:
_res = self.list_files(_path, _password, refresh=True)
if _res.code == 200:
_ = {d.name: d for d in _res.data.content or []}
self._cached_path_list[_path] = _
return _
return {}

path = str(path)
if refresh:
self._cached_path_list.pop(path, None)
if len(self._cached_path_list) >= 100:
self._cached_path_list.pop(0) # Python 3中的字典是按照插入顺序保存的

return self._cached_path_list.get(path, _dict_files(path, password))
12 changes: 9 additions & 3 deletions alist_sdk/path_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,20 @@ def raw_stat(self, force=False, retry=1, timeout=0.1) -> RawItem:
return self.raw_stat(force, retry - 1)
raise _e

def stat(self) -> RawItem | Item:
return getattr(self, "_stat", self.re_stat())
def stat(self) -> Item:
def _stat() -> Item:
_r = self.client.dict_files_item(self.parent.as_posix()).get(self.name)
if not _r:
raise FileNotFoundError(f"文件不存在: {self.as_posix()} ")
return _r

return getattr(self, "_stat", _stat())

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

def re_stat(self, retry=2, timeout=1) -> RawItem:
def re_stat(self, retry=2, timeout=1) -> Item:
if hasattr(self, "_stat"):
delattr(self, "_stat")
self.client.list_files(self.parent.as_posix(), per_page=1, refresh=True)
Expand Down
4 changes: 2 additions & 2 deletions alist_sdk/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
3. UPDATE: 更新AplistPath.__repl__方法.
4. UPDATE: 更新AlistPath.添加新的方法 set_stat,可以自定义设置stat属性,加快速度。
5. UPDATE: 更新AlistPath.iterdir,在迭代时添加stat数据,加快速度。
6. UPDATE: 更新AlistPath.stat,不再使用/api/fs/get 接口
"""

__version__ = "0.30.11a5"
__version__ = "0.30.11a6"

ALIST_VERSION = "v3.31.0"

0 comments on commit d16042a

Please sign in to comment.