def refresh(self) -> None: self._mutex.acquire() # just to ensure things are executed properly try: cnx, cursor = __CONNECT__(DB_USER, DB_PASSWORD, DATABASE) selected_curtains = SELECT_Curtains(cursor) # Cleanup events since destructor doesn't work, especially when called by dict reassignment. [curtain.delete_events() for curtain in self._Curtains.values()] self._Curtains = { curtain["id"]: Curtain(**{ **curtain, "System": self }) for curtain in selected_curtains } self._Options = { option["id"]: Option(**option) for option in SELECT_Options(cursor) } self._Options_names = { self._Options[opt].name(): self._Options[opt] for opt in self._Options } __CLOSE__(cnx, cursor) finally: self._mutex.release()
def New(**info) -> object: # Check attributes are present from System.Curtain import Curtain as Curtain_Class # must be imported here to prevent circular importing keys = ["Curtain", "desired_position", "time"] types = [Curtain_Class, int, datetime] CurtainEvent.validate_data(keys, types, info) # Set possible missing attributes names, defaults = ["Options.id", "is_activated", "is_current"], [None, False, True] [ info.update({name: info.get(name, defaults[x])}) for x, name in enumerate(names) ] # Add to DB cnx, cursor = __CONNECT__(DB_USER, DB_PASSWORD, DATABASE) event_params = [ info["Curtain"].id(), info["Options.id"], info["desired_position"], info["time"] ] info["id"] = INSERT_CurtainsEvents(cnx, cursor, *event_params) if (not info["id"]): __CLOSE__(cnx, cursor) raise Exception("Unable to add event to DB") __CLOSE__(cnx, cursor) # Return new instance of CurtainEvents return CurtainEvent(**info)
def prior_CurtainEvents_for_current_day_of_week(self, earliest: object = None ) -> list: earliest = earliest or datetime.today() - timedelta(days=28) cnx, cursor = __CONNECT__(DB_USER, DB_PASSWORD, DATABASE) CurtainEvents_data = SELECT_CurtainsEvents(cursor, CurtainsEvents_id) __CLOSE__(cnx, cursor)
def __init__(self, **curtain_info): DBClass.__init__(self, "UPDATE_Curtains", **curtain_info) # Get associated relations cnx, cursor = __CONNECT__(DB_USER, DB_PASSWORD, DATABASE) current_events = SELECT_current_CurtainsEvents(cursor, self._id) curtains_options = SELECT_CurtainsOptions(cursor, self._id) __CLOSE__(cnx, cursor) self._CurtainEvents = { event["id"]: CurtainEvent(**{ **event, "Curtain": self }) for event in current_events } self._CurtainOptions_dict = { option["Options.id"]: CurtainOption(**option) for option in curtains_options } self._CurtainOptions_list = self._CurtainOptions_dict.values()
def CurtainEvent(self, CurtainEvent_id: int = None): if (CurtainEvent_id in self._CurtainEvents): return self._CurtainEvents.get(CurtainEvent_id) # easy! # not found, check if in DB cnx, cursor = __CONNECT__(DB_USER, DB_PASSWORD, DATABASE) CurtainEvents_data = SELECT_CurtainsEvents(cursor, CurtainEvent_id) __CLOSE__(cnx, cursor) # return if found in DB if (CurtainEvents_data): event = CurtainEvent(**{ **CurtainEvents_data, "Curtain": self }) if (not CurtainEvents_data): return None if (event.Curtains_id() != self._id): event.delete() return None self._CurtainEvents[event.id()] = event return event
def function(new_value=None): if (isinstance(new_value, type(None))): return getattr(self, attribute_name) if (new_value == getattr(self, attribute_name)): return True # values match, take the easy way out # gotta update DB to match structure DB_function = getattr(DBFunctions, db_prefix + attribute_name) cnx, cursor = __CONNECT__(DB_USER, DB_PASSWORD, DATABASE) success_flag = DB_function(cnx, cursor, self._id, new_value) if (success_flag): setattr(self, attribute_name, new_value) return success_flag + bool(__CLOSE__(cnx, cursor))