From eb7344dce61b827cacaf4110a7275798e189208c Mon Sep 17 00:00:00 2001 From: Ajordat Date: Sun, 19 Jan 2020 09:04:54 +0100 Subject: [PATCH 1/7] Added DirectorySnapshotEmpty (#612). --- src/watchdog/utils/dirsnapshot.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/watchdog/utils/dirsnapshot.py b/src/watchdog/utils/dirsnapshot.py index 4083b3fc8..a435c4fe1 100644 --- a/src/watchdog/utils/dirsnapshot.py +++ b/src/watchdog/utils/dirsnapshot.py @@ -347,3 +347,18 @@ def __str__(self): def __repr__(self): return str(self._stat_info) + + +class DirectorySnapshotEmpty(object): + """Class to implement an empty snapshot. This is used together with + DirectorySnapshot and DirectorySnapshotDiff in order to get all the files/folders + in the directory as created. + """ + + @staticmethod + def path(_): + return None + + @property + def paths(self): + return set() From ea21171d2646f04020619b7977edd74f504db90c Mon Sep 17 00:00:00 2001 From: Ajordat Date: Sun, 19 Jan 2020 09:07:55 +0100 Subject: [PATCH 2/7] Added test to show (and test) the usage of DirectorySnapshotEmpty (#612). --- tests/test_snapshot_diff.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_snapshot_diff.py b/tests/test_snapshot_diff.py index 477f04154..c4d3c7eb0 100644 --- a/tests/test_snapshot_diff.py +++ b/tests/test_snapshot_diff.py @@ -21,6 +21,7 @@ from watchdog.utils.dirsnapshot import DirectorySnapshot from watchdog.utils.dirsnapshot import DirectorySnapshotDiff +from watchdog.utils.dirsnapshot import DirectorySnapshotEmpty from watchdog.utils import platform from .shell import mkdir, touch, mv, rm @@ -200,3 +201,19 @@ def inode(self, path): diff_without_device = DirectorySnapshotDiff(ref, snapshot, ignore_device=True) assert diff_without_device.files_deleted == [] assert diff_without_device.files_created == [] + + +def test_empty_snapshot(p): + # Create a file and declare a DirectorySnapshot and a DirectorySnapshotEmpty. + # When we make the diff, although both objects were declared with the same items on + # the directory, the file and directories created BEFORE the DirectorySnapshot will + # be detected as newly created. + + touch(p('a')) + mkdir(p('b', 'c'), parents=True) + ref = DirectorySnapshot(p('')) + empty = DirectorySnapshotEmpty() + + diff = DirectorySnapshotDiff(empty, ref) + assert diff.files_created == [p('a')] + assert sorted(diff.dirs_created) == sorted([p(''), p('b'), p('b', 'c')]) From 3c90c8805aaa6e2f2ddd1f3a438c0060f6879077 Mon Sep 17 00:00:00 2001 From: Ajordat Date: Sun, 19 Jan 2020 09:12:08 +0100 Subject: [PATCH 3/7] Added sphinx class for DirectorySnapshotEmpty. --- src/watchdog/utils/dirsnapshot.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/watchdog/utils/dirsnapshot.py b/src/watchdog/utils/dirsnapshot.py index a435c4fe1..1a40ff3a9 100644 --- a/src/watchdog/utils/dirsnapshot.py +++ b/src/watchdog/utils/dirsnapshot.py @@ -42,6 +42,10 @@ :members: :show-inheritance: +.. autoclass:: DirectorySnapshotEmpty + :members: + :show-inheritance: + """ import errno From 2872dc61075a6e0f038be93df0f410ed88988a0b Mon Sep 17 00:00:00 2001 From: Ajordat Date: Sun, 19 Jan 2020 09:50:30 +0100 Subject: [PATCH 4/7] Changed class name from DirectorySnapshotEmpty to EmptyDirectorySnapshot. --- src/watchdog/utils/dirsnapshot.py | 2 +- tests/test_snapshot_diff.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/watchdog/utils/dirsnapshot.py b/src/watchdog/utils/dirsnapshot.py index 1a40ff3a9..d35800ef3 100644 --- a/src/watchdog/utils/dirsnapshot.py +++ b/src/watchdog/utils/dirsnapshot.py @@ -353,7 +353,7 @@ def __repr__(self): return str(self._stat_info) -class DirectorySnapshotEmpty(object): +class EmptyDirectorySnapshot(object): """Class to implement an empty snapshot. This is used together with DirectorySnapshot and DirectorySnapshotDiff in order to get all the files/folders in the directory as created. diff --git a/tests/test_snapshot_diff.py b/tests/test_snapshot_diff.py index c4d3c7eb0..c912fc1fe 100644 --- a/tests/test_snapshot_diff.py +++ b/tests/test_snapshot_diff.py @@ -21,7 +21,7 @@ from watchdog.utils.dirsnapshot import DirectorySnapshot from watchdog.utils.dirsnapshot import DirectorySnapshotDiff -from watchdog.utils.dirsnapshot import DirectorySnapshotEmpty +from watchdog.utils.dirsnapshot import EmptyDirectorySnapshot from watchdog.utils import platform from .shell import mkdir, touch, mv, rm @@ -212,7 +212,7 @@ def test_empty_snapshot(p): touch(p('a')) mkdir(p('b', 'c'), parents=True) ref = DirectorySnapshot(p('')) - empty = DirectorySnapshotEmpty() + empty = EmptyDirectorySnapshot() diff = DirectorySnapshotDiff(empty, ref) assert diff.files_created == [p('a')] From abe8c85acf553274e8182e4fbf93b02e57a9d013 Mon Sep 17 00:00:00 2001 From: Ajordat Date: Sun, 19 Jan 2020 10:06:26 +0100 Subject: [PATCH 5/7] Added documentation. --- src/watchdog/utils/dirsnapshot.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/watchdog/utils/dirsnapshot.py b/src/watchdog/utils/dirsnapshot.py index d35800ef3..8d5a5e6b7 100644 --- a/src/watchdog/utils/dirsnapshot.py +++ b/src/watchdog/utils/dirsnapshot.py @@ -361,8 +361,21 @@ class EmptyDirectorySnapshot(object): @staticmethod def path(_): + """Mock up method to return the path of the received inode. As the snapshot + is intended to be empty, it always returns None. + + :returns: + None. + """ return None @property def paths(self): + """ + Mock up method to return a set of file/directory paths in the snapshot. As the + snapshot is intended to be empty, it always returns an empty set. + + :returns: + An empty set. + """ return set() From bccd603e08ee5fc726dce8113ad9518e862e1f65 Mon Sep 17 00:00:00 2001 From: Ajordat Date: Sun, 19 Jan 2020 10:06:53 +0100 Subject: [PATCH 6/7] Small doc fix. --- src/watchdog/utils/dirsnapshot.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/watchdog/utils/dirsnapshot.py b/src/watchdog/utils/dirsnapshot.py index 8d5a5e6b7..918eaf0ec 100644 --- a/src/watchdog/utils/dirsnapshot.py +++ b/src/watchdog/utils/dirsnapshot.py @@ -371,9 +371,8 @@ def path(_): @property def paths(self): - """ - Mock up method to return a set of file/directory paths in the snapshot. As the - snapshot is intended to be empty, it always returns an empty set. + """Mock up method to return a set of file/directory paths in the snapshot. As + the snapshot is intended to be empty, it always returns an empty set. :returns: An empty set. From f22e7045dccb06d80c6cefa34b41f5b776de3636 Mon Sep 17 00:00:00 2001 From: Ajordat Date: Sun, 19 Jan 2020 10:44:05 +0100 Subject: [PATCH 7/7] Updated changelog.rst. --- changelog.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.rst b/changelog.rst index 8b67fbd6e..996ea113c 100644 --- a/changelog.rst +++ b/changelog.rst @@ -11,8 +11,8 @@ Changelog Other Changes ============= -- -- Thanks to our beloved contributors: +- [snapshot] Added EmptyDirectorySnapshot (`#613 `__) +- Thanks to our beloved contributors: @Ajordat 0.10.0