|
@@ -4,6 +4,7 @@ from enum import Enum, auto
|
|
|
import inspect
|
|
|
import functools
|
|
|
|
|
|
+from sqlalchemy import Column, DateTime, Binary, String, Float, Integer
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
|
|
|
|
@@ -12,6 +13,35 @@ BANGS = ('!',)
|
|
|
ModelBase = declarative_base()
|
|
|
|
|
|
|
|
|
+class GroupBasedSingleton(ModelBase):
|
|
|
+ __tablename__ = "group_based_singleton"
|
|
|
+ group_id = Column(String, primary_key=True)
|
|
|
+ command_name = Column(String, primary_key=True)
|
|
|
+ subpart_name = Column(String, primary_key=True)
|
|
|
+ integer_data = Column(Integer)
|
|
|
+ float_data = Column(Float)
|
|
|
+ string_data = Column(String)
|
|
|
+ binary_data = Column(Binary)
|
|
|
+ datetime_data = Column(DateTime)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_or_create(db, group_id, command_name, subpart_name):
|
|
|
+ sing = db.query(GroupBasedSingleton).get((group_id, command_name, subpart_name))
|
|
|
+ if sing is None:
|
|
|
+ sing = GroupBasedSingleton(
|
|
|
+ group_id=group_id,
|
|
|
+ command_name=command_name,
|
|
|
+ subpart_name=subpart_name,
|
|
|
+ integer_data=None,
|
|
|
+ float_data=None,
|
|
|
+ string_data=None,
|
|
|
+ binary_data=None,
|
|
|
+ datetime_data=None
|
|
|
+ )
|
|
|
+ db.add(sing)
|
|
|
+ return sing
|
|
|
+
|
|
|
+
|
|
|
def pop_arg(text):
|
|
|
if text is None:
|
|
|
return None, None
|
|
@@ -171,6 +201,9 @@ def as_plugin(command):
|
|
|
converters.append(lambda self, db, msg: self.logger)
|
|
|
elif p in ("bot", "rollbot"):
|
|
|
converters.append(lambda self, db, msg: self.bot)
|
|
|
+ elif p.startswith("data") or p.endswith("data") or p in ("group_singleton", "singleton"):
|
|
|
+ subp = fn.__annotations__.get(p, "")
|
|
|
+ converters.append(lambda self, db, msg, subp=subp: GroupBasedSingleton.get_or_create(db, msg.group_id, self.command, subp))
|
|
|
else:
|
|
|
raise ValueError(f"Illegal argument name {p} in decorated plugin {command_name}")
|
|
|
|