fixed some issues with iter

This commit is contained in:
2022-09-01 19:43:59 +03:00
parent 3c0851102d
commit 646008e4d8
3 changed files with 68 additions and 63 deletions

View File

@@ -1,74 +1,84 @@
import sqlite3
import disnake
class User:
def __init__(self, userid: str = None):
self.userid = userid
self.list: list = self._lister(self.userid)
from lib import logger
class DB_Reader:
def __init__(self, guildid: int = None):
self.guildid = guildid
self.list = self._read_db(self.guildid)
self._current_index = 0
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return (
f'<Audio file name={self.name} path={self.userid} type={self.type}>'
)
@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
return str(self.guildid)
@classmethod
def _lister(cls, path) -> list:
_dict: list = []
for _files in os.walk(path):
_dict.extend(_files)
break
return _dict[2]
def _read_db(cls, guildid: int) -> list:
try:
sql_con = sqlite3.connect("user.db")
cursor = sql_con.cursor()
cursor.execute(f"""SELECT * FROM "{guildid}" """)
record = cursor.fetchall()
return record
except sqlite3.Error as _e:
logger.info(f'Error reading DB\n{_e}')
def __iter__(self):
return _ListGenerationIter(self)
class _FileAttrs:
def __init__(self, name, path, type):
self.name = name
self.path = path
self.type = type
class _DBAttrs:
def __init__(self,
userid,
username,
nick,
isbot,
defaulttracks,
usertracks):
self.userid = userid
self.username = username
self.nick = nick
self.isbot = isbot
self.defaulttracks = defaulttracks
self.usertracks = usertracks
def __str__(self):
return 'Audio Dict Generator'
return self.username
def __repr__(self):
return f'<File attrs name={self.name} path={self.path} type={self.path}>'
return f'<File attrs userid={self.userid} username={self.username} nick={self.nick} ' \
f'isbot={self.isbot} defaulttracks={self.defaulttracks} usertracks={self.usertracks}>'
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
def __init__(self, user_class):
self._current_index = 0
self._list = user_class.list
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
_userid = self._list[self._current_index][0]
_username = self._list[self._current_index][1]
_nick = self._list[self._current_index][2]
_isbot = self._list[self._current_index][3]
_defaulttracks = self._list[self._current_index][4]
_usertracks = self._list[self._current_index][5]
self._current_index += 1
memb = _FileAttrs(_name, _path, _type)
memb = _DBAttrs(_userid,
_username,
_nick,
_isbot,
_defaulttracks,
_usertracks)
return memb
raise StopIteration

View File

@@ -1,11 +1,13 @@
import mimetypes
import os
from typing import Tuple, Optional
class ListGenerator:
def __init__(self, path: str = None):
self.path = path
self.list: list = self._lister(self.path)
self._size = len(self.list)
self._current_index = 0
def __str__(self) -> str:
@@ -14,25 +16,10 @@ class ListGenerator:
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 = []
@@ -47,6 +34,10 @@ class ListGenerator:
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, type):
@@ -58,27 +49,32 @@ class _FileAttrs:
return self.name
def __repr__(self):
return f'<File attrs name={self.name} path={self.path} type={self.path}>'
return f'<File attrs name={self.name} path={self.path} type=<{self.type}> >'
class _ListGenerationIter:
def __init__(self, list_class):
self._current_index = 0
self._list = list_class.list
self._name = list_class.name
self._type = list_class.type
self._name = self._list[self._current_index]
self._path = list_class.path
self._size = len(self._list)
self._current_index = 0
def __iter__(self):
return self
@property
def type(self) -> tuple[Optional[str], Optional[str]]:
guess = mimetypes.guess_type(f'{self._path}/{self._list[self._current_index]}')[0]
return guess
def __next__(self):
if self._current_index < self._size:
_name = self._list[self._current_index]
_path = self._path
_type = self._type
_type = self.type
self._current_index += 1
memb = _FileAttrs(_name, _path, _type)
return memb