Skip to content

Commit

Permalink
0.36.14:
Browse files Browse the repository at this point in the history
    1. Client API 现在增加属性获取服务端版本: client.server_version
    2. 异步客户端添加 client.login_username 属性。
    3. 移除为保持兼容的 alist_sdk.path_lib_old.py 文件
    4. 添加测试
  • Loading branch information
lee-cq committed Aug 18, 2024
1 parent a3b2e93 commit 8ae45f9
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 340 deletions.
2 changes: 2 additions & 0 deletions alist_sdk/alistpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

__all__ = [*posixpath.__all__, "splitroot"]

_ = sep # 解决IDE报错:未使用的导入


def splitroot(p):
"""Split a pathname into drive, root and tail. On Posix, drive is always
Expand Down
18 changes: 15 additions & 3 deletions alist_sdk/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import time
import urllib.parse
from functools import cached_property
from pathlib import Path, PurePosixPath

from httpx import AsyncClient as HttpClient, Response
Expand Down Expand Up @@ -55,9 +56,7 @@ async def verify_login_status(self) -> bool:
"""验证登陆状态,"""
me = (await self.get("/api/me")).json()
if me.get("code") != 200:
logger.error(
"异步客户端登陆失败[%d], %s", me.get("code"), me.get("message")
)
logger.error("异步客户端登陆失败[%d], %s", me.get("code"), me.get("message"))
return False

username = me["data"].get("username")
Expand Down Expand Up @@ -132,6 +131,10 @@ async def login(self, username, password, has_opt=False) -> bool:
logger.warning("登陆失败[%d]:%s", res.status_code, res.text)
return False

@cached_property
async def login_username(self):
return (await self.me()).data.username

# ================ FS 相关方法 =================


Expand Down Expand Up @@ -483,6 +486,15 @@ async def admin_setting_list(self, group: int = None):
query = {"group": group} if group else {}
return locals(), await self.get("/api/admin/setting/list", params=query)

@cached_property
async def service_version(self) -> tuple:
"""返回服务器版本元组 (int, int, int)"""
settings: list[Setting] = (await self.admin_setting_list(group=1)).data
for s in settings:
if s.key == "version":
return tuple(map(int, s.value.strip("v").split(".", 2)))
raise ValueError("无法获取服务端版本")


class AsyncClient(
_AsyncFS,
Expand Down
14 changes: 13 additions & 1 deletion alist_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ def get_item_info(self, path: str | PurePosixPath, password=None):
"/api/fs/get", json={"path": path, "password": password}
)

def fs_get(self, path: str | PurePosixPath, password=None):
return self.get_item_info(path, password)

@verify()
def search(
self,
Expand Down Expand Up @@ -500,6 +503,15 @@ def admin_setting_list(self, group: int = None):
query = {"group": group} if group else {}
return locals(), self.get("/api/admin/setting/list", params=query)

@cached_property
def service_version(self) -> tuple:
"""返回服务器版本元组 (int, int, int)"""
settings: list[Setting] = self.admin_setting_list(group=1).data
for s in settings:
if s.key == "version":
return tuple(map(int, s.value.strip("v").split(".", 2)))
raise ValueError("无法获取服务端版本")


class Client(
_SyncFs,
Expand Down Expand Up @@ -530,7 +542,7 @@ def dict_files_items(
logger.debug("缓存命中[times: %d]: %s", self._succeed_cache, path)
return self._cached_path_list[path]

if len(self._cached_path_list) >= 10000:
if len(self._cached_path_list) >= 1000:
self._cached_path_list.pop(0) # Python 3中的字典是按照插入顺序保存的

logger.debug("缓存未命中: %s", path)
Expand Down
2 changes: 1 addition & 1 deletion alist_sdk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class Setting(_BaseModel):
"""/api/admin/setting/list .data.[]"""

key: str
value: Any
value: str
help: str # 帮助信息
type: str # TODO 类型指导
options: str
Expand Down
11 changes: 9 additions & 2 deletions alist_sdk/path_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import core_schema

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

Expand Down Expand Up @@ -186,7 +188,12 @@ def raw_stat(self, retry=1, timeout=0.1) -> RawItem:

def stat(self) -> Item | RawItem:
def f_stat() -> Item | RawItem:
_r = self.client.dict_files_items(self.parent.as_posix()).get(self.name)
_r = (
self.client.get_item_info(self.as_posix()).data
if self.as_posix() == "/"
else self.client.dict_files_items(self.parent.as_posix()).get(self.name)
)

if not _r:
raise FileNotFoundError(f"文件不存在: {self.as_posix()} ")
self.set_stat(_r)
Expand Down
Loading

0 comments on commit 8ae45f9

Please sign in to comment.