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