1
0
mirror of https://codeberg.org/polarisfm/youtube-dl synced 2024-11-26 10:24:33 +01:00

Remove logic checking if file has changed

This commit is contained in:
Sergio Kef 2020-04-20 15:11:17 +02:00
parent f947efd3e9
commit e58f3a61fe
2 changed files with 5 additions and 47 deletions

View File

@ -39,38 +39,6 @@ class TestArchive(unittest.TestCase):
def test_archive_not_exists(self): def test_archive_not_exists(self):
self.assertFalse("dl_id" in self.archive) self.assertFalse("dl_id" in self.archive)
def test_archive_last_read_on_write(self):
t1 = self.archive._last_read
self.archive.record_download("dl_id")
t2 = self.archive._last_read
self.assertNotEqual(t1, t2)
def test_archive_last_read_on_read(self):
t1 = self.archive._last_read
self.archive.record_download("dl_id 1")
t2 = self.archive._last_read
self.assertNotEqual(t1, t2)
def test_archive_file_not_changed(self):
self.archive.record_download("dl_id")
self.assertFalse(self.archive._file_changed())
def test_archive_file_changed(self):
self.archive.record_download("dl_id 1")
with open(self.archive.filepath, "a", encoding="utf-8") as f_out:
sleep(0.01)
f_out.write("dl_id 2\n")
self.assertTrue(self.archive._file_changed())
def test_archive_file_changed_exists(self):
self.archive.record_download("dl_id 1")
with open(self.archive.filepath, "a", encoding="utf-8") as f_out:
sleep(0.01)
f_out.write("dl_id 2\n")
self.assertTrue(self.archive._file_changed())
self.assertFalse("dl_id 2" in self.archive._data)
self.assertTrue("dl_id 2" in self.archive)
def test_archive_multiple_writes(self): def test_archive_multiple_writes(self):
self.archive.record_download("dl_id 1") self.archive.record_download("dl_id 1")
self.archive.record_download("dl_id 2") self.archive.record_download("dl_id 2")

View File

@ -18,25 +18,22 @@ class Archive(object):
_data: set Container for holding a cache of the archive _data: set Container for holding a cache of the archive
_disabled: bool When true, all functions are effectively void _disabled: bool When true, all functions are effectively void
filepath: str The filepath of the archive filepath: str The filepath of the archive
_last_read: float The last modification date of the archive file
Methods: Methods:
__contains__ Checks a video id (archive id) exists in archive __contains__ Checks a video id (archive id) exists in archive
_file_changed Checks if file has been modified since last read
_read_archive Reads archive from disk _read_archive Reads archive from disk
record_download Adds a new download to archive record_download Adds a new download to archive
""" """
def __init__(self, filepath): def __init__(self, filepath):
""" Instantiate Archive """ Instantiate Archive
filepath: str or None. The filepath of the archive. If None is provided filepath: str or None. The filepath of the archive. If None or empty
instance can still be used but it's effectively string is provided instance can still be used
doing nothing.""" but it's effectively doing nothing."""
self.filepath = None if filepath == "" else filepath self.filepath = None if filepath == "" else filepath
self._disabled = self.filepath is None self._disabled = self.filepath is None
self._data = set() self._data = set()
self._last_read = 0
def record_download(self, archive_id): def record_download(self, archive_id):
""" Records a new archive_id in the archive """ """ Records a new archive_id in the archive """
@ -45,23 +42,16 @@ class Archive(object):
with locked_file(self.filepath, "a", encoding="utf-8") as file_out: with locked_file(self.filepath, "a", encoding="utf-8") as file_out:
file_out.write(archive_id + "\n") file_out.write(archive_id + "\n")
self._last_read = compat_st_mtime(self.filepath)
self._data.add(archive_id) self._data.add(archive_id)
def _file_changed(self):
""" Checks if file has been modified, using system Modification Date """
if os.path.exists(self.filepath):
return self._last_read < compat_st_mtime(self.filepath)
return True
def _read_file(self): def _read_file(self):
""" Reads the data from archive file """ """ Reads the data from archive file """
if self._disabled or not self._file_changed(): if self._disabled:
return return
try: try:
with locked_file(self.filepath, "r", encoding="utf-8") as file_in: with locked_file(self.filepath, "r", encoding="utf-8") as file_in:
self._data.update(line.strip() for line in file_in) self._data.update(line.strip() for line in file_in)
self._last_read = compat_st_mtime(self.filepath)
except IOError as err: except IOError as err:
if err.errno != errno.ENOENT: if err.errno != errno.ENOENT:
raise raise