def as_hamster(self): """Provide an convenient way to return it as a ``hamster_lib.Fact`` instance.""" return Fact( pk=self.pk, activity=self.activity.as_hamster(), start=self.start, end=self.end, description=self.description, )
def generate(): activity = activity_factory() tags = set([tag_factory() for i in range(1)]) start, end = start_end_datetimes fact = Fact(activity, start, end, pk=None, description=description, tags=tags) return fact
def test_create_from_raw_fact_valid(self, raw_fact_parametrized): """Make sure the constructed ``Fact``s anatomy reflets our expectations.""" raw_fact, expectation = raw_fact_parametrized fact = Fact.create_from_raw_fact(raw_fact) assert fact.start == expectation['start'] assert fact.end == expectation['end'] assert fact.activity.name == expectation['activity'] if fact.activity.category: assert fact.activity.category.name == expectation['category'] else: assert expectation['category'] is None assert fact.description == expectation['description']
def test_fact_init_valid(self, activity, start_end_datetimes, pk_valid_parametrized, description_valid_parametrized, tag_list_valid_parametrized): """Make sure valid values instaniate a Fact.""" fact = Fact(activity, start_end_datetimes[0], start_end_datetimes[1], pk_valid_parametrized, description_valid_parametrized, tag_list_valid_parametrized) assert fact.activity == activity assert fact.pk == pk_valid_parametrized assert fact.description == description_valid_parametrized assert fact.start == start_end_datetimes[0] assert fact.end == start_end_datetimes[1] assert fact.tags == tag_list_valid_parametrized
def create(self, start, end, activity, category, description): """ Create a fact for the given date """ command = activity if category: command = command + '@' + category if description: command = command + ',' + description fact = Fact.create_from_raw_fact(command) fact.start = self._cleanStart(start.toPyDateTime()) fact.end = self._cleanEnd(end.toPyDateTime()) try: fact = self._control.facts.save(fact) except ValueError as err: self.errorMessage.emit("Fact error: {0}".format(err)) else: self.factAdded.emit(FactPyQt(fact))
def _on_start_tracking_button(self, button): """ Start a new *ongoing fact*. Note: Whilst we accept the full ``raw_fact`` syntax, we ignore any ``Fact.end`` information encoded in the string. Unlike legacy hamster we *only* deal with *ongoing facts* in this widget. """ # [FIXME] # This should be done in one place only. And the hamster-lib. If at all # via hamster-lib.helpers. def complete_tmp_fact(fact): """Apply fallback logic in case no start time has been encoded.""" if not fact.start: fact.start = datetime.datetime.now() # Make sure we dismiss any extracted end information. fact.end = None return fact raw_fact = _u(self.raw_fact_entry.props.text) try: fact = Fact.create_from_raw_fact(raw_fact) except Exception as error: helpers.show_error(helpers.get_parent_window(self), error) else: fact = complete_tmp_fact(fact) try: fact = self._controler.store.facts.save(fact) except Exception as error: helpers.show_error(self.get_top_level(), error) else: self.emit('tracking-started') self._controler.signal_handler.emit('facts-changed') self.reset()
def updated_fact(self): """Fact instance using values at the time of accessing it.""" def get_raw_fact_value(): """Get text from raw fact entry field.""" return _u(self._raw_fact_widget.get_text()) def get_description_value(): """Get unicode value from widget.""" text_view = self._description_widget.get_child() text_buffer = text_view.get_buffer() start, end = text_buffer.get_bounds() return _u(text_buffer.get_text(start, end, True)) # Create a new fact instance from the provided raw string. fact = Fact.create_from_raw_fact(get_raw_fact_value()) # Instead of transferring all attributes of the parsed fact to the # existing ``self._fact`` we just go the other way round and attach the # old facts PK to the newly created instance. fact.pk = self._fact.pk # Explicit description trumps anything that may have been included in # the `raw_fact``. fact.description = get_description_value() return fact
def _on_start_tracking_button(self, button): """ Start a new *ongoing fact*. Note: Whilst we accept the full ``raw_fact`` syntax, we ignore any ``Fact.end`` information encoded in the string. Unlike legacy hamster we *only* deal with *ongoing facts* in this widget. """ # [FIXME] # This should be done in one place only. And the hamster-lib. If at all # via hamster-lib.helpers. def complete_tmp_fact(fact): """Apply fallback logic in case no start time has been encoded.""" if not fact.start: fact.start = datetime.datetime.now() # Make sure we dismiss any extracted end information. fact.end = None return fact raw_fact = _u(self.raw_fact_entry.props.text) try: fact = Fact.create_from_raw_fact(raw_fact) except Exception as error: helpers.show_error(self.get_toplevel(), error) else: fact = complete_tmp_fact(fact) try: fact = self._controler.store.facts.save(fact) except Exception as error: helpers.show_error(self.get_top_level(), error) else: self.emit('tracking-started') self._controler.signal_handler.emit('facts-changed') self.reset()
def start(self, command): """ Start a fact """ if not command: self.errorMessage.emit('Empty fact information, can\'t start fact.') return # Some basic handling # If the command has a comma but no @, add the @ since comma is for comment if ( ',' in command ) and ( not '@' in command ): command = command.replace( ',', '@,' ) fact = Fact.create_from_raw_fact(command) if not fact.start: # No start time for the fact, set to now fact.start = datetime.datetime.now() # Clean up the fact start as per the hamster-QML interface. fact.start = self._cleanStart(fact.start) # Save the fact. If the fact does not have an end time it will be set as the # current fact. # At this point first check if there is not alreay a fact ongoing, # if there it, it must be stopped with the stop time set to before # the stop time of the ongoing fact. self.stop(fact.start, True) try: fact = self._control.facts.save(fact) except ValueError as err: self.errorMessage.emit("Fact start error: {0}".format(err)) else: self.startSuccessful.emit() # Check if the started fact has a end time. If it does have one, a # start and end time was specified and the fact was added to the # database. If it does not have a end it is an ongoing fact. if fact.end: self.factAdded.emit(FactPyQt(fact)) self.current()
((-1, 'foo'), Tag('foo', None)), ((-2, ''), None), (helpers.DBusTag(1, 'foo'), Tag('foo', 1)), (helpers.DBusTag(-1, 'foo'), Tag('foo', None)), (helpers.DBusTag(-2, ''), None), )) def test_dbus_to_hamster_tag(tag_tuple, expectation): """Make sure that de-serialization works as intended.""" result = helpers.dbus_to_hamster_tag(tag_tuple) assert result == expectation if expectation: assert isinstance(result, Tag) @pytest.mark.parametrize(('fact', 'expectation'), ( (Fact(Activity('foo', pk=1), dt.datetime(2017, 2, 1, 18), pk=1), DBusFact(1, '2017-02-01 18:00:00', '', '', DBusActivity(1, 'foo', DBusCategory(-2, ''), False), dbus.Array([], '(is)'))), (Fact(Activity('foo', pk=1), dt.datetime(2017, 2, 1, 18), pk=None), DBusFact(-1, '2017-02-01 18:00:00', '', '', DBusActivity(1, 'foo', DBusCategory(-2, ''), False), dbus.Array([], '(is)'))), (Fact(Activity('foo', pk=1), start=None, pk=None), DBusFact(-1, '', '', '', DBusActivity(1, 'foo', DBusCategory(-2, ''), False), dbus.Array([], '(is)'))), )) def test_hamster_to_dbus_fact(fact, expectation): """Make sure that serialization works as intended.""" result = helpers.hamster_to_dbus_fact(fact)
def test_create_from_raw_fact_with_delta(self, raw_fact, expectations): fact = Fact.create_from_raw_fact(raw_fact) assert fact.start == expectations['start']
def test_create_from_raw_fact_invalid(self, invalid_raw_fact_parametrized): """Make sure invalid string raises an exception.""" with pytest.raises(ValueError): Fact.create_from_raw_fact(invalid_raw_fact_parametrized)
def test_update_tmp_fact(self, basestore, tmp_fact, new_fact_values): """Make sure the updated fact has the new values.""" updated_fact = Fact(**new_fact_values(tmp_fact)) result = basestore.facts.update_tmp_fact(updated_fact) assert result == updated_fact
def generate(): activity = activity_factory() start, end = start_end_datetimes return Fact(activity, start, end, pk=None, description=description)
def test_create_from_raw_fact_valid(self, valid_raw_fact_parametrized): """Make sure that a valid raw fact creates a proper Fact.""" assert Fact.create_from_raw_fact(valid_raw_fact_parametrized)