From 5261751dc32c060df3f1b8713a62c21845eed1df Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 31 Oct 2019 22:17:48 +0000 Subject: [PATCH] adds exclude_deleted flag to find() --- HISTORY.rst | 1 + README.rst | 9 +++++++++ src/mega/mega.py | 14 +++++++++++++- src/tests/tests.py | 27 ++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 2336f54..04fe363 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -11,6 +11,7 @@ Release History - Adds ``export()`` method to share a file or folder, returning public share URL with key. - Adds code, message attrs to RequestError exception, makes message in raised exceptions include more details. - Alters ``create_folder()`` to accept a path including multiple sub directories, adds support to create them all (similar to 'mkdir -p' on unix systems). +- Adds ``exclude_deleted=True`` optional arg to ``find()`` method, to exclude deleted nodes from results. 0.9.20 (2019-10-17) diff --git a/README.rst b/README.rst index 37267a4..1a3327a 100644 --- a/README.rst +++ b/README.rst @@ -116,6 +116,15 @@ Upload a file, and get its public link m.get_upload_link(file) # see mega.py for destination and filename options +Fine a file or folder +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: python + + folder = m.find('my_mega_folder') + # Excludes results which are in the Trash folder (i.e. deleted) + folder = m.find('my_mega_folder', exclude_deleted=True) + Upload a file to a destination folder ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/mega/mega.py b/src/mega/mega.py index 9e809f6..d544c8e 100644 --- a/src/mega/mega.py +++ b/src/mega/mega.py @@ -32,6 +32,7 @@ class Mega(object): self.sid = None self.sequence_num = random.randint(0, 0xFFFFFFFF) self.request_id = make_id(10) + self._trash_folder_node_id = None if options is None: options = {} @@ -42,6 +43,7 @@ class Mega(object): self._login_user(email, password) else: self.login_anonymous() + self._trash_folder_node_id = self.get_node_by_type(4)[0] return self def _login_user(self, email, password): @@ -291,7 +293,7 @@ class Mega(object): return None return parent_desc - def find(self, filename=None, handle=None): + def find(self, filename=None, handle=None, exclude_deleted=False): """ Return file object from given filename """ @@ -312,11 +314,21 @@ class Mega(object): file[1]['a'] and file[1]['a']['n'] == filename and parent_node_id == file[1]['p'] ): + if ( + exclude_deleted and + self._trash_folder_node_id == file[1]['p'] + ): + continue return file if ( filename and file[1]['a'] and file[1]['a']['n'] == filename ): + if ( + exclude_deleted and + self._trash_folder_node_id == file[1]['p'] + ): + continue return file def get_files(self): diff --git a/src/tests/tests.py b/src/tests/tests.py index b1de64f..a31f608 100644 --- a/src/tests/tests.py +++ b/src/tests/tests.py @@ -97,7 +97,7 @@ class TestExport: mega.upload( __file__, dest=dest_node_id, dest_filename='test.py' ) - path = '{}/test.py'.format(folder_name) + path = f'{folder_name}/test.py' assert mega.find(path) for _ in range(2): @@ -133,6 +133,31 @@ class TestCreateFolder: } +class TestFind: + + def test_find_file(self, mega, folder_name): + folder = mega.find(folder_name) + dest_node_id = folder[1]['h'] + mega.upload( + __file__, dest=dest_node_id, dest_filename='test.py' + ) + path = f'{folder_name}/test.py' + + assert mega.find(path) + + def test_path_not_found_returns_none(self, mega): + assert mega.find('not_found') is None + + def test_exclude_deleted_files(self, mega, folder_name): + folder_node_id = mega.find(folder_name)[0] + assert mega.find(folder_name) + + mega.delete(folder_node_id) + + assert mega.find(folder_name) + assert not mega.find(folder_name, exclude_deleted=True) + + def test_rename(mega, folder_name): file = mega.find(folder_name) if file: