Add TelltaleFile helper class
This commit is contained in:
parent
63600416b3
commit
0a0fae3e90
2 changed files with 91 additions and 0 deletions
53
mylib/telltale.py
Normal file
53
mylib/telltale.py
Normal file
|
@ -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
|
38
tests/test_telltale.py
Normal file
38
tests/test_telltale.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue