From 0a0fae3e90fd7083ed036c32cc07060adcc06ae0 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 12 Apr 2022 12:59:20 +0200 Subject: [PATCH] Add TelltaleFile helper class --- mylib/telltale.py | 53 ++++++++++++++++++++++++++++++++++++++++++ tests/test_telltale.py | 38 ++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 mylib/telltale.py create mode 100644 tests/test_telltale.py diff --git a/mylib/telltale.py b/mylib/telltale.py new file mode 100644 index 0000000..69422f7 --- /dev/null +++ b/mylib/telltale.py @@ -0,0 +1,53 @@ +""" Telltale files helpers """ + +import datetime +import logging +import os + +log = logging.getLogger(__name__) + + +class TelltaleFile: + """ Telltale file helper class """ + + def __init__(self, filepath=None, filename=None, dirpath=None): + assert filepath or filename, "filename or filepath is required" + if filepath: + assert not filename or os.path.basename(filepath) == filename, "filepath and filename does not match" + assert not dirpath or os.path.dirname(filepath) == dirpath, "filepath and dirpath does not match" + self.filename = filename if filename else os.path.basename(filepath) + self.dirpath = ( + dirpath if dirpath + else ( + os.path.dirname(filepath) if filepath + else os.getcwd() + ) + ) + self.filepath = filepath if filepath else os.path.join(self.dirpath, self.filename) + + @property + def last_update(self): + """ Retreive last update datetime of the telltall file """ + try: + return datetime.datetime.fromtimestamp( + os.stat(self.filepath).st_mtime + ) + except FileNotFoundError: + log.info('Telltale file not found (%s)', self.filepath) + return None + + def update(self): + """ Update the telltale file """ + log.info('Update telltale file (%s)', self.filepath) + try: + os.utime(self.filepath, None) + except FileNotFoundError: + open(self.filepath, 'a').close() + + def remove(self): + """ Remove the telltale file """ + try: + os.remove(self.filepath) + return True + except FileNotFoundError: + return True diff --git a/tests/test_telltale.py b/tests/test_telltale.py new file mode 100644 index 0000000..945f2f9 --- /dev/null +++ b/tests/test_telltale.py @@ -0,0 +1,38 @@ +# pylint: disable=missing-function-docstring +""" Tests on opening hours helpers """ + +import datetime +import os +import pytest + +from mylib.telltale import TelltaleFile + + +def test_create_telltale_file(tmp_path): + filename = 'test' + file = TelltaleFile(filename=filename, dirpath=tmp_path) + assert file.filename == filename + assert file.dirpath == tmp_path + assert file.filepath == os.path.join(tmp_path, filename) + + assert not os.path.exists(file.filepath) + assert file.last_update is None + file.update() + assert os.path.exists(file.filepath) + assert isinstance(file.last_update, datetime.datetime) + + +def test_create_telltale_file_with_filepath_and_invalid_dirpath(): + with pytest.raises(AssertionError): + TelltaleFile(filepath='/tmp/test', dirpath='/var/tmp') + + +def test_create_telltale_file_with_filepath_and_invalid_filename(): + with pytest.raises(AssertionError): + TelltaleFile(filepath='/tmp/test', filename='other') + + +def test_remove_telltale_file(tmp_path): + file = TelltaleFile(filename='test', dirpath=tmp_path) + file.update() + assert file.remove()