87 lines
2.2 KiB
Python
87 lines
2.2 KiB
Python
from mimetypes import guess_type
|
|
from os import walk
|
|
|
|
|
|
class ListGenerator:
|
|
def __init__(self, path: str = None):
|
|
self.path = path
|
|
self.list: list = self._lister(self.path)
|
|
try:
|
|
self._size = len(self.list)
|
|
except TypeError:
|
|
pass
|
|
self._current_index = 0
|
|
|
|
def __str__(self) -> str:
|
|
return 'Audio iter generator'
|
|
|
|
@classmethod
|
|
def _lister(cls, path) -> list:
|
|
_list: list = []
|
|
try:
|
|
for f in walk(path):
|
|
_list.extend(f)
|
|
break
|
|
return sorted(_list[2])
|
|
except TypeError:
|
|
pass
|
|
|
|
def __iter__(self):
|
|
return _ListGenerationIter(self)
|
|
|
|
def __next__(self):
|
|
if self._current_index < self._size:
|
|
self._current_index += 1
|
|
|
|
|
|
class _FileAttrs:
|
|
def __init__(self, name, path, mimetype, exc):
|
|
self.name = name
|
|
self.path = path
|
|
self.mimetype = mimetype
|
|
self.exc = exc
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def __repr__(self):
|
|
return f'<File attrs name={self.name} path={self.path} exc={self.exc} type=<{self.mimetype}> >'
|
|
|
|
|
|
class _ListGenerationIter:
|
|
|
|
def __init__(self, list_class):
|
|
self._current_index = 0
|
|
self._list = list_class.list
|
|
self._name = self._list[self._current_index]
|
|
self._path = list_class.path
|
|
|
|
self._size = len(self._list)
|
|
|
|
def __iter__(self):
|
|
return self
|
|
|
|
@property
|
|
def type(self) -> str:
|
|
guess = guess_type(f'{self._path}/{self._list[self._current_index]}')[0]
|
|
return guess
|
|
|
|
@property
|
|
def _exc(self) -> str:
|
|
try:
|
|
_ret = self._list[self._current_index].split('.')[-1]
|
|
except AttributeError:
|
|
_ret = None
|
|
return _ret
|
|
|
|
def __next__(self):
|
|
if self._current_index < self._size:
|
|
_name = self._list[self._current_index]
|
|
_path = self._path
|
|
_type = self.type
|
|
_exc = self._exc
|
|
self._current_index += 1
|
|
_memb = _FileAttrs(_name, _path, _type, _exc)
|
|
return _memb
|
|
raise StopIteration
|