def _start_tmp_fact(self, fact): """ Store new ongoing fact in persistent tmp file Args: fact (hamster_lib.Fact): Fact to be stored. Returns: hamster_lib.Fact: Fact stored. Raises: ValueError: If we already have a ongoing fact running. ValueError: If the fact passed does have an end and hence does not qualify for an 'ongoing fact'. """ self.store.logger.debug(_("Fact: '{}' has been received.".format(fact))) if fact.end: message = _("The passed fact has an end specified.") self.store.logger.debug(message) raise ValueError(message) tmp_fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if tmp_fact: message = _("Trying to start with ongoing fact already present.") self.store.logger.debug(message) raise ValueError(message) else: with open(self._get_tmp_fact_path(), 'wb') as fobj: pickle.dump(fact, fobj) self.store.logger.debug(_("New temporary fact started.")) return fact
def _start_tmp_fact(self, fact): """ Store new ongoing fact in persistent tmp file Args: fact (hamster_lib.Fact): Fact to be stored. Returns: hamster_lib.Fact: Fact stored. Raises: ValueError: If we already have a ongoing fact running. ValueError: If the fact passed does have an end and hence does not qualify for an 'ongoing fact'. """ self.store.logger.debug(_( "Fact: '{}' has been received.".format(fact))) if fact.end: message = _("The passed fact has an end specified.") self.store.logger.debug(message) raise ValueError(message) tmp_fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if tmp_fact: message = _("Trying to start with ongoing fact already present.") self.store.logger.debug(message) raise ValueError(message) else: with open(self._get_tmp_fact_path(), 'wb') as fobj: pickle.dump(fact, fobj) self.store.logger.debug(_("New temporary fact started.")) return fact
def stop_tmp_fact(self, end_hint=None): """ Stop current 'ongoing fact'. Args: end_hint (datetime.timedelta or datetime.datetime, optional): Hint to be considered when setting ``Fact.end``. If no hint is provided ``Fact.end`` will be ``datetime.datetime.now()``. If a ``datetime`` is provided, this will be used as ``Fact.end`` value. If a ``timedelta`` is provided it will be added to ``datetime.datetime.now()``. If you want the computed ``end`` to be *before* ``now()`` you can pass negative ``timedelta`` values. Defaults to None. Returns: hamster_lib.Fact: The stored fact. Raises: TypeError: If ``end_hint`` is not a ``datetime.datetime`` or ``datetime.timedelta`` instance or ``None``. ValueError: If there is no currently 'ongoing fact' present. ValueError: If the final end value (due to the hint) is before the fact's start value. """ self.store.logger.debug(_("Stopping 'ongoing fact'.")) if not ((end_hint is None) or isinstance(end_hint, datetime.datetime) or ( isinstance(end_hint, datetime.timedelta))): raise TypeError(_( "The 'end_hint' you passed needs to be either a" "'datetime.datetime' or 'datetime.timedelta' instance." )) if end_hint: if isinstance(end_hint, datetime.datetime): end = end_hint else: end = datetime.datetime.now() + end_hint else: end = datetime.datetime.now() fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if fact: if fact.start > end: raise ValueError(_("The indicated 'end' value seem to be before its 'start'.")) else: fact.end = end result = self.save(fact) os.remove(self._get_tmp_fact_path()) self.store.logger.debug(_("Temporary fact stopped.")) else: message = _("Trying to stop a non existing ongoing fact.") self.store.logger.debug(message) raise ValueError(message) return result
def get_tmp_fact(self): """ Provide a way to retrieve any existing 'ongoing fact'. Returns: hamster_lib.Fact: An instance representing our current 'ongoing fact'.capitalize Raises: KeyError: If no ongoing fact is present. """ self.store.logger.debug(_("Trying to get 'ongoing fact'.")) fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if not fact: message = _("Tried to retrieve an 'ongoing fact' when there is none present.") self.store.logger.debug(message) raise KeyError(message) return fact
def get_tmp_fact(self): """ Provide a way to retrieve any existing 'ongoing fact'. Returns: hamster_lib.Fact: An instance representing our current 'ongoing fact'.capitalize Raises: KeyError: If no ongoing fact is present. """ self.store.logger.debug(_("Trying to get 'ongoing fact'.")) fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if not fact: message = _("Tried to retrieve an 'ongoing fact' when there is none present.") self.store.logger.debug(message) raise KeyError(message) return fact
def stop_tmp_fact(self): """ Stop current 'ongoing fact'. Returns: hamster_lib.Fact: The stored fact. Raises: ValueError: If there is no currently 'ongoing fact' present. """ self.store.logger.debug(_("Stopping 'ongoing fact'.")) fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if fact: fact.end = datetime.datetime.now() result = self.save(fact) os.remove(self._get_tmp_fact_path()) self.store.logger.debug(_("Temporary fact stopped.")) else: message = _("Trying to stop a non existing ongoing fact.") self.store.logger.debug(message) raise ValueError(message) return result
def stop_tmp_fact(self): """ Stop current 'ongoing fact'. Returns: hamster_lib.Fact: The stored fact. Raises: ValueError: If there is no currently 'ongoing fact' present. """ self.store.logger.debug(_("Stopping 'ongoing fact'.")) fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if fact: fact.end = datetime.datetime.now() result = self.save(fact) os.remove(self._get_tmp_fact_path()) self.store.logger.debug(_("Temporary fact stopped.")) else: message = _("Trying to stop a non existing ongoing fact.") self.store.logger.debug(message) raise ValueError(message) return result
def cancel_tmp_fact(self): """ Provide a way to stop an 'ongoing fact' without saving it in the backend. Returns: None: If everything worked as expected. Raises: KeyError: If no ongoing fact is present. """ # [TODO] # Maybe it would be useful to return the canceled fact instead. So it # would be available to clients. Otherwise they may be tempted to look # it up before canceling. which would result in two retrievals. self.store.logger.debug(_("Trying to cancel 'ongoing fact'.")) fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if not fact: message = _("Trying to stop a non existing ongoing fact.") self.store.logger.debug(message) raise KeyError(message) os.remove(self._get_tmp_fact_path()) self.store.logger.debug(_("Temporary fact stoped."))
def cancel_tmp_fact(self): """ Provide a way to stop an 'ongoing fact' without saving it in the backend. Returns: None: If everything worked as expected. Raises: KeyError: If no ongoing fact is present. """ # [TODO] # Maybe it would be useful to return the canceled fact instead. So it # would be available to clients. Otherwise they may be tempted to look # it up before canceling. which would result in two retrievals. self.store.logger.debug(_("Trying to cancel 'ongoing fact'.")) fact = helpers._load_tmp_fact(self._get_tmp_fact_path()) if not fact: message = _("Trying to stop a non existing ongoing fact.") self.store.logger.debug(message) raise KeyError(message) os.remove(self._get_tmp_fact_path()) self.store.logger.debug(_("Temporary fact stoped."))
def test_valid(self, base_config, tmp_fact): """Make sure that we return the stored 'ongoing fact' as expected.""" result = helpers._load_tmp_fact(base_config['tmpfile_path']) assert result == tmp_fact
def test_file_instance_invalid(self, base_config): """Make sure we throw an error if the instance picked in the file is no ``Fact``.""" with open(base_config['tmpfile_path'], 'wb') as fobj: pickle.dump('foobar', fobj) with pytest.raises(TypeError): helpers._load_tmp_fact(base_config['tmpfile_path'])
def test_no_file_present(self): """Make sure that we return ``False`` if there is no 'tmpfile' present.""" assert helpers._load_tmp_fact('non_existing_file') is False
def test_valid(self, base_config, tmp_fact, fact): """Make sure that we return the stored 'ongoing fact' as expected.""" fact.end = None result = helpers._load_tmp_fact(base_config['tmpfile_path']) assert result == fact
def test_file_instance_invalid(self, base_config): """Make sure we throw an error if the instance picked in the file is no ``Fact``.""" with open(base_config['tmpfile_path'], 'wb') as fobj: pickle.dump('foobar', fobj) with pytest.raises(TypeError): helpers._load_tmp_fact(base_config['tmpfile_path'])
def test_no_file_present(self): """Make sure that we return ``False`` if there is no 'tmpfile' present.""" assert helpers._load_tmp_fact('non_existing_file') is False