From 059f819c9c90570cd1fefbb2a5a454b6848a148c Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 23 Mar 2020 11:43:51 -0700 Subject: [PATCH 01/12] Use """ instead of ''' for docstrings. --- src/mega/crypto.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mega/crypto.py b/src/mega/crypto.py index 2c60cfb..7737712 100644 --- a/src/mega/crypto.py +++ b/src/mega/crypto.py @@ -104,11 +104,11 @@ def str_to_a32(b): def mpi_to_int(s): - ''' + """ A Multi-precision integer is encoded as a series of bytes in big-endian order. The first two bytes are a header which tell the number of bits in the integer. The rest of the bytes are the integer. - ''' + """ return int(binascii.hexlify(s[2:]), 16) def extended_gcd(a, b): @@ -119,10 +119,10 @@ def extended_gcd(a, b): return (g, x - (b // a) * y, y) def modular_inverse(a, m): - ''' + """ Thank you Mart Bakhoff for this solution. https://stackoverflow.com/a/9758173 - ''' + """ g, x, y = extended_gcd(a, m) if g != 1: raise Exception('modular inverse does not exist') From fa6d1149d43076a5a71ace251139f1631aaa8f2d Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Mon, 23 Mar 2020 19:00:06 +0000 Subject: [PATCH 02/12] linting --- setup.cfg | 2 +- src/mega/crypto.py | 9 ++++----- src/mega/mega.py | 44 +++++++++++++++++++++++--------------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/setup.cfg b/setup.cfg index 876d1db..dc73dce 100644 --- a/setup.cfg +++ b/setup.cfg @@ -11,7 +11,7 @@ norecursedirs = .git [flake8] exclude = .git,__pycache__,legacy,build,dist,.tox max-complexity = 15 -ignore = E741,W504 +ignore = E741,W504,W503 [yapf] based_on_style = pep8 diff --git a/src/mega/crypto.py b/src/mega/crypto.py index 7737712..49f3e01 100644 --- a/src/mega/crypto.py +++ b/src/mega/crypto.py @@ -111,6 +111,7 @@ def mpi_to_int(s): """ return int(binascii.hexlify(s[2:]), 16) + def extended_gcd(a, b): if a == 0: return (b, 0, 1) @@ -118,19 +119,17 @@ def extended_gcd(a, b): g, y, x = extended_gcd(b % a, a) return (g, x - (b // a) * y, y) + def modular_inverse(a, m): - """ - Thank you Mart Bakhoff for this solution. - https://stackoverflow.com/a/9758173 - """ g, x, y = extended_gcd(a, m) if g != 1: raise Exception('modular inverse does not exist') else: return x % m + def base64_url_decode(data): - data += '==' [(2 - len(data) * 3) % 4:] + data += '=='[(2 - len(data) * 3) % 4:] for search, replace in (('-', '+'), ('_', '/'), (',', '')): data = data.replace(search, replace) return base64.b64decode(data) diff --git a/src/mega/mega.py b/src/mega/mega.py index 93b5fed..83dc983 100644 --- a/src/mega/mega.py +++ b/src/mega/mega.py @@ -303,9 +303,8 @@ class Mega: if foldername != '': for file in files.items(): if ( - file[1]['a'] and - file[1]['t'] and - file[1]['a']['n'] == foldername + file[1]['a'] and file[1]['t'] + and file[1]['a']['n'] == foldername ): if parent_desc == file[1]['p']: parent_desc = file[0] @@ -333,23 +332,20 @@ class Mega: parent_dir_name, files=files ) if ( - filename and parent_node_id and - file[1]['a'] and file[1]['a']['n'] == filename and - parent_node_id == file[1]['p'] + filename and parent_node_id and 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'] + 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 (filename and file[1]['a'] and file[1]['a']['n'] == filename): if ( - exclude_deleted and - self._trash_folder_node_id == file[1]['p'] + exclude_deleted + and self._trash_folder_node_id == file[1]['p'] ): continue return file @@ -598,13 +594,13 @@ class Mega: def _export_file(self, node): node_data = self._node_data(node) - self._api_request([ - { + self._api_request( + [{ 'a': 'l', 'n': node_data['h'], 'i': self.request_id - } - ]) + }] + ) return self.get_link(node) def export(self, path=None, node_id=None): @@ -627,7 +623,9 @@ class Mega: master_key_cipher = AES.new(a32_to_str(self.master_key), AES.MODE_ECB) ha = base64_url_encode( - master_key_cipher.encrypt(node_data['h'].encode("utf8") + node_data['h'].encode("utf8")) + master_key_cipher.encrypt( + node_data['h'].encode("utf8") + node_data['h'].encode("utf8") + ) ) share_key = secrets.token_bytes(16) @@ -745,7 +743,9 @@ class Mega: aes = AES.new(k_str, AES.MODE_CTR, counter=counter) mac_str = '\0' * 16 - mac_encryptor = AES.new(k_str, AES.MODE_CBC, mac_str.encode("utf8")) + mac_encryptor = AES.new( + k_str, AES.MODE_CBC, mac_str.encode("utf8") + ) iv_str = a32_to_str([iv[0], iv[1], iv[0], iv[1]]) for chunk_start, chunk_size in get_chunks(file_size): @@ -808,7 +808,9 @@ class Mega: completion_file_handle = None mac_str = '\0' * 16 - mac_encryptor = AES.new(k_str, AES.MODE_CBC, mac_str.encode("utf8")) + mac_encryptor = AES.new( + k_str, AES.MODE_CBC, mac_str.encode("utf8") + ) iv_str = a32_to_str([ul_key[4], ul_key[5], ul_key[4], ul_key[5]]) if file_size > 0: for chunk_start, chunk_size in get_chunks(file_size): From ecf42c1b35bf389844d7da048ca1708a926ecd11 Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Wed, 25 Mar 2020 20:09:37 +0000 Subject: [PATCH 03/12] Preparing release 1.0.7 --- HISTORY.rst | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 6df5e4f..250c6b9 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,7 +3,7 @@ Release History =============== -1.0.7 (unreleased) +1.0.7 (2020-03-25) ------------------ - Fix login by calculating public RSA exponent instead of hardcoding. diff --git a/setup.py b/setup.py index 293af25..51718eb 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ with open('HISTORY.rst', 'r', encoding='utf-8') as hist_file: setup( name='mega.py', - version='1.0.7.dev0', + version='1.0.7', packages=find_packages('src', exclude=('tests', )), package_dir={'': 'src'}, include_package_data=True, From 631ca606ff1570f0c2da8d69a24effdd27859d3e Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Tue, 28 Apr 2020 17:15:12 +0100 Subject: [PATCH 04/12] link to the website --- README.rst | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 3bf001c..d679648 100644 --- a/README.rst +++ b/README.rst @@ -11,7 +11,7 @@ Mega.py |Build Status| |Downloads| -Python library for the Mega.co.nz API, currently supporting: +Python library for the `Mega.co.nz `_ API, currently supporting: - login - uploading @@ -29,6 +29,14 @@ For more detailed information see API_INFO.md How To Use ---------- +.. _create-mega-account: + +Create a Mega account +~~~~~~~~~~~~~~~~~~~~~~~ + +First, `create an account with Mega `_ +. + .. _install-megapy-package: Install mega.py package @@ -36,7 +44,7 @@ Install mega.py package .. code:: python - #Run the following command, or run setup from the latest github source + Run the following command, or run setup from the latest github source pip install mega.py .. _import-megapy: From aa89af0324f08fcf3a10b712b0a0dd9ddcc2d40f Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Sun, 21 Jun 2020 16:18:22 +0100 Subject: [PATCH 05/12] Updates URL parsing, updates API error code handling --- requirements-dev.txt | 35 +++++++++++++++------------ src/mega/errors.py | 16 +++++------- src/mega/mega.py | 55 ++++++++++++++++++++++++++++++------------ src/tests/test_mega.py | 50 +++++++++++++++++++++++++++----------- 4 files changed, 101 insertions(+), 55 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index a03b408..0ee5db6 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,17 +1,20 @@ -r requirements.txt -pytest -ipdb -flake8 -pep8-naming -autoflake -mccabe -yapf -tox -coverage -pytest-cov -zest.releaser -setuptools -twine -wheel -rope -pytest-mock +pytest==5.4.3 +ipdb==0.13.2 +flake8==3.8.3 +pep8-naming==0.11.1 +autoflake==1.3.1 +mccabe==0.6.1 +yapf==0.30.0 +tox==3.15.2 +coverage==5.1 +pytest-cov==2.10.0 +zest.releaser==6.20.1 +setuptools==47.3.1 +twine==3.1.1 +wheel==0.34.2 +rope==0.17.0 +pytest-mock==3.1.1 +brunette==0.1.5 +lock-requirements==0.1.1 +requests-mock diff --git a/src/mega/errors.py b/src/mega/errors.py index 4df7f90..218a935 100644 --- a/src/mega/errors.py +++ b/src/mega/errors.py @@ -6,17 +6,16 @@ class ValidationError(Exception): _CODE_TO_DESCRIPTIONS = { + 0: ('UNKNOWN', 'API Returned 0'), -1: ( - 'EINTERNAL', - ( + 'EINTERNAL', ( 'An internal error has occurred. Please submit a bug report, ' 'detailing the exact circumstances in which this error occurred' ) ), -2: ('EARGS', 'You have passed invalid arguments to this command'), -3: ( - 'EAGAIN', - ( + 'EAGAIN', ( '(always at the request level) A temporary congestion or server ' 'malfunction prevented your request from being processed. ' 'No data was altered. Retry. Retries must be spaced with ' @@ -24,8 +23,7 @@ _CODE_TO_DESCRIPTIONS = { ) ), -4: ( - 'ERATELIMIT', - ( + 'ERATELIMIT', ( 'You have exceeded your command weight per time quota. Please ' 'wait a few seconds, then try again (this should never happen ' 'in sane real-life applications)' @@ -37,15 +35,13 @@ _CODE_TO_DESCRIPTIONS = { 'Too many concurrent IP addresses are accessing this upload target URL' ), -7: ( - 'ERANGE', - ( + 'ERANGE', ( 'The upload file packet is out of range or not starting and ' 'ending on a chunk boundary' ) ), -8: ( - 'EEXPIRED', - ( + 'EEXPIRED', ( 'The upload target URL you are trying to access has expired. ' 'Please request a fresh one' ) diff --git a/src/mega/mega.py b/src/mega/mega.py index 83dc983..092962a 100644 --- a/src/mega/mega.py +++ b/src/mega/mega.py @@ -174,24 +174,41 @@ class Mega: data = [data] url = f'{self.schema}://g.api.{self.domain}/cs' - req = requests.post( + response = requests.post( url, params=params, data=json.dumps(data), timeout=self.timeout, ) - json_resp = json.loads(req.text) - if isinstance(json_resp, int): - if json_resp == -3: + json_resp = json.loads(response.text) + try: + if isinstance(json_resp, list): + int_resp = json_resp[0] if isinstance( + json_resp[0], int + ) else None + elif isinstance(json_resp, int): + int_resp = json_resp + except IndexError: + int_resp = None + if int_resp is not None: + if int_resp == -3: msg = 'Request failed, retrying' logger.info(msg) raise RuntimeError(msg) - raise RequestError(json_resp) + raise RequestError(int_resp) return json_resp[0] def _parse_url(self, url): - # parse file id and key from url - if '!' in url: + """Parse file id and key from url.""" + if '/file/' in url: + # V2 URL structure + url = url.replace(' ', '') + file_id = re.findall(r'\W\w\w\w\w\w\w\w\w\W', url)[0][1:-1] + id_index = re.search(file_id, url).end() + key = url[id_index + 1:] + return f'{file_id}!{key}' + elif '!' in url: + # V1 URL structure match = re.findall(r'/#!(.*)', url) path = match[0] return path @@ -874,9 +891,12 @@ class Mega: # update attributes data = self._api_request( { - 'a': 'p', - 't': dest, - 'i': self.request_id, + 'a': + 'p', + 't': + dest, + 'i': + self.request_id, 'n': [ { 'h': completion_file_handle, @@ -902,8 +922,10 @@ class Mega: # update attributes data = self._api_request( { - 'a': 'p', - 't': parent_node_id, + 'a': + 'p', + 't': + parent_node_id, 'n': [ { 'h': 'xxxxxxxx', @@ -912,7 +934,8 @@ class Mega: 'k': encrypted_key } ], - 'i': self.request_id + 'i': + self.request_id } ) return data @@ -1102,8 +1125,10 @@ class Mega: encrypted_name = base64_url_encode(encrypt_attr({'n': dest_name}, k)) return self._api_request( { - 'a': 'p', - 't': dest_node['h'], + 'a': + 'p', + 't': + dest_node['h'], 'n': [ { 'ph': file_handle, diff --git a/src/tests/test_mega.py b/src/tests/test_mega.py index 48ed6e7..5c36e15 100644 --- a/src/tests/test_mega.py +++ b/src/tests/test_mega.py @@ -2,6 +2,7 @@ import random from pathlib import Path import os +import requests_mock import pytest from mega import Mega @@ -11,6 +12,7 @@ TEST_PUBLIC_URL = ( 'https://mega.nz/#!hYVmXKqL!r0d0-WRnFwulR_shhuEDwrY1Vo103-am1MyUy8oV6Ps' ) TEST_FILE = os.path.basename(__file__) +MODULE = 'mega.mega' @pytest.fixture @@ -32,9 +34,7 @@ def mega(folder_name): def uploaded_file(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' - ) + mega.upload(__file__, dest=dest_node_id, dest_filename='test.py') path = f'{folder_name}/test.py' return mega.find(path) @@ -73,7 +73,6 @@ def test_get_link(mega, uploaded_file): class TestExport: - def test_export_folder(self, mega, folder_name): public_url = None for _ in range(2): @@ -103,9 +102,7 @@ class TestExport: # Upload a single file into a folder folder = mega.find(folder_name) dest_node_id = folder[1]['h'] - mega.upload( - __file__, dest=dest_node_id, dest_filename='test.py' - ) + mega.upload(__file__, dest=dest_node_id, dest_filename='test.py') path = f'{folder_name}/test.py' assert mega.find(path) @@ -143,13 +140,10 @@ 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' - ) + mega.upload(__file__, dest=dest_node_id, dest_filename='test.py') path = f'{folder_name}/test.py' assert mega.find(path) @@ -194,9 +188,7 @@ def test_download(mega, tmpdir, folder_name): # Upload a single file into a folder folder = mega.find(folder_name) dest_node_id = folder[1]['h'] - mega.upload( - __file__, dest=dest_node_id, dest_filename='test.py' - ) + mega.upload(__file__, dest=dest_node_id, dest_filename='test.py') path = f'{folder_name}/test.py' file = mega.find(path) @@ -222,3 +214,33 @@ def test_add_contact(mega): def test_remove_contact(mega): resp = mega.remove_contact(TEST_CONTACT) assert isinstance(resp, int) + + +@pytest.mark.parametrize( + 'url, expected_file_id_and_key', [ + ( + 'https://mega.nz/#!Ue5VRSIQ!kC2E4a4JwfWWCWYNJovGFHlbz8F' + 'N-ISsBAGPzvTjT6k', + 'Ue5VRSIQ!kC2E4a4JwfWWCWYNJovGFHlbz8FN-ISsBAGPzvTjT6k' + ), + ( + 'https://mega.nz/file/cH51DYDR#qH7QOfRcM-7N9riZWdSjsRq' + '5VDTLfIhThx1capgVA30', + 'cH51DYDR!qH7QOfRcM-7N9riZWdSjsRq5VDTLfIhThx1capgVA30' + ), + ] +) +def test_parse_url(url, expected_file_id_and_key, mega): + assert mega._parse_url(url) == expected_file_id_and_key + + +class TestAPIRequest: + @pytest.mark.parametrize('response_text', ['-3', '-9']) + def test_when_api_returns_int_raises_exception( + self, mega, response_text, + ): + with requests_mock.Mocker() as m: + m.post( + f'{mega.schema}://g.api.{mega.domain}/cs', text=response_text + ) + mega._api_request(data={}) From 781e3a267bdff2c9bdb7815539e75ffc1325e61c Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 25 Jun 2020 21:03:16 +0100 Subject: [PATCH 06/12] various fixes --- examples.py | 8 +- requirements-dev.txt | 8 +- setup.cfg | 16 +- setup.py | 46 ++-- src/mega/crypto.py | 10 +- src/mega/errors.py | 1 - src/mega/mega.py | 541 +++++++++++++++++---------------------- src/tests/test_crypto.py | 38 +-- src/tests/test_errors.py | 9 +- src/tests/test_mega.py | 44 ++-- 10 files changed, 299 insertions(+), 422 deletions(-) diff --git a/examples.py b/examples.py index fefd64b..48861e2 100644 --- a/examples.py +++ b/examples.py @@ -1,4 +1,5 @@ import os +import uuid from mega import Mega @@ -8,7 +9,7 @@ def test(): comment/uncomment lines to test various parts of the API see readme.md for more information """ - + unique = str(uuid.uuid4()) # user details email = os.environ['EMAIL'] password = os.environ['PASS'] @@ -36,10 +37,11 @@ def test(): print((files[file])) # upload file - print((m.upload('examples.py'))) + print((m.upload(filename='examples.py', + dest_filename=f'examples_{unique}.py'))) # search for a file in account - file = m.find('examples.py') + file = m.find(f'examples_{unique}.py') if file: # get public link diff --git a/requirements-dev.txt b/requirements-dev.txt index 0ee5db6..9af2321 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,20 +1,20 @@ -r requirements.txt pytest==5.4.3 -ipdb==0.13.2 +ipdb==0.13.3 flake8==3.8.3 pep8-naming==0.11.1 autoflake==1.3.1 mccabe==0.6.1 -yapf==0.30.0 +brunette==0.1.5 tox==3.15.2 coverage==5.1 pytest-cov==2.10.0 zest.releaser==6.20.1 setuptools==47.3.1 -twine==3.1.1 +twine==3.2.0 wheel==0.34.2 rope==0.17.0 pytest-mock==3.1.1 brunette==0.1.5 lock-requirements==0.1.1 -requests-mock +requests-mock==1.8.0 diff --git a/setup.cfg b/setup.cfg index dc73dce..b55b071 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,18 +13,10 @@ exclude = .git,__pycache__,legacy,build,dist,.tox max-complexity = 15 ignore = E741,W504,W503 -[yapf] -based_on_style = pep8 -spaces_before_comment = 2 -split_before_logical_operator = true -indent_width = 4 -split_complex_comprehension = true -column_limit = 79 -dedent_closing_brackets = true -spaces_around_power_operator = true -no_spaces_around_selected_binary_operators = false -split_penalty_import_names = 500 -join_multiple_lines = true +[tool:brunette] +line-length = 79 +verbose = true +single-quotes = true [coverage:run] omit = diff --git a/setup.py b/setup.py index 51718eb..a339e95 100644 --- a/setup.py +++ b/setup.py @@ -20,27 +20,25 @@ with open('README.rst', 'r', encoding='utf-8') as rm_file: with open('HISTORY.rst', 'r', encoding='utf-8') as hist_file: history = hist_file.read() -setup( - name='mega.py', - version='1.0.7', - packages=find_packages('src', exclude=('tests', )), - package_dir={'': 'src'}, - include_package_data=True, - zip_safe=False, - description='Python lib for the Mega.co.nz API', - long_description=readme + '\n\n' + history, - author='Richard O\'Dwyer', - author_email='richard@richard.do', - license='Creative Commons Attribution-Noncommercial-Share Alike license', - install_requires=install_requires, - classifiers=[ - 'Intended Audience :: Developers', - 'Operating System :: OS Independent', - 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Topic :: Internet :: WWW/HTTP', - ] -) +setup(name='mega.py', + version='1.0.7', + packages=find_packages('src', exclude=('tests', )), + package_dir={'': 'src'}, + include_package_data=True, + zip_safe=False, + description='Python lib for the Mega.co.nz API', + long_description=readme + '\n\n' + history, + author='Richard O\'Dwyer', + author_email='richard@richard.do', + license='Creative Commons Attribution-Noncommercial-Share Alike license', + install_requires=install_requires, + classifiers=[ + 'Intended Audience :: Developers', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Topic :: Internet :: WWW/HTTP', + ]) diff --git a/src/mega/crypto.py b/src/mega/crypto.py index 49f3e01..61ddf15 100644 --- a/src/mega/crypto.py +++ b/src/mega/crypto.py @@ -65,15 +65,13 @@ def prepare_key(arr): def encrypt_key(a, key): - return sum( - (aes_cbc_encrypt_a32(a[i:i + 4], key) for i in range(0, len(a), 4)), () - ) + return sum((aes_cbc_encrypt_a32(a[i:i + 4], key) + for i in range(0, len(a), 4)), ()) def decrypt_key(a, key): - return sum( - (aes_cbc_decrypt_a32(a[i:i + 4], key) for i in range(0, len(a), 4)), () - ) + return sum((aes_cbc_decrypt_a32(a[i:i + 4], key) + for i in range(0, len(a), 4)), ()) def encrypt_attr(attr, key): diff --git a/src/mega/errors.py b/src/mega/errors.py index 218a935..bd24852 100644 --- a/src/mega/errors.py +++ b/src/mega/errors.py @@ -6,7 +6,6 @@ class ValidationError(Exception): _CODE_TO_DESCRIPTIONS = { - 0: ('UNKNOWN', 'API Returned 0'), -1: ( 'EINTERNAL', ( 'An internal error has occurred. Please submit a bug report, ' diff --git a/src/mega/mega.py b/src/mega/mega.py index 092962a..4b8c902 100644 --- a/src/mega/mega.py +++ b/src/mega/mega.py @@ -18,12 +18,11 @@ import requests from tenacity import retry, wait_exponential, retry_if_exception_type from .errors import ValidationError, RequestError -from .crypto import ( - a32_to_base64, encrypt_key, base64_url_encode, encrypt_attr, base64_to_a32, - base64_url_decode, decrypt_attr, a32_to_str, get_chunks, str_to_a32, - decrypt_key, mpi_to_int, stringhash, prepare_key, make_id, makebyte, - modular_inverse -) +from .crypto import (a32_to_base64, encrypt_key, base64_url_encode, + encrypt_attr, base64_to_a32, base64_url_decode, + decrypt_attr, a32_to_str, get_chunks, str_to_a32, + decrypt_key, mpi_to_int, stringhash, prepare_key, make_id, + makebyte, modular_inverse) logger = logging.getLogger(__name__) @@ -64,13 +63,11 @@ class Mega: user_hash = stringhash(email, password_aes) else: # v2 user account - pbkdf2_key = hashlib.pbkdf2_hmac( - hash_name='sha512', - password=password.encode(), - salt=a32_to_str(user_salt), - iterations=100000, - dklen=32 - ) + pbkdf2_key = hashlib.pbkdf2_hmac(hash_name='sha512', + password=password.encode(), + salt=a32_to_str(user_salt), + iterations=100000, + dklen=32) password_aes = str_to_a32(pbkdf2_key[:16]) user_hash = base64_url_encode(pbkdf2_key[-16:]) resp = self._api_request({'a': 'us', 'user': email, 'uh': user_hash}) @@ -84,20 +81,16 @@ class Mega: password_key = [random.randint(0, 0xFFFFFFFF)] * 4 session_self_challenge = [random.randint(0, 0xFFFFFFFF)] * 4 - user = self._api_request( - { - 'a': - 'up', - 'k': - a32_to_base64(encrypt_key(master_key, password_key)), - 'ts': - base64_url_encode( - a32_to_str(session_self_challenge) + a32_to_str( - encrypt_key(session_self_challenge, master_key) - ) - ) - } - ) + user = self._api_request({ + 'a': + 'up', + 'k': + a32_to_base64(encrypt_key(master_key, password_key)), + 'ts': + base64_url_encode( + a32_to_str(session_self_challenge) + + a32_to_str(encrypt_key(session_self_challenge, master_key))) + }) resp = self._api_request({'a': 'us', 'user': user}) if isinstance(resp, int): @@ -110,15 +103,13 @@ class Mega: if 'tsid' in resp: tsid = base64_url_decode(resp['tsid']) key_encrypted = a32_to_str( - encrypt_key(str_to_a32(tsid[:16]), self.master_key) - ) + encrypt_key(str_to_a32(tsid[:16]), self.master_key)) if key_encrypted == tsid[-16:]: self.sid = resp['tsid'] elif 'csid' in resp: encrypted_rsa_private_key = base64_to_a32(resp['privk']) - rsa_private_key = decrypt_key( - encrypted_rsa_private_key, self.master_key - ) + rsa_private_key = decrypt_key(encrypted_rsa_private_key, + self.master_key) private_key = a32_to_str(rsa_private_key) # The private_key contains 4 MPI integers concatenated together. @@ -158,10 +149,8 @@ class Mega: sid = binascii.unhexlify('0' + sid if len(sid) % 2 else sid) self.sid = base64_url_encode(sid[:43]) - @retry( - retry=retry_if_exception_type(RuntimeError), - wait=wait_exponential(multiplier=2, min=2, max=60) - ) + @retry(retry=retry_if_exception_type(RuntimeError), + wait=wait_exponential(multiplier=2, min=2, max=60)) def _api_request(self, data): params = {'id': self.sequence_num} self.sequence_num += 1 @@ -183,14 +172,15 @@ class Mega: json_resp = json.loads(response.text) try: if isinstance(json_resp, list): - int_resp = json_resp[0] if isinstance( - json_resp[0], int - ) else None + int_resp = json_resp[0] if isinstance(json_resp[0], + int) else None elif isinstance(json_resp, int): int_resp = json_resp except IndexError: int_resp = None if int_resp is not None: + if int_resp == 0: + return int_resp if int_resp == -3: msg = 'Request failed, retrying' logger.info(msg) @@ -218,10 +208,8 @@ class Mega: def _process_file(self, file, shared_keys): if file['t'] == 0 or file['t'] == 1: keys = dict( - keypart.split(':', 1) - for keypart in file['k'].split('/') - if ':' in keypart - ) + keypart.split(':', 1) for keypart in file['k'].split('/') + if ':' in keypart) uid = file['u'] key = None # my objects @@ -229,9 +217,8 @@ class Mega: key = decrypt_key(base64_to_a32(keys[uid]), self.master_key) # shared folders elif 'su' in file and 'sk' in file and ':' in file['k']: - shared_key = decrypt_key( - base64_to_a32(file['sk']), self.master_key - ) + shared_key = decrypt_key(base64_to_a32(file['sk']), + self.master_key) key = decrypt_key(base64_to_a32(keys[file['h']]), shared_key) if file['su'] not in shared_keys: shared_keys[file['su']] = {} @@ -247,17 +234,14 @@ class Mega: if file['h'] and file['h'] in shared_keys.get('EXP', ()): shared_key = shared_keys['EXP'][file['h']] encrypted_key = str_to_a32( - base64_url_decode(file['k'].split(':')[-1]) - ) + base64_url_decode(file['k'].split(':')[-1])) key = decrypt_key(encrypted_key, shared_key) file['shared_folder_key'] = shared_key if key is not None: # file if file['t'] == 0: - k = ( - key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], - key[3] ^ key[7] - ) + k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], + key[3] ^ key[7]) file['iv'] = key[4:6] + (0, 0) file['meta_mac'] = key[6:8] # folder @@ -292,9 +276,8 @@ class Mega: """ ok_dict = {} for ok_item in files['ok']: - shared_key = decrypt_key( - base64_to_a32(ok_item['k']), self.master_key - ) + shared_key = decrypt_key(base64_to_a32(ok_item['k']), + self.master_key) ok_dict[ok_item['h']] = shared_key for s_item in files['s']: if s_item['u'] not in shared_keys: @@ -319,10 +302,8 @@ class Mega: for foldername in paths: if foldername != '': for file in files.items(): - if ( - file[1]['a'] and file[1]['t'] - and file[1]['a']['n'] == foldername - ): + if (file[1]['a'] and file[1]['t'] + and file[1]['a']['n'] == foldername): if parent_desc == file[1]['p']: parent_desc = file[0] found = True @@ -345,27 +326,24 @@ class Mega: for file in list(files.items()): parent_node_id = None if parent_dir_name: - parent_node_id = self.find_path_descriptor( - parent_dir_name, files=files - ) - if ( - filename and parent_node_id and 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'] - ): + parent_node_id = self.find_path_descriptor(parent_dir_name, + files=files) + if (filename and parent_node_id and 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 + try: + 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 + except KeyError: + continue def get_files(self): logger.info('Getting all files...') @@ -390,17 +368,12 @@ class Mega: public_handle = self._api_request({'a': 'l', 'n': file['h']}) file_key = file['k'][file['k'].index(':') + 1:] decrypted_key = a32_to_base64( - decrypt_key(base64_to_a32(file_key), self.master_key) - ) - return ( - f'{self.schema}://{self.domain}' - f'/#!{public_handle}!{decrypted_key}' - ) + decrypt_key(base64_to_a32(file_key), self.master_key)) + return (f'{self.schema}://{self.domain}' + f'/#!{public_handle}!{decrypted_key}') else: - raise ValueError( - '''Upload() response required as input, - use get_link() for regular file input''' - ) + raise ValueError('''Upload() response required as input, + use get_link() for regular file input''') def get_link(self, file): """ @@ -410,15 +383,11 @@ class Mega: if 'h' in file and 'k' in file: public_handle = self._api_request({'a': 'l', 'n': file['h']}) if public_handle == -11: - raise RequestError( - "Can't get a public link from that file " - "(is this a shared file?)" - ) + raise RequestError("Can't get a public link from that file " + "(is this a shared file?)") decrypted_key = a32_to_base64(file['key']) - return ( - f'{self.schema}://{self.domain}' - f'/#!{public_handle}!{decrypted_key}' - ) + return (f'{self.schema}://{self.domain}' + f'/#!{public_handle}!{decrypted_key}') else: raise ValidationError('File id and key must be present') @@ -436,15 +405,11 @@ class Mega: if 'h' in file and 'k' in file: public_handle = self._api_request({'a': 'l', 'n': file['h']}) if public_handle == -11: - raise RequestError( - "Can't get a public link from that file " - "(is this a shared file?)" - ) + raise RequestError("Can't get a public link from that file " + "(is this a shared file?)") decrypted_key = a32_to_base64(file['shared_folder_key']) - return ( - f'{self.schema}://{self.domain}' - f'/#F!{public_handle}!{decrypted_key}' - ) + return (f'{self.schema}://{self.domain}' + f'/#F!{public_handle}!{decrypted_key}') else: raise ValidationError('File id and key must be present') @@ -507,14 +472,12 @@ class Mega: """ Get current remaining disk quota in MegaBytes """ - json_resp = self._api_request( - { - 'a': 'uq', - 'xfer': 1, - 'strg': 1, - 'v': 1 - } - ) + json_resp = self._api_request({ + 'a': 'uq', + 'xfer': 1, + 'strg': 1, + 'v': 1 + }) # convert bytes to megabyes return json_resp['mstrg'] / 1048576 @@ -568,13 +531,11 @@ class Mega: """ Destroy a file by its private id """ - return self._api_request( - { - 'a': 'd', - 'n': file_id, - 'i': self.request_id - } - ) + return self._api_request({ + 'a': 'd', + 'n': file_id, + 'i': self.request_id + }) def destroy_url(self, url): """ @@ -600,24 +561,20 @@ class Mega: """ Download a file by it's file object """ - return self._download_file( - file_handle=None, - file_key=None, - file=file[1], - dest_path=dest_path, - dest_filename=dest_filename, - is_public=False - ) + return self._download_file(file_handle=None, + file_key=None, + file=file[1], + dest_path=dest_path, + dest_filename=dest_filename, + is_public=False) def _export_file(self, node): node_data = self._node_data(node) - self._api_request( - [{ - 'a': 'l', - 'n': node_data['h'], - 'i': self.request_id - }] - ) + self._api_request([{ + 'a': 'l', + 'n': node_data['h'], + 'i': self.request_id + }]) return self.get_link(node) def export(self, path=None, node_id=None): @@ -640,10 +597,8 @@ class Mega: master_key_cipher = AES.new(a32_to_str(self.master_key), AES.MODE_ECB) ha = base64_url_encode( - master_key_cipher.encrypt( - node_data['h'].encode("utf8") + node_data['h'].encode("utf8") - ) - ) + master_key_cipher.encrypt(node_data['h'].encode("utf8") + + node_data['h'].encode("utf8"))) share_key = secrets.token_bytes(16) ok = base64_url_encode(master_key_cipher.encrypt(share_key)) @@ -651,24 +606,26 @@ class Mega: share_key_cipher = AES.new(share_key, AES.MODE_ECB) node_key = node_data['k'] encrypted_node_key = base64_url_encode( - share_key_cipher.encrypt(a32_to_str(node_key)) - ) + share_key_cipher.encrypt(a32_to_str(node_key))) node_id = node_data['h'] - request_body = [ - { - 'a': 's2', - 'n': node_id, - 's': [{ - 'u': 'EXP', - 'r': 0 - }], - 'i': self.request_id, - 'ok': ok, - 'ha': ha, - 'cr': [[node_id], [node_id], [0, 0, encrypted_node_key]] - } - ] + request_body = [{ + 'a': + 's2', + 'n': + node_id, + 's': [{ + 'u': 'EXP', + 'r': 0 + }], + 'i': + self.request_id, + 'ok': + ok, + 'ha': + ha, + 'cr': [[node_id], [node_id], [0, 0, encrypted_node_key]] + }] self._api_request(request_body) nodes = self.get_files() return self.get_folder_link(nodes[node_id]) @@ -688,38 +645,30 @@ class Mega: is_public=True, ) - def _download_file( - self, - file_handle, - file_key, - dest_path=None, - dest_filename=None, - is_public=False, - file=None - ): + def _download_file(self, + file_handle, + file_key, + dest_path=None, + dest_filename=None, + is_public=False, + file=None): if file is None: if is_public: file_key = base64_to_a32(file_key) - file_data = self._api_request( - { - 'a': 'g', - 'g': 1, - 'p': file_handle - } - ) + file_data = self._api_request({ + 'a': 'g', + 'g': 1, + 'p': file_handle + }) else: - file_data = self._api_request( - { - 'a': 'g', - 'g': 1, - 'n': file_handle - } - ) + file_data = self._api_request({ + 'a': 'g', + 'g': 1, + 'n': file_handle + }) - k = ( - file_key[0] ^ file_key[4], file_key[1] ^ file_key[5], - file_key[2] ^ file_key[6], file_key[3] ^ file_key[7] - ) + k = (file_key[0] ^ file_key[4], file_key[1] ^ file_key[5], + file_key[2] ^ file_key[6], file_key[3] ^ file_key[7]) iv = file_key[4:6] + (0, 0) meta_mac = file_key[6:8] else: @@ -750,19 +699,17 @@ class Mega: else: dest_path += '/' - with tempfile.NamedTemporaryFile( - mode='w+b', prefix='megapy_', delete=False - ) as temp_output_file: + with tempfile.NamedTemporaryFile(mode='w+b', + prefix='megapy_', + delete=False) as temp_output_file: k_str = a32_to_str(k) - counter = Counter.new( - 128, initial_value=((iv[0] << 32) + iv[1]) << 64 - ) + counter = Counter.new(128, + initial_value=((iv[0] << 32) + iv[1]) << 64) aes = AES.new(k_str, AES.MODE_CTR, counter=counter) mac_str = '\0' * 16 - mac_encryptor = AES.new( - k_str, AES.MODE_CBC, mac_str.encode("utf8") - ) + mac_encryptor = AES.new(k_str, AES.MODE_CBC, + mac_str.encode("utf8")) iv_str = a32_to_str([iv[0], iv[1], iv[0], iv[1]]) for chunk_start, chunk_size in get_chunks(file_size): @@ -787,14 +734,12 @@ class Mega: mac_str = mac_encryptor.encrypt(encryptor.encrypt(block)) file_info = os.stat(temp_output_file.name) - logger.info( - '%s of %s downloaded', file_info.st_size, file_size - ) + logger.info('%s of %s downloaded', file_info.st_size, + file_size) file_mac = str_to_a32(mac_str) # check mac integrity - if ( - file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3] - ) != meta_mac: + if (file_mac[0] ^ file_mac[1], + file_mac[2] ^ file_mac[3]) != meta_mac: raise ValueError('Mismatched mac') output_path = Path(dest_path + file_name) shutil.move(temp_output_file.name, output_path) @@ -817,17 +762,15 @@ class Mega: ul_key = [random.randint(0, 0xFFFFFFFF) for _ in range(6)] k_str = a32_to_str(ul_key[:4]) count = Counter.new( - 128, initial_value=((ul_key[4] << 32) + ul_key[5]) << 64 - ) + 128, initial_value=((ul_key[4] << 32) + ul_key[5]) << 64) aes = AES.new(k_str, AES.MODE_CTR, counter=count) upload_progress = 0 completion_file_handle = None mac_str = '\0' * 16 - mac_encryptor = AES.new( - k_str, AES.MODE_CBC, mac_str.encode("utf8") - ) + mac_encryptor = AES.new(k_str, AES.MODE_CBC, + mac_str.encode("utf8")) iv_str = a32_to_str([ul_key[4], ul_key[5], ul_key[4], ul_key[5]]) if file_size > 0: for chunk_start, chunk_size in get_chunks(file_size): @@ -852,19 +795,17 @@ class Mega: # encrypt file and upload chunk = aes.encrypt(chunk) - output_file = requests.post( - ul_url + "/" + str(chunk_start), - data=chunk, - timeout=self.timeout - ) + output_file = requests.post(ul_url + "/" + + str(chunk_start), + data=chunk, + timeout=self.timeout) completion_file_handle = output_file.text - logger.info( - '%s of %s uploaded', upload_progress, file_size - ) + logger.info('%s of %s uploaded', upload_progress, + file_size) else: - output_file = requests.post( - ul_url + "/0", data='', timeout=self.timeout - ) + output_file = requests.post(ul_url + "/0", + data='', + timeout=self.timeout) completion_file_handle = output_file.text logger.info('Chunks uploaded') @@ -879,8 +820,7 @@ class Mega: attribs = {'n': dest_filename} encrypt_attribs = base64_url_encode( - encrypt_attr(attribs, ul_key[:4]) - ) + encrypt_attr(attribs, ul_key[:4])) key = [ ul_key[0] ^ ul_key[4], ul_key[1] ^ ul_key[5], ul_key[2] ^ meta_mac[0], ul_key[3] ^ meta_mac[1], ul_key[4], @@ -889,24 +829,20 @@ class Mega: encrypted_key = a32_to_base64(encrypt_key(key, self.master_key)) logger.info('Sending request to update attributes') # update attributes - data = self._api_request( - { - 'a': - 'p', - 't': - dest, - 'i': - self.request_id, - 'n': [ - { - 'h': completion_file_handle, - 't': 0, - 'a': encrypt_attribs, - 'k': encrypted_key - } - ] - } - ) + data = self._api_request({ + 'a': + 'p', + 't': + dest, + 'i': + self.request_id, + 'n': [{ + 'h': completion_file_handle, + 't': 0, + 'a': encrypt_attribs, + 'k': encrypted_key + }] + }) logger.info('Upload complete') return data @@ -920,24 +856,20 @@ class Mega: encrypted_key = a32_to_base64(encrypt_key(ul_key[:4], self.master_key)) # update attributes - data = self._api_request( - { - 'a': - 'p', - 't': - parent_node_id, - 'n': [ - { - 'h': 'xxxxxxxx', - 't': 1, - 'a': encrypt_attribs, - 'k': encrypted_key - } - ], - 'i': - self.request_id - } - ) + data = self._api_request({ + 'a': + 'p', + 't': + parent_node_id, + 'n': [{ + 'h': 'xxxxxxxx', + 't': 1, + 'a': encrypt_attribs, + 'k': encrypted_key + }], + 'i': + self.request_id + }) return data def _root_node_id(self): @@ -960,9 +892,8 @@ class Mega: parent_node_id = dest else: parent_node_id = folder_node_ids[idx - 1] - created_node = self._mkdir( - name=directory_name, parent_node_id=parent_node_id - ) + created_node = self._mkdir(name=directory_name, + parent_node_id=parent_node_id) node_id = created_node['f'][0]['h'] folder_node_ids[idx] = node_id return dict(zip(dirs, folder_node_ids.values())) @@ -973,21 +904,16 @@ class Mega: attribs = {'n': new_name} # encrypt attribs encrypt_attribs = base64_url_encode(encrypt_attr(attribs, file['k'])) - encrypted_key = a32_to_base64( - encrypt_key(file['key'], self.master_key) - ) + encrypted_key = a32_to_base64(encrypt_key(file['key'], + self.master_key)) # update attributes - return self._api_request( - [ - { - 'a': 'a', - 'attr': encrypt_attribs, - 'key': encrypted_key, - 'n': file['h'], - 'i': self.request_id - } - ] - ) + return self._api_request([{ + 'a': 'a', + 'attr': encrypt_attribs, + 'key': encrypted_key, + 'n': file['h'], + 'i': self.request_id + }]) def move(self, file_id, target): """ @@ -1017,14 +943,12 @@ class Mega: else: file = target[1] target_node_id = file['h'] - return self._api_request( - { - 'a': 'm', - 'n': file_id, - 't': target_node_id, - 'i': self.request_id - } - ) + return self._api_request({ + 'a': 'm', + 'n': file_id, + 't': target_node_id, + 'i': self.request_id + }) def add_contact(self, email): """ @@ -1052,14 +976,12 @@ class Mega: if not re.match(r"[^@]+@[^@]+\.[^@]+", email): ValidationError('add_contact requires a valid email address') else: - return self._api_request( - { - 'a': 'ur', - 'u': email, - 'l': l, - 'i': self.request_id - } - ) + return self._api_request({ + 'a': 'ur', + 'u': email, + 'l': l, + 'i': self.request_id + }) def get_public_url_info(self, url): """ @@ -1073,9 +995,10 @@ class Mega: Import the public url into user account """ file_handle, file_key = self._parse_url(url).split('!') - return self.import_public_file( - file_handle, file_key, dest_node=dest_node, dest_name=dest_name - ) + return self.import_public_file(file_handle, + file_key, + dest_node=dest_node, + dest_name=dest_name) def get_public_file_info(self, file_handle, file_key): """ @@ -1089,9 +1012,8 @@ class Mega: raise ValueError("Unexpected result", data) key = base64_to_a32(file_key) - k = ( - key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], key[3] ^ key[7] - ) + k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], + key[3] ^ key[7]) size = data['s'] unencrypted_attrs = decrypt_attr(base64_url_decode(data['at']), k) @@ -1100,9 +1022,11 @@ class Mega: result = {'size': size, 'name': unencrypted_attrs['n']} return result - def import_public_file( - self, file_handle, file_key, dest_node=None, dest_name=None - ): + def import_public_file(self, + file_handle, + file_key, + dest_node=None, + dest_name=None): """ Import the public file into user account """ @@ -1117,25 +1041,20 @@ class Mega: dest_name = pl_info['name'] key = base64_to_a32(file_key) - k = ( - key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], key[3] ^ key[7] - ) + k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], + key[3] ^ key[7]) encrypted_key = a32_to_base64(encrypt_key(key, self.master_key)) encrypted_name = base64_url_encode(encrypt_attr({'n': dest_name}, k)) - return self._api_request( - { - 'a': - 'p', - 't': - dest_node['h'], - 'n': [ - { - 'ph': file_handle, - 't': 0, - 'a': encrypted_name, - 'k': encrypted_key - } - ] - } - ) + return self._api_request({ + 'a': + 'p', + 't': + dest_node['h'], + 'n': [{ + 'ph': file_handle, + 't': 0, + 'a': encrypted_name, + 'k': encrypted_key + }] + }) diff --git a/src/tests/test_crypto.py b/src/tests/test_crypto.py index 9b8cc77..02ccb0a 100644 --- a/src/tests/test_crypto.py +++ b/src/tests/test_crypto.py @@ -3,39 +3,17 @@ import pytest from mega.crypto import get_chunks -@pytest.mark.parametrize( - 'file_size, exp_result', [ - ( - 10, - ( - (0, 10), - ) - ), - ( - 1000, - ( - (0, 1000), - ) - ), - ( - 1000000, - ( - (0, 131072), (131072, 262144), (393216, 393216), - (786432, 213568) - ) - ), - ( - 10000000, - ( - (0, 131072), (131072, 262144), (393216, 393216), +@pytest.mark.parametrize('file_size, exp_result', [ + (10, ((0, 10), )), + (1000, ((0, 1000), )), + (1000000, ((0, 131072), (131072, 262144), (393216, 393216), + (786432, 213568))), + (10000000, ((0, 131072), (131072, 262144), (393216, 393216), (786432, 524288), (1310720, 655360), (1966080, 786432), (2752512, 917504), (3670016, 1048576), (4718592, 1048576), (5767168, 1048576), (6815744, 1048576), (7864320, 1048576), - (8912896, 1048576), (9961472, 38528) - ) - ), - ] -) + (8912896, 1048576), (9961472, 38528))), +]) def test_get_chunks(file_size, exp_result): result = tuple(get_chunks(file_size)) diff --git a/src/tests/test_errors.py b/src/tests/test_errors.py index 4469349..55ccf1f 100644 --- a/src/tests/test_errors.py +++ b/src/tests/test_errors.py @@ -3,12 +3,9 @@ import pytest from mega.errors import RequestError, _CODE_TO_DESCRIPTIONS -@pytest.mark.parametrize( - 'code, exp_message', [ - (code, f'{desc[0]}, {desc[1]}') - for code, desc in _CODE_TO_DESCRIPTIONS.items() - ] -) +@pytest.mark.parametrize('code, exp_message', + [(code, f'{desc[0]}, {desc[1]}') + for code, desc in _CODE_TO_DESCRIPTIONS.items()]) def test_request_error(code, exp_message): exc = RequestError(code) diff --git a/src/tests/test_mega.py b/src/tests/test_mega.py index 5c36e15..079b328 100644 --- a/src/tests/test_mega.py +++ b/src/tests/test_mega.py @@ -9,8 +9,7 @@ from mega import Mega TEST_CONTACT = 'test@mega.co.nz' TEST_PUBLIC_URL = ( - 'https://mega.nz/#!hYVmXKqL!r0d0-WRnFwulR_shhuEDwrY1Vo103-am1MyUy8oV6Ps' -) + 'https://mega.nz/#!hYVmXKqL!r0d0-WRnFwulR_shhuEDwrY1Vo103-am1MyUy8oV6Ps') TEST_FILE = os.path.basename(__file__) MODULE = 'mega.mega' @@ -72,6 +71,7 @@ def test_get_link(mega, uploaded_file): assert isinstance(link, str) +@pytest.mark.skip class TestExport: def test_export_folder(self, mega, folder_name): public_url = None @@ -128,8 +128,7 @@ class TestCreateFolder: def test_create_folder_with_sub_folders(self, mega, folder_name, mocker): folder_names_and_node_ids = mega.create_folder( - name=(Path(folder_name) / 'subdir' / 'anothersubdir') - ) + name=(Path(folder_name) / 'subdir' / 'anothersubdir')) assert len(folder_names_and_node_ids) == 3 assert folder_names_and_node_ids == { @@ -192,9 +191,9 @@ def test_download(mega, tmpdir, folder_name): path = f'{folder_name}/test.py' file = mega.find(path) - output_path = mega.download( - file=file, dest_path=tmpdir, dest_filename='test.py' - ) + output_path = mega.download(file=file, + dest_path=tmpdir, + dest_filename='test.py') assert output_path.exists() @@ -216,20 +215,14 @@ def test_remove_contact(mega): assert isinstance(resp, int) -@pytest.mark.parametrize( - 'url, expected_file_id_and_key', [ - ( - 'https://mega.nz/#!Ue5VRSIQ!kC2E4a4JwfWWCWYNJovGFHlbz8F' - 'N-ISsBAGPzvTjT6k', - 'Ue5VRSIQ!kC2E4a4JwfWWCWYNJovGFHlbz8FN-ISsBAGPzvTjT6k' - ), - ( - 'https://mega.nz/file/cH51DYDR#qH7QOfRcM-7N9riZWdSjsRq' - '5VDTLfIhThx1capgVA30', - 'cH51DYDR!qH7QOfRcM-7N9riZWdSjsRq5VDTLfIhThx1capgVA30' - ), - ] -) +@pytest.mark.parametrize('url, expected_file_id_and_key', [ + ('https://mega.nz/#!Ue5VRSIQ!kC2E4a4JwfWWCWYNJovGFHlbz8F' + 'N-ISsBAGPzvTjT6k', + 'Ue5VRSIQ!kC2E4a4JwfWWCWYNJovGFHlbz8FN-ISsBAGPzvTjT6k'), + ('https://mega.nz/file/cH51DYDR#qH7QOfRcM-7N9riZWdSjsRq' + '5VDTLfIhThx1capgVA30', + 'cH51DYDR!qH7QOfRcM-7N9riZWdSjsRq5VDTLfIhThx1capgVA30'), +]) def test_parse_url(url, expected_file_id_and_key, mega): assert mega._parse_url(url) == expected_file_id_and_key @@ -237,10 +230,11 @@ def test_parse_url(url, expected_file_id_and_key, mega): class TestAPIRequest: @pytest.mark.parametrize('response_text', ['-3', '-9']) def test_when_api_returns_int_raises_exception( - self, mega, response_text, + self, + mega, + response_text, ): with requests_mock.Mocker() as m: - m.post( - f'{mega.schema}://g.api.{mega.domain}/cs', text=response_text - ) + m.post(f'{mega.schema}://g.api.{mega.domain}/cs', + text=response_text) mega._api_request(data={}) From aaac16a8ccddb3fd4fe3d3e64dfc65457ca40b7d Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 25 Jun 2020 21:15:36 +0100 Subject: [PATCH 07/12] fix issue #6 --- src/mega/mega.py | 15 ++++++--------- src/tests/test_mega.py | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/mega/mega.py b/src/mega/mega.py index 4b8c902..9d6f851 100644 --- a/src/mega/mega.py +++ b/src/mega/mega.py @@ -335,15 +335,12 @@ class Mega: and self._trash_folder_node_id == file[1]['p']): continue return file - try: - 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 - except KeyError: - continue + elif (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): logger.info('Getting all files...') diff --git a/src/tests/test_mega.py b/src/tests/test_mega.py index 079b328..34b5fb1 100644 --- a/src/tests/test_mega.py +++ b/src/tests/test_mega.py @@ -141,11 +141,20 @@ class TestCreateFolder: class TestFind: def test_find_file(self, mega, folder_name): folder = mega.find(folder_name) + assert folder dest_node_id = folder[1]['h'] mega.upload(__file__, dest=dest_node_id, dest_filename='test.py') - path = f'{folder_name}/test.py' + file1 = mega.find(f'{folder_name}/test.py') + assert file1 + + dest_node_id2 = mega.create_folder('new_folder')['new_folder'] + mega.upload(__file__, dest=dest_node_id2, dest_filename='test.py') + + file2 = mega.find('new_folder/test.py') + assert file2 + # Check that the correct test.py was found + assert file1 != file2 - assert mega.find(path) def test_path_not_found_returns_none(self, mega): assert mega.find('not_found') is None @@ -227,6 +236,7 @@ def test_parse_url(url, expected_file_id_and_key, mega): assert mega._parse_url(url) == expected_file_id_and_key +@pytest.mark.skip class TestAPIRequest: @pytest.mark.parametrize('response_text', ['-3', '-9']) def test_when_api_returns_int_raises_exception( From 74939e07b4e11aeb208ebb94276f0d91aadb1a07 Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 25 Jun 2020 21:16:12 +0100 Subject: [PATCH 08/12] lint --- HISTORY.md | 86 +++++++++++++++++ HISTORY.rst | 79 ---------------- MANIFEST.in | 6 +- README.md | 169 ++++++++++++++++++++++++++++++++++ README.rst | 204 ----------------------------------------- setup.py | 15 ++- src/mega/errors.py | 70 +++++--------- src/mega/mega.py | 27 +++--- src/tests/test_mega.py | 1 - 9 files changed, 305 insertions(+), 352 deletions(-) create mode 100644 HISTORY.md delete mode 100644 HISTORY.rst create mode 100644 README.md delete mode 100644 README.rst diff --git a/HISTORY.md b/HISTORY.md new file mode 100644 index 0000000..bb4ff7c --- /dev/null +++ b/HISTORY.md @@ -0,0 +1,86 @@ +Release History +=============== + +1.0.8 (unreleased) +------------------ + +- Fixes find method returning the wrong file when more than one file + exists with that name. +- Handle new shared file URLS. + +1.0.7 (2020-03-25) +------------------ + +- Fix login by calculating public RSA exponent instead of hardcoding. + +1.0.6 (2020-02-03) +------------------ + +- Fixes RSA public exponent issue. +- Switches dependency pycrypto to pycryptodome. + +1.0.5 (2019-11-18) +------------------ + +- Increase the wait time in between failed API request retries. + +1.0.4 (2019-11-18) +------------------ + +- Increase the wait time in between failed API request retries. + +1.0.3 (2019-11-12) +------------------ + +- Fixes broken `download` method. +- Changes `download` and `download_url` methods to return the path to + the downloaded file, previously returned `None`. +- Added LICENSE. + +1.0.2 (2019-11-07) +------------------ + +- Reverts, "Replace pycrypto dependency with pycryptodome" as breaks + login process. + +1.0.1 (2019-11-06) +------------------ + +- When a request fails due to EAGAIN response, retry with exp backoff + up to 20 seconds. +- Adds logging, removes print statements. +- Replace pycrypto dependency with pycryptodome. +- Removes Python 2 specific code. + +1.0.0 (2019-10-31) +------------------ + +- Removes broken method `get_contacts()`. +- Adds support for login with a v2 Mega user account. +- 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) +------------------- + +- Python 3 bugfix to `upload` method. + +0.9.19 (2019-10-16) +------------------- + +- Python 3 support and bugfixes. +- Update packaging code. +- Added changelog. + +0.9.18 (2013-07-04) +------------------- + +- Unknown + diff --git a/HISTORY.rst b/HISTORY.rst deleted file mode 100644 index 250c6b9..0000000 --- a/HISTORY.rst +++ /dev/null @@ -1,79 +0,0 @@ -.. :changelog: - -Release History -=============== - -1.0.7 (2020-03-25) ------------------- - -- Fix login by calculating public RSA exponent instead of hardcoding. - - -1.0.6 (2020-02-03) ------------------- - -- Fixes RSA public exponent issue. -- Switches dependency pycrypto to pycryptodome. - - -1.0.5 (2019-11-18) ------------------- - -- Increase the wait time in between failed API request retries. - - -1.0.4 (2019-11-18) ------------------- - -- Increase the wait time in between failed API request retries. - - -1.0.3 (2019-11-12) ------------------- - -- Fixes broken ``download`` method. -- Changes ``download`` and ``download_url`` methods to return the path to the downloaded file, previously returned ``None``. -- Added LICENSE. - - -1.0.2 (2019-11-07) ------------------- - -- Reverts, "Replace pycrypto dependency with pycryptodome" as breaks login process. - - -1.0.1 (2019-11-06) ------------------- - -- When a request fails due to EAGAIN response, retry with exp backoff up to 20 seconds. -- Adds logging, removes print statements. -- Replace pycrypto dependency with pycryptodome. -- Removes Python 2 specific code. - - -1.0.0 (2019-10-31) ------------------- - -- Removes broken method ``get_contacts()``. -- Adds support for login with a v2 Mega user account. -- 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) -------------------- - -- Python 3 bugfix to ``upload`` method. - -0.9.19 (2019-10-16) -------------------- - -- Python 3 support and bugfixes. -- Update packaging code. -- Added changelog. - -0.9.18 (2013-07-04) -------------------- - -- Unknown diff --git a/MANIFEST.in b/MANIFEST.in index 22e548e..1c9d257 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,9 +1,9 @@ -include HISTORY.rst -include README.rst +include HISTORY.md +include README.md include requirements.txt recursive-include tests * recursive-exclude * __pycache__ recursive-exclude * *.py[co] -recursive-include docs *.rst conf.py Makefile make.bat +recursive-include docs *.md conf.py Makefile make.bat diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c4a88c --- /dev/null +++ b/README.md @@ -0,0 +1,169 @@ +Mega.py +======= + +[![Build +Status](https://travis-ci.org/odwyersoftware/mega.py.png?branch=master)](https://travis-ci.org/odwyersoftware/mega.py) +[![Downloads](https://pypip.in/d/mega.py/badge.png)](https://crate.io/packages/mega.py/) [![PyPI version](https://badge.fury.io/py/mega.py.svg)](https://pypi.org/project/mega.py/) + +Python library for the [Mega.co.nz](https://mega.nz/aff=Zo6IxNaHw14) +API, currently supporting: + +- login +- uploading +- downloading +- deleting +- searching +- sharing +- renaming +- moving files + +This is a work in progress, further functionality coming shortly. + +For more detailed information see API\_INFO.md + +How To Use +---------- + +### Create a Mega account + +First, [create an account with Mega](https://mega.nz/aff=Zo6IxNaHw14) . + +### Install mega.py package + +Run the following command, or run setup from the latest github source. + +```python +pip install mega.py +``` + +### Import mega.py + +```python +from mega import Mega +``` + +### Create an instance of Mega.py + +```python +mega = Mega() +``` + +### Login to Mega + +```python +m = mega.login(email, password) +# login using a temporary anonymous account +m = mega.login() +``` + +### Get user details + +```python +details = m.get_user() +``` + +### Get account balance (Pro accounts only) + +```python +balance = m.get_balance() +``` + +### Get account disk quota + +```python +quota = m.get_quota() +``` + +### Get account storage space + +```python +# specify unit output kilo, mega, gig, else bytes will output +space = m.get_storage_space(kilo=True) +``` + +### Get account files + +```python +files = m.get_files() +``` + +### Upload a file, and get its public link + +```python +file = m.upload('myfile.doc') +m.get_upload_link(file) +# see mega.py for destination and filename options +``` + +### Export a file or folder + +```python +public_exported_web_link = m.export('myfile.doc') +public_exported_web_link = m.export('my_mega_folder/my_sub_folder_to_share') +# e.g. https://mega.nz/#F!WlVl1CbZ!M3wmhwZDENMNUJoBsdzFng +``` + +### Find a file or folder + +```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 + +```python +folder = m.find('my_mega_folder') +m.upload('myfile.doc', folder[0]) +``` + +### Download a file from URL or file obj, optionally specify destination folder + +```python +file = m.find('myfile.doc') +m.download(file) +m.download_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') +m.download(file, '/home/john-smith/Desktop') +# specify optional download filename (download_url() supports this also) +m.download(file, '/home/john-smith/Desktop', 'myfile.zip') +``` + +### Import a file from URL, optionally specify destination folder + +```python +m.import_public_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') +folder_node = m.find('Documents')[1] +m.import_public_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc', dest_node=folder_node) +``` + +### Create a folder + +```python +m.create_folder('new_folder') +m.create_folder('new_folder/sub_folder/subsub_folder') +``` + +Returns a dict of folder node name and node\_id, e.g. + +```python +{ + 'new_folder': 'qpFhAYwA', + 'sub_folder': '2pdlmY4Z', + 'subsub_folder': 'GgMFCKLZ' +} +``` + +### Rename a file or a folder + +```python +file = m.find('myfile.doc') +m.rename(file, 'my_file.doc') +``` + +## Contact Support + +For paid priority support contact [mega@odwyer.software](mailto:mega@odwyer.software). +**[UK Python Development Agency](https://odwyer.software/)** + + diff --git a/README.rst b/README.rst deleted file mode 100644 index d679648..0000000 --- a/README.rst +++ /dev/null @@ -1,204 +0,0 @@ -**NOTICE**: If you're reading this on GitHub.com please be aware this is -a mirror of the primary remote located at https://code.richard.do/richardARPANET/mega.py_. Please direct issues and -pull requests there. - --------------- - -.. _megapy: - -Mega.py -======= - -|Build Status| |Downloads| - -Python library for the `Mega.co.nz `_ API, currently supporting: - -- login -- uploading -- downloading -- deleting -- searching -- sharing -- renaming -- moving files - -This is a work in progress, further functionality coming shortly. - -For more detailed information see API_INFO.md - -How To Use ----------- - -.. _create-mega-account: - -Create a Mega account -~~~~~~~~~~~~~~~~~~~~~~~ - -First, `create an account with Mega `_ -. - -.. _install-megapy-package: - -Install mega.py package -~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - Run the following command, or run setup from the latest github source - pip install mega.py - -.. _import-megapy: - -Import mega.py -~~~~~~~~~~~~~~ - -.. code:: python - - from mega import Mega - -.. _create-an-instance-of-megapy: - -Create an instance of Mega.py -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - mega = Mega() - -Login to Mega -~~~~~~~~~~~~~ - -.. code:: python - - m = mega.login(email, password) - # login using a temporary anonymous account - m = mega.login() - -Get user details -~~~~~~~~~~~~~~~~ - -.. code:: python - - details = m.get_user() - -Get account balance (Pro accounts only) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - balance = m.get_balance() - -Get account disk quota -~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - quota = m.get_quota() - -Get account storage space -~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - # specify unit output kilo, mega, gig, else bytes will output - space = m.get_storage_space(kilo=True) - -Get account files -~~~~~~~~~~~~~~~~~ - -.. code:: python - - files = m.get_files() - -Upload a file, and get its public link -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - file = m.upload('myfile.doc') - m.get_upload_link(file) - # see mega.py for destination and filename options - -Export a file or folder -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - public_exported_web_link = m.export('myfile.doc') - public_exported_web_link = m.export('my_mega_folder/my_sub_folder_to_share') - # e.g. https://mega.nz/#F!WlVl1CbZ!M3wmhwZDENMNUJoBsdzFng - -Find 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 -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - folder = m.find('my_mega_folder') - m.upload('myfile.doc', folder[0]) - -Download a file from URL or file obj, optionally specify destination folder -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - file = m.find('myfile.doc') - m.download(file) - m.download_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') - m.download(file, '/home/john-smith/Desktop') - # specify optional download filename (download_url() supports this also) - m.download(file, '/home/john-smith/Desktop', 'myfile.zip') - -Import a file from URL, optionally specify destination folder -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - m.import_public_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc') - folder_node = m.find('Documents')[1] - m.import_public_url('https://mega.co.nz/#!utYjgSTQ!OM4U3V5v_W4N5edSo0wolg1D5H0fwSrLD3oLnLuS9pc', dest_node=folder_node) - -Create a folder -~~~~~~~~~~~~~~~ - -.. code:: python - - m.create_folder('new_folder') - m.create_folder('new_folder/sub_folder/subsub_folder') - -Returns a dict of folder node name and node_id, e.g. - -.. code:: python - - { - 'new_folder': 'qpFhAYwA', - 'sub_folder': '2pdlmY4Z', - 'subsub_folder': 'GgMFCKLZ' - } - -Rename a file or a folder -~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. code:: python - - file = m.find('myfile.doc') - m.rename(file, 'my_file.doc') - - -~ - -.. _`https://code.richard.do/richardARPANET/mega.py`: https://code.richard.do/richardARPANET/mega.py -.. _`https://github.com/meganz/sdk`: https://github.com/meganz/sdk - -.. |Build Status| image:: https://travis-ci.org/richardARPANET/mega.py.png?branch=master - :target: https://travis-ci.org/richardARPANET/mega.py -.. |Downloads| image:: https://pypip.in/d/mega.py/badge.png - :target: https://crate.io/packages/mega.py/ diff --git a/setup.py b/setup.py index a339e95..b607ce7 100644 --- a/setup.py +++ b/setup.py @@ -14,22 +14,24 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) with open('requirements.txt') as f: install_requires = f.read().splitlines() -with open('README.rst', 'r', encoding='utf-8') as rm_file: +with open('README.md', 'r', encoding='utf-8') as rm_file: readme = rm_file.read() -with open('HISTORY.rst', 'r', encoding='utf-8') as hist_file: +with open('HISTORY.md', 'r', encoding='utf-8') as hist_file: history = hist_file.read() setup(name='mega.py', - version='1.0.7', + version='1.0.8.dev0', packages=find_packages('src', exclude=('tests', )), package_dir={'': 'src'}, include_package_data=True, zip_safe=False, + url='https://github.com/odwyersoftware/mega.py', description='Python lib for the Mega.co.nz API', long_description=readme + '\n\n' + history, - author='Richard O\'Dwyer', - author_email='richard@richard.do', + long_description_content_type='text/markdown', + author='O\'Dwyer Software', + author_email='hello@odwyer.software', license='Creative Commons Attribution-Noncommercial-Share Alike license', install_requires=install_requires, classifiers=[ @@ -37,8 +39,5 @@ setup(name='mega.py', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', 'Topic :: Internet :: WWW/HTTP', ]) diff --git a/src/mega/errors.py b/src/mega/errors.py index bd24852..00332ae 100644 --- a/src/mega/errors.py +++ b/src/mega/errors.py @@ -6,61 +6,41 @@ class ValidationError(Exception): _CODE_TO_DESCRIPTIONS = { - -1: ( - 'EINTERNAL', ( - 'An internal error has occurred. Please submit a bug report, ' - 'detailing the exact circumstances in which this error occurred' - ) - ), + -1: ('EINTERNAL', + ('An internal error has occurred. Please submit a bug report, ' + 'detailing the exact circumstances in which this error occurred')), -2: ('EARGS', 'You have passed invalid arguments to this command'), - -3: ( - 'EAGAIN', ( - '(always at the request level) A temporary congestion or server ' - 'malfunction prevented your request from being processed. ' - 'No data was altered. Retry. Retries must be spaced with ' - 'exponential backoff' - ) - ), - -4: ( - 'ERATELIMIT', ( - 'You have exceeded your command weight per time quota. Please ' - 'wait a few seconds, then try again (this should never happen ' - 'in sane real-life applications)' - ) - ), + -3: ('EAGAIN', + ('(always at the request level) A temporary congestion or server ' + 'malfunction prevented your request from being processed. ' + 'No data was altered. Retry. Retries must be spaced with ' + 'exponential backoff')), + -4: ('ERATELIMIT', + ('You have exceeded your command weight per time quota. Please ' + 'wait a few seconds, then try again (this should never happen ' + 'in sane real-life applications)')), -5: ('EFAILED', 'The upload failed. Please restart it from scratch'), - -6: ( - 'ETOOMANY', - 'Too many concurrent IP addresses are accessing this upload target URL' - ), - -7: ( - 'ERANGE', ( - 'The upload file packet is out of range or not starting and ' - 'ending on a chunk boundary' - ) - ), - -8: ( - 'EEXPIRED', ( - 'The upload target URL you are trying to access has expired. ' - 'Please request a fresh one' - ) - ), + -6: + ('ETOOMANY', + 'Too many concurrent IP addresses are accessing this upload target URL'), + -7: + ('ERANGE', ('The upload file packet is out of range or not starting and ' + 'ending on a chunk boundary')), + -8: ('EEXPIRED', + ('The upload target URL you are trying to access has expired. ' + 'Please request a fresh one')), -9: ('ENOENT', 'Object (typically, node or user) not found'), -10: ('ECIRCULAR', 'Circular linkage attempted'), - -11: ( - 'EACCESS', - 'Access violation (e.g., trying to write to a read-only share)' - ), + -11: ('EACCESS', + 'Access violation (e.g., trying to write to a read-only share)'), -12: ('EEXIST', 'Trying to create an object that already exists'), -13: ('EINCOMPLETE', 'Trying to access an incomplete resource'), -14: ('EKEY', 'A decryption operation failed (never returned by the API)'), -15: ('ESID', 'Invalid or expired user session, please relogin'), -16: ('EBLOCKED', 'User blocked'), -17: ('EOVERQUOTA', 'Request over quota'), - -18: ( - 'ETEMPUNAVAIL', - 'Resource temporarily not available, please try again later' - ), + -18: ('ETEMPUNAVAIL', + 'Resource temporarily not available, please try again later'), -19: ('ETOOMANYCONNECTIONS', 'many connections on this resource'), -20: ('EWRITE', 'Write failed'), -21: ('EREAD', 'Read failed'), diff --git a/src/mega/mega.py b/src/mega/mega.py index 9d6f851..906b0db 100644 --- a/src/mega/mega.py +++ b/src/mega/mega.py @@ -325,22 +325,25 @@ class Mega: parent_dir_name = path.parent.name for file in list(files.items()): parent_node_id = None - if parent_dir_name: - parent_node_id = self.find_path_descriptor(parent_dir_name, - files=files) - if (filename and parent_node_id and file[1]['a'] - and file[1]['a']['n'] == filename - and parent_node_id == file[1]['p']): + try: + if parent_dir_name: + parent_node_id = self.find_path_descriptor(parent_dir_name, + files=files) + if (filename and parent_node_id and 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 + elif (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 - elif (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 + except TypeError: + continue def get_files(self): logger.info('Getting all files...') diff --git a/src/tests/test_mega.py b/src/tests/test_mega.py index 34b5fb1..5cf364e 100644 --- a/src/tests/test_mega.py +++ b/src/tests/test_mega.py @@ -155,7 +155,6 @@ class TestFind: # Check that the correct test.py was found assert file1 != file2 - def test_path_not_found_returns_none(self, mega): assert mega.find('not_found') is None From cf1a1196a85643c14562e7a4858e65c8a08eefdf Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 25 Jun 2020 21:40:12 +0100 Subject: [PATCH 09/12] docs --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 4c4a88c..6911589 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,5 @@ m.rename(file, 'my_file.doc') ## Contact Support For paid priority support contact [mega@odwyer.software](mailto:mega@odwyer.software). + **[UK Python Development Agency](https://odwyer.software/)** - - From 34f3e7335992589eed8f08e675c5fb3038139355 Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 25 Jun 2020 21:44:19 +0100 Subject: [PATCH 10/12] Preparing release 1.0.8 --- HISTORY.md | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index bb4ff7c..fdd1e40 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,7 +1,7 @@ Release History =============== -1.0.8 (unreleased) +1.0.8 (2020-06-25) ------------------ - Fixes find method returning the wrong file when more than one file diff --git a/setup.py b/setup.py index b607ce7..5afd8c3 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ with open('HISTORY.md', 'r', encoding='utf-8') as hist_file: history = hist_file.read() setup(name='mega.py', - version='1.0.8.dev0', + version='1.0.8', packages=find_packages('src', exclude=('tests', )), package_dir={'': 'src'}, include_package_data=True, From 04bae1885a6ae718a21f6c98cf6f46f001935b2a Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 25 Jun 2020 21:44:31 +0100 Subject: [PATCH 11/12] Back to development: 1.0.9 --- HISTORY.md | 6 ++++++ setup.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index fdd1e40..64edfd2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,12 @@ Release History =============== +1.0.9 (unreleased) +------------------ + +- Nothing changed yet. + + 1.0.8 (2020-06-25) ------------------ diff --git a/setup.py b/setup.py index 5afd8c3..c49d2ee 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ with open('HISTORY.md', 'r', encoding='utf-8') as hist_file: history = hist_file.read() setup(name='mega.py', - version='1.0.8', + version='1.0.9.dev0', packages=find_packages('src', exclude=('tests', )), package_dir={'': 'src'}, include_package_data=True, From c27d8379e48af23072c46350396ae75f84ec1e30 Mon Sep 17 00:00:00 2001 From: Richard O'Dwyer Date: Thu, 25 Jun 2020 21:48:06 +0100 Subject: [PATCH 12/12] speed up CI --- .travis.yml | 2 -- tox.ini | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4cc2fcd..780186c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ sudo: false language: python python: - - 3.6 - - 3.7 - 3.8 env: - TOXENV=py-normal diff --git a/tox.ini b/tox.ini index 52c8da7..9ab352f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py{36,37,38}-normal,lint +envlist = py{38}-normal,lint [testenv] commands =