Skip to content

Commit 86281d2

Browse files
authored
refactor: split URL parsing logic (#22)
1 parent 323cb98 commit 86281d2

3 files changed

Lines changed: 37 additions & 28 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "async-mega.py"
33
authors = [{name = "NTFSvolume", email = "NTFSvolume@proton.me"}]
44
classifiers = [
5-
"Development Status :: 4 - Beta",
5+
"Development Status :: 5 - Production/Stable",
66
"Environment :: Console",
77
"Framework :: AsyncIO",
88
"Intended Audience :: Developers",
@@ -34,7 +34,7 @@ license = "Apache-2.0"
3434
license-files = ["LICENSE"]
3535
readme = "README.md"
3636
requires-python = ">=3.11"
37-
version = "2.4.0"
37+
version = "2.4.1"
3838

3939
[project.optional-dependencies]
4040
cli = [
@@ -123,7 +123,7 @@ requires = ["uv_build<=0.10.11"]
123123

124124
[dependency-groups]
125125
dev = [
126-
"prek>=0.3.6",
126+
"prek>=0.4.0",
127127
"pytest-asyncio>=0.25.0",
128128
"pytest>=8.3.5",
129129
"ruff==0.15.6"

src/mega/core.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,27 @@ def parse_folder_url(cls, url: str | yarl.URL) -> tuple[NodeID, str, NodeID | No
106106
return result.public_handle, result.public_key, result.selected_node
107107

108108
@staticmethod
109-
def parse_url(url: str | yarl.URL) -> PublicURLInfo:
110-
"""Parse a public URL"""
109+
def ensure_v2_url(url: str | yarl.URL) -> yarl.URL:
110+
"""Transform an URL in V1 format to v2"""
111111
url = yarl.URL(url)
112112
Site.MEGA.check_host(url)
113113
new_url = transform_v1_url(url)
114114
if new_url != url:
115115
logger.info(f"Transformed v1 URL from {url} to {new_url}")
116+
return new_url
116117

117-
url = new_url
118-
if not url.fragment:
118+
@staticmethod
119+
def parse_url(url: str | yarl.URL, *, check_key: bool = True) -> PublicURLInfo:
120+
"""Parse a public URL"""
121+
url = MegaCore.ensure_v2_url(url)
122+
info = MegaCore._parse_public_v2_url(url)
123+
if check_key and not info.public_key:
119124
raise ValidationError(f"Public key missing from {url}")
125+
return info
120126

127+
@staticmethod
128+
def _parse_public_v2_url(url: yarl.URL, /) -> PublicURLInfo:
129+
"""Parse a public URL"""
121130
match url.parts[1:]:
122131
case ["file", public_handle]:
123132
return PublicURLInfo(False, public_handle, url.fragment)
@@ -422,7 +431,7 @@ async def import_file(
422431
) -> Node:
423432
"""Import the public file into user account"""
424433
full_key = b64_to_a32(public_key)
425-
key = Crypto.decompose(full_key).key
434+
key = Crypto.decompose(full_key).key # pyright: ignore[reportArgumentType]
426435
file_info = await self.request_file_info(public_handle, is_public=True)
427436
name = self.decrypt_attrs(file_info._at, key, public_handle).name
428437
encrypted_key = a32_to_base64(encrypt_key(full_key, self.vault.master_key))

uv.lock

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)