-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
重构checker init_alist支持多平台 完成CopyJob创建
- Loading branch information
Showing
6 changed files
with
174 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,63 @@ | ||
from pathlib import PurePosixPath | ||
from pydantic import BaseModel | ||
|
||
from alist_sdk import Item | ||
from alist_sync.models import SyncDir | ||
|
||
|
||
class Checker: | ||
"""检查结果""" | ||
class Checker(BaseModel): | ||
# matrix: 相对文件路径 -> {同步目录: Item} | ||
matrix: dict[PurePosixPath, dict[PurePosixPath, Item]] | ||
# cols: 同步目录 - base_path | ||
cols: list[PurePosixPath] | ||
|
||
def __init__(self, *scanned_dirs: SyncDir): | ||
self._result: dict[str, dict[str, Item]] = dict() | ||
self.table_title = [t.base_path for t in scanned_dirs] | ||
@classmethod | ||
def checker(cls, *scanned_dirs): | ||
_result = {} | ||
for scanned_dir in scanned_dirs: | ||
self.check(scanned_dir) | ||
|
||
@property | ||
def result(self): | ||
return self._result | ||
|
||
def check(self, scanned_dir: SyncDir): | ||
for item in scanned_dir.items: | ||
r_path = item.full_name.relative_to( | ||
scanned_dir.base_path | ||
) | ||
try: | ||
self._result[str(r_path)].setdefault(scanned_dir.base_path, item) | ||
except KeyError: | ||
self._result[str(r_path)] = {scanned_dir.base_path: item} | ||
|
||
def rich_table(self): | ||
"""Rich的报表 """ | ||
for item in scanned_dir.items: | ||
r_path = item.full_name.relative_to( | ||
scanned_dir.base_path | ||
) | ||
try: | ||
_result[PurePosixPath(r_path)].setdefault( | ||
PurePosixPath(scanned_dir.base_path), | ||
item | ||
) | ||
except KeyError: | ||
_result[PurePosixPath(r_path)] = { | ||
PurePosixPath(scanned_dir.base_path): item | ||
} | ||
|
||
return cls( | ||
matrix=_result, | ||
cols=[PurePosixPath(t.base_path) for t in scanned_dirs] | ||
) | ||
|
||
def model_dump_table(self): | ||
"""""" | ||
from rich.console import Console | ||
from rich.table import Table | ||
|
||
console = Console() | ||
table = Table(show_header=True, header_style="bold magenta") | ||
table.add_column('r_path', style="dim red", width=120) | ||
for col in self.table_title: | ||
table.add_column(col, justify="center", vertical='middle') | ||
table.add_column('r_path', style="dim red", ) | ||
for col in self.cols: | ||
table.add_column(str(col), justify="center", vertical='middle') | ||
|
||
for r_path, raw in self._result.items(): | ||
for r_path, raw in self.matrix.items(): | ||
table.add_row( | ||
r_path, | ||
*[True if raw.get(tt) else "False" for tt in self.table_title] | ||
str(r_path), | ||
*["True" if raw.get(tt) else "False" for tt in self.cols] | ||
) | ||
console.print(table) | ||
|
||
|
||
if __name__ == '__main__': | ||
import json | ||
from pathlib import Path | ||
from alist_sync.models import SyncDir | ||
|
||
checker = Checker(*[SyncDir(**s) for s in json.load( | ||
Path(__file__).parent.parent.joinpath('tests/resource/SyncDirs.json').open()) | ||
]) | ||
checker.rich_table() | ||
checker = Checker.checker(*[SyncDir(**s) for s in json.load( | ||
Path(__file__).parent.parent.joinpath('tests/resource/SyncDirs-m.json').open()) | ||
]) | ||
checker.model_dump_table() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.