4
1
Fork 1

Add OSGetter implementation of DirGetter

- init dirgetter.getter
- add OSGetter class
This commit is contained in:
trueold89 2024-08-05 14:56:18 +03:00
parent 0b48cb152e
commit b00c60f448
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
2 changed files with 43 additions and 1 deletions

View File

@ -11,7 +11,9 @@ from tubot.dirgetter.types import GetterTypes
class DirGetter(IValidatable, ABC): class DirGetter(IValidatable, ABC):
"""
DirectoryGetter Abstract class
"""
_gtype: GetterTypes _gtype: GetterTypes
def __init__(self) -> None: def __init__(self) -> None:

40
tubot/dirgetter/getter.py Normal file
View File

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
####################################
# Directory-Getter implementations #
####################################
# Imports
from tubot.dirgetter.types import GetterTypes
from tubot.dirgetter.abc import DirGetter
from aiofiles.os import listdir
from aiofiles.ospath import isdir
class OSGetter(DirGetter):
"""
Python.os module implementation of DirectoryGetter
"""
_gtype = GetterTypes.OS
base_dir: str
def __init__(self, base_dir: str) -> None:
"""
:param base_dir: Path to parent directory
"""
super().__init__()
self.base_dir = base_dir
@property
async def folders(self) -> dict:
dirs = {}
ls = await listdir(self.base_dir)
if len(ls) == 0:
raise ValueError("No dirs found")
for item in ls:
if await isdir(f"{self.base_dir}/{item}"):
dirs[item] = f"{self.base_dir}/{item}"
return dirs
async def __validate__(self) -> bool:
return await isdir(self.base_dir)