1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

METACLASSIIIIIIIIIIIIII

This commit is contained in:
Steffo 2019-03-29 15:40:45 +01:00
parent 38baa29fc3
commit 090896b9ee
5 changed files with 47 additions and 3 deletions

View file

@ -1,6 +1,17 @@
import typing
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from .tables import Royal
from ..utils import classdictjanitor
class Alchemy:
def __init__(self, database_uri: str = "sqlite://"):
self.engine = create_engine(database_uri)
self.Base = declarative_base(bind=self.engine)
self.Session = sessionmaker(bind=self.engine)
self._create_tables()
def _create_tables(self):
self.Royal = type("Royal", (self.Base,), classdictjanitor(Royal))
self.Base.metadata.create_all()

View file

@ -0,0 +1,3 @@
from .royals import Role, Royal
__all__ = ["Role", "Royal"]

View file

@ -0,0 +1,20 @@
from enum import Enum
from sqlalchemy import Column, \
Integer, \
String, \
LargeBinary
class Role(Enum):
Guest = "Guest"
Member = "Member"
Admin = "Admin"
class Royal:
__tablename__ = "royals"
uid = Column(Integer, unique=True, primary_key=True)
username = Column(String, unique=True, nullable=False)
password = Column(LargeBinary)
role = Column(String, nullable=False)

View file

@ -2,5 +2,7 @@ from .asyncify import asyncify
from .call import Call from .call import Call
from .command import Command, CommandArgs, InvalidInputError, UnsupportedError from .command import Command, CommandArgs, InvalidInputError, UnsupportedError
from .safeformat import safeformat from .safeformat import safeformat
from .classdictjanitor import classdictjanitor
__all__ = ["asyncify", "Call", "Command", "safeformat", "InvalidInputError", "UnsupportedError", "CommandArgs"] __all__ = ["asyncify", "Call", "Command", "safeformat", "InvalidInputError", "UnsupportedError", "CommandArgs",
"classdictjanitor"]

View file

@ -0,0 +1,8 @@
def classdictjanitor(class_) -> dict:
"""Return the cleaned class attributes in a dict."""
d = dict(class_.__dict__)
del d["__module__"]
del d["__dict__"]
del d["__weakref__"]
del d["__doc__"]
return d