diff --git a/launcher.py b/launcher.py index 16f7749..35001f6 100644 --- a/launcher.py +++ b/launcher.py @@ -12,7 +12,7 @@ class Launcher: def __init__(self, **modules): """""" self.persistence = p_io.PersistentDict("persistence/prst.json") - self.db = p_out.DBConnector() + self.db = p_out.DataBaseConnector() logger.info(self.__class__.__name__ + " initialized") diff --git a/persistence/models.py b/persistence/models.py index 0fc0303..17ad086 100644 --- a/persistence/models.py +++ b/persistence/models.py @@ -4,25 +4,15 @@ from playhouse.shortcuts import ReconnectMixin import logging logger = logging.getLogger(__name__) -from . import keys -dbk = keys.db_keys +db = DatabaseProxy() +# set the nature of the db at runtime class ReconnectDataBase(ReconnectMixin, MySQLDatabase): pass - -db = ReconnectDataBase( - dbk["name"], - user=dbk["username"], - password=dbk["password"], - host=dbk["url"], - port=dbk["port"], - autorollback=True -) - class DBModel(Model): # specific to the above DB class Meta: diff --git a/persistence/p_out.py b/persistence/p_out.py index 6181b62..9eca8a6 100644 --- a/persistence/p_out.py +++ b/persistence/p_out.py @@ -1,4 +1,16 @@ from . import models +from peewee import * +# from playhouse.pool import PooledMySQLDatabase +from playhouse.shortcuts import ReconnectMixin +import inspect + +from . import keys +dbk = keys.db_keys + + +class ReconnectDataBase(ReconnectMixin, MySQLDatabase): + pass + class DBConnector: @@ -16,3 +28,88 @@ class DBConnector: def create_tables(self): self.db.create_tables([self.sensors, self.chats, self.errors, self.lists]) + + +class DataBaseConnector: + def __init__(self) -> None: + self.db_object = models.ReconnectDataBase( + dbk["name"], + user=dbk["username"], + password=dbk["password"], + host=dbk["url"], + port=dbk["port"], + autorollback=True + ) + models.db.initialize(self.db_object) + + # self.sensors = models.SensorMetric + # self.chats = models.ChatMetric + # self.errors = models.ErrorMetric + # self.lists = models.List + ## Set as property methods instead + + self.db_object.create_tables([self.sensors, self.chats, self.errors, self.lists]) + + @property + def sensors(self): + self.connect_first() + return models.SensorMetric + + @property + def chats(self): + self.connect_first() + return models.ChatMetric + + @property + def errors(self): + self.connect_first() + return models.ErrorMetric + + @property + def lists(self): + self.connect_first() + return models.List + + + def connect_first(self): + if self.db_object.is_closed(): + self.db_object.connect() + + + +# def auto_reconnect(func, *args, **kwargs): +# return func + +# def classwide_decorator(decorator): +# def decorate(cls): +# for attr in inspect.getmembers(cls, inspect.ismethod): # there's propably a better way to do this +# # TODO: filter init +# print(attr) +# if callable(getattr(cls, attr)): +# setattr(cls, attr, decorator(getattr(cls, attr))) +# return cls +# return decorate + +# # apply auto_reconnect to every method so that every method first checks the db connection and reconnects if necessary +# @classwide_decorator(auto_reconnect) +# class DataBaseConnector(ReconnectMixin, MySQLDatabase): +# def __init__(self, *args, **kwargs): +# super().__init__( +# dbk["name"], +# user=dbk["username"], +# password=dbk["password"], +# host=dbk["url"], +# port=dbk["port"], +# autorollback=True, +# *args, **kwargs) + +# models.db.initialize(self) +# self.sensors = models.SensorMetric +# self.chats = models.ChatMetric +# self.errors = models.ErrorMetric +# self.lists = models.List + +# self.create_tables([self.sensors, self.chats, self.errors, self.lists]) + +# def m1(self): pass +# def m2(self, x): pass diff --git a/test.py b/test.py new file mode 100644 index 0000000..0ea607f --- /dev/null +++ b/test.py @@ -0,0 +1,20 @@ + +class MyResource: + def __enter__(self): + print('Entering context.') + return self + + def __exit__(self, *exc): + print('EXITING context.') + + def try_me(self): + print("I work") + +def fun(): + with MyResource() as a: + print('Returning inside with-statement.') + return a + print('Returning outside with-statement.') + +t =fun() +t.try_me() \ No newline at end of file