Add OSGetter implementation of DirGetter
- init dirgetter.getter - add OSGetter class
This commit is contained in:
parent
0b48cb152e
commit
b00c60f448
|
@ -11,7 +11,9 @@ from tubot.dirgetter.types import GetterTypes
|
|||
|
||||
|
||||
class DirGetter(IValidatable, ABC):
|
||||
|
||||
"""
|
||||
DirectoryGetter Abstract class
|
||||
"""
|
||||
_gtype: GetterTypes
|
||||
|
||||
def __init__(self) -> None:
|
||||
|
|
|
@ -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)
|
Reference in New Issue