def test_round_trip(self): fact = Fact.parse( "11:00 12:00 activity, with comma@category,, description, with comma #and #tags" ) dbus_fact = to_dbus_fact_json(fact) return_fact = from_dbus_fact_json(dbus_fact) self.assertEqual(return_fact, fact) dbus_fact = to_dbus_fact(fact) return_fact = from_dbus_fact(dbus_fact) self.assertEqual(return_fact, fact) fact = Fact.parse("11:00 activity") dbus_fact = to_dbus_fact_json(fact) return_fact = from_dbus_fact_json(dbus_fact) self.assertEqual(return_fact, fact) dbus_fact = to_dbus_fact(fact) return_fact = from_dbus_fact(dbus_fact) self.assertEqual(return_fact, fact) range, __ = dt.Range.parse("2020-01-19 11:00 - 2020-01-19 12:00") dbus_range = to_dbus_range(range) return_range = from_dbus_range(dbus_range) self.assertEqual(return_range, range)
def GetFactJSON(self, fact_id): """Get fact by id. Return fact in JSON format (cf. to_dbus_fact_json) """ fact = self.get_fact(fact_id) return to_dbus_fact_json(fact)
def update_fact(self, fact_id, fact, temporary_activity = False): """Update fact values. See add_fact for rules. Update is performed via remove/insert, so the fact_id after update should not be used anymore. Instead use the ID from the fact dict that is returned by this function""" dbus_fact = to_dbus_fact_json(fact) new_id = self.conn.UpdateFactJSON(fact_id, dbus_fact) return new_id
def add_fact(self, fact, temporary_activity = False): """Add fact (Fact).""" assert fact.activity, "missing activity" if not fact.start_time: logger.info("Adding fact without any start_time is deprecated") fact.start_time = dt.datetime.now() dbus_fact = to_dbus_fact_json(fact) new_id = self.conn.AddFactJSON(dbus_fact) return new_id
def check_fact(self, fact, default_day=None): """Check Fact validity for inclusion in the storage. default_day (date): Default hamster day, used to simplify some hint messages (remove unnecessary dates). None is safe (always show dates). """ if not fact.start_time: # Do not even try to pass fact through D-Bus as # conversions would fail in this case. raise FactError("Missing start time") dbus_fact = to_dbus_fact_json(fact) dbus_day = to_dbus_date(default_day) success, message = self.conn.CheckFact(dbus_fact, dbus_day) if not success: raise FactError(message) return success, message
def GetFactsJSON(self, dbus_range, search_terms): """Gets facts between the day of start and the day of end. Args: dbus_range (str): same format as on the command line. (cf. dt.Range.parse) search_terms (str): If starts with "not ", the search terms will be reversed Return: array of D-Bus facts in JSON format. (cf. to_dbus_fact_json) This will be the preferred way to get facts. """ range = from_dbus_range(dbus_range) return [ to_dbus_fact_json(fact) for fact in self.get_facts(range, search_terms=search_terms) ]
def GetTodaysFactsJSON(self): """Gets facts of the current hamster day. Return an array of facts in JSON format. """ return [to_dbus_fact_json(fact) for fact in self.get_todays_facts()]