forked from clan/clan-core
api: list files
This commit is contained in:
parent
bf7b148592
commit
337ba1f8f6
@ -6,6 +6,11 @@ from pathlib import Path
|
|||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
# These imports are unused, but necessary for @API.register to run once.
|
||||||
|
from clan_cli.api import directory
|
||||||
|
|
||||||
|
__all__ = ["directory"]
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
backups,
|
backups,
|
||||||
config,
|
config,
|
||||||
|
56
pkgs/clan-cli/clan_cli/api/directory.py
Normal file
56
pkgs/clan-cli/clan_cli/api/directory.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import os
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
from clan_cli.errors import ClanError
|
||||||
|
|
||||||
|
from . import API
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class File:
|
||||||
|
path: str
|
||||||
|
file_type: Literal["file", "directory", "symlink"]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Directory:
|
||||||
|
path: str
|
||||||
|
files: list[File] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
@API.register
|
||||||
|
def get_directory(current_path: str) -> Directory:
|
||||||
|
curr_dir = Path(current_path)
|
||||||
|
directory = Directory(path=str(curr_dir))
|
||||||
|
|
||||||
|
if not curr_dir.is_dir():
|
||||||
|
raise ClanError()
|
||||||
|
|
||||||
|
with os.scandir(curr_dir.resolve()) as it:
|
||||||
|
for entry in it:
|
||||||
|
if entry.is_symlink():
|
||||||
|
directory.files.append(
|
||||||
|
File(
|
||||||
|
path=str(curr_dir / Path(entry.name)),
|
||||||
|
file_type="symlink",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
elif entry.is_file():
|
||||||
|
directory.files.append(
|
||||||
|
File(
|
||||||
|
path=str(curr_dir / Path(entry.name)),
|
||||||
|
file_type="file",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
elif entry.is_dir():
|
||||||
|
directory.files.append(
|
||||||
|
File(
|
||||||
|
path=str(curr_dir / Path(entry.name)),
|
||||||
|
file_type="directory",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return directory
|
Loading…
Reference in New Issue
Block a user