From 090896b9ee2d969dc54a2821656868be678b144e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 29 Mar 2019 15:40:45 +0100 Subject: [PATCH] METACLASSIIIIIIIIIIIIII --- royalnet/database/alchemy.py | 15 +++++++++++++-- royalnet/database/tables/__init__.py | 3 +++ royalnet/database/tables/royals.py | 20 ++++++++++++++++++++ royalnet/utils/__init__.py | 4 +++- royalnet/utils/classdictjanitor.py | 8 ++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 royalnet/database/tables/royals.py create mode 100644 royalnet/utils/classdictjanitor.py diff --git a/royalnet/database/alchemy.py b/royalnet/database/alchemy.py index de290d29..e0f4464a 100644 --- a/royalnet/database/alchemy.py +++ b/royalnet/database/alchemy.py @@ -1,6 +1,17 @@ -import typing from sqlalchemy import create_engine -from sqlalchemy.engine import Engine from sqlalchemy.ext.declarative import declarative_base 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() diff --git a/royalnet/database/tables/__init__.py b/royalnet/database/tables/__init__.py index e69de29b..9b85c1d9 100644 --- a/royalnet/database/tables/__init__.py +++ b/royalnet/database/tables/__init__.py @@ -0,0 +1,3 @@ +from .royals import Role, Royal + +__all__ = ["Role", "Royal"] diff --git a/royalnet/database/tables/royals.py b/royalnet/database/tables/royals.py new file mode 100644 index 00000000..2d5220ec --- /dev/null +++ b/royalnet/database/tables/royals.py @@ -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) diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index 2af35184..38afaa7c 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -2,5 +2,7 @@ from .asyncify import asyncify from .call import Call from .command import Command, CommandArgs, InvalidInputError, UnsupportedError 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"] diff --git a/royalnet/utils/classdictjanitor.py b/royalnet/utils/classdictjanitor.py new file mode 100644 index 00000000..cd16fe1c --- /dev/null +++ b/royalnet/utils/classdictjanitor.py @@ -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