Skip to content

Commit

Permalink
优化 checker
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-cq committed Dec 8, 2023
1 parent 61fd0db commit 89cc3a8
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 13 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "CLI - main copy",
"type": "python",
Expand Down Expand Up @@ -32,6 +33,22 @@
"console": "integratedTerminal",
"justMyCode": true,

},
{
"name": "debug - check",
"type": "python",
"request": "launch",
"program": "alist_sync/__main__.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"check",
"-u", "admin",
"-p", "123456",
"-t", "/local",
"-t", "/local_dst",
"-t", "/local_dst2"
]
}
]
}
18 changes: 11 additions & 7 deletions alist_sync/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
help="Verify SSL Certificates"
)

_stores_config: str = Option(
..., '-c', '--store-path',
_stores_config: str | None = Option(
None, '-c', '--store-path',
help="一个包含存储配置的JSON文件,可以是AList的备份文件"
)

Expand All @@ -63,13 +63,16 @@ def check(
password=password,
token=token,
verify=verify,
storage_config=storage_config,
storage_config=Path(storage_config) if storage_config else None,
)
echo(
f"Will Be Check {target} "
"on {alist_info.base_url} [{alist_info.username}]"
f"Will Be Check '{target} "
f"on {alist_info.base_url} [{alist_info.username}] \n"
f"将会从 {alist_info.storage_config} 存储库获取存储库信息。"
)
return SyncBase(alist_info=alist_info, sync_dirs=target).sync_job.checker.model_dump_table()
c = SyncBase(alist_info=alist_info, sync_dirs=target)
c.run()
c.sync_job.checker.model_dump_table()


@app.command(name="copy")
Expand All @@ -94,7 +97,8 @@ def copy(
)
echo(
f"Will Be Copy '{source}' -> {target} "
"on {alist_info.base_url} [{alist_info.username}]"
f"on {alist_info.base_url} [{alist_info.username}]"
f"将会从 {alist_info.storage_config} 存储库获取存储库信息。"
)
return CopyToTarget(alist_info, source_path=source, targets_path=target).run()

Expand Down
8 changes: 6 additions & 2 deletions alist_sync/base_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def __init__(self,
sync_dirs: list[str | os.PathLike]
):
self.client = AlistClient(
timeout=30, **alist_info.model_dump(exclude={'storage_config'})
timeout=30, **alist_info.model_dump(
exclude={'storage_config'}
)
)

self.sync_dirs = sync_dirs
Expand Down Expand Up @@ -103,7 +105,9 @@ async def async_run(self):
if not self.sync_job.sync_dirs.values():
await self.scans()
self.save_to_cache()
self.sync_job.checker = Checker.checker(*self.sync_job.sync_dirs.values())
self.sync_job.checker = Checker.checker(
*self.sync_job.sync_dirs.values()
)
else:
logger.info(f"一件从缓存中找到 %d 个 SyncDir",
len(self.sync_job.sync_dirs))
7 changes: 4 additions & 3 deletions alist_sync/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json
from pathlib import PurePosixPath, Path
from typing import Optional, Literal

Expand All @@ -23,7 +24,7 @@ class AlistServer(BaseModel):
has_opt: Optional[bool] = False

max_connect: int = 30 # 最大同时连接数
storage_config: Path = []
storage_config: Optional[Path] = None

# httpx 的参数
verify: Optional[bool] = True
Expand All @@ -39,7 +40,7 @@ def is_storage(_st):
return True
return False

if not self.storage_config:
if not self.storage_config or self.storage_config == Path():
return []
if not self.storage_config.exists():
raise FileNotFoundError(f"找不到文件:{self.storage_config}")
Expand Down Expand Up @@ -198,6 +199,6 @@ def model_dump_table(self):
from pathlib import Path

checker = Checker.checker(*[SyncDir(**s) for s in json.load(
Path(__file__).parent.parent.joinpath('tests/resource/SyncDirs.json').open())
Path(__file__).parent.parent.joinpath('tests/resource/SyncDirs-m.json').open())
])
checker.model_dump_table()
143 changes: 143 additions & 0 deletions tests/resource/SyncDirs-m.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
[
{
"base_path": "/local",
"items": [
{
"parent": "/local",
"name": "check.py",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
},
{
"parent": "/local/a",
"name": "a.txt",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
},
{
"parent": "/local/b",
"name": "b.txt",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
}
]
},
{
"base_path": "/local_dst",
"items": [
{
"parent": "/local_dst",
"name": "check.py",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
},
{
"parent": "/local_dst/a",
"name": "a.txt",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
},
{
"parent": "/local_dst/c",
"name": "c.txt",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
}
]
},
{
"base_path": "/local_dst2",
"items": [
{
"parent": "/local_dst2",
"name": "check.py",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
},
{
"parent": "/local_dst2/d",
"name": "d.txt",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
},
{
"parent": "/local_dst2/c",
"name": "c.txt",
"size": 540,
"is_dir": false,
"hashinfo": "null",
"hash_info": null,
"modified": "2023-12-02T06:10:17Z",
"created": "2023-12-02T06:10:17Z",
"sign": "",
"thumb": "https://japane/.Dh75k",
"type": 4,
"full_name": "/onedrive/Music/check.py"
}
]
}
]
7 changes: 6 additions & 1 deletion tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

@pytest.mark.parametrize(
"scanned_dirs",
[[SyncDir(**s) for s in json.load(SUP_DIR.joinpath('SyncDirs.json').open())]]
[
[SyncDir(**s)
for s in json.load(SUP_DIR.joinpath('SyncDirs.json').open())],
[SyncDir(**s)
for s in json.load(SUP_DIR.joinpath('SyncDirs-m.json').open())]
]
)
def test_check(scanned_dirs):
checker = Checker.checker(*scanned_dirs)
Expand Down

0 comments on commit 89cc3a8

Please sign in to comment.