adds exclude_deleted flag to find()

This commit is contained in:
Richard O'Dwyer 2019-10-31 22:17:48 +00:00
parent 5ec9e78c42
commit 5261751dc3
4 changed files with 49 additions and 2 deletions

View file

@ -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)

View file

@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -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):

View file

@ -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: