From 460587d7952652c6cbe8c36fbcb7b87870279289 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 5 Oct 2026 23:58:10 +0500 Subject: move URL-related stuff to url.py --- cgitize/config.py | 1 - cgitize/repo.py | 6 +++--- cgitize/url.py | 29 +++++++++++++++++++++++++++++ cgitize/utils.py | 24 ------------------------ 4 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 cgitize/url.py diff --git a/cgitize/config.py b/cgitize/config.py index ad31cc3..6be39f4 100644 --- a/cgitize/config.py +++ b/cgitize/config.py @@ -5,7 +5,6 @@ from abc import ABC, abstractmethod import os -from urllib.parse import quote import tomli diff --git a/cgitize/repo.py b/cgitize/repo.py index fff4d8e..f0c8400 100644 --- a/cgitize/repo.py +++ b/cgitize/repo.py @@ -6,7 +6,7 @@ from enum import Enum import os.path -from cgitize.utils import url_remove_auth, url_replace_auth +from cgitize import url class Visibility(Enum): @@ -117,7 +117,7 @@ class Repo: if len(https_urls) != 1: raise RuntimeError(f"no https:// clone URL for repository '{name}'?!") # Bitbucket leaves the username in the URL... Sigh. - api_auth, https_url = url_remove_auth(https_urls[0]["href"]) + api_auth, https_url = url.remove_auth(https_urls[0]["href"]) ssh_urls = [ link for link in src.data["links"]["clone"] if link["name"] == "ssh" @@ -231,7 +231,7 @@ class Repo: def clone_url_with_auth(self): if not self.url_auth: return self.clone_url - return url_replace_auth(self.clone_url, self.url_auth) + return url.replace_auth(self.clone_url, self.url_auth) def _with_dir(self, s): if self._dir is None: diff --git a/cgitize/url.py b/cgitize/url.py new file mode 100644 index 0000000..8d62b50 --- /dev/null +++ b/cgitize/url.py @@ -0,0 +1,29 @@ +# Copyright (c) 2026 Egor Tensin +# This file is part of the "cgitize" project. +# For details, see https://github.com/egor-tensin/cgitize +# Distributed under the MIT License. + +from urllib.parse import quote, urlsplit, urlunsplit + + +def replace_auth(url, auth): + username, password = auth + parts = urlsplit(url) + netloc = quote(username) + if password is not None: + netloc += f":{quote(password)}" + netloc += f"@{parts.hostname}" + if parts.port is not None: + netloc += f":{parts.port}" + parts = parts._replace(netloc=netloc) + return urlunsplit(parts) + + +def remove_auth(url): + parts = urlsplit(url) + auth = parts.username, parts.password + netloc = parts.hostname + if parts.port is not None: + netloc += f":{parts.port}" + parts = parts._replace(netloc=netloc) + return auth, urlunsplit(parts) diff --git a/cgitize/utils.py b/cgitize/utils.py index 6925288..c86e2d8 100644 --- a/cgitize/utils.py +++ b/cgitize/utils.py @@ -9,7 +9,6 @@ import os import stat import subprocess import sys -from urllib.parse import quote, urlsplit, urlunsplit @contextmanager @@ -107,26 +106,3 @@ def protected_file(path): yield finally: os.unlink(path) - - -def url_replace_auth(url, auth): - username, password = auth - parts = urlsplit(url) - netloc = quote(username) - if password is not None: - netloc += f":{quote(password)}" - netloc += f"@{parts.hostname}" - if parts.port is not None: - netloc += f":{parts.port}" - parts = parts._replace(netloc=netloc) - return urlunsplit(parts) - - -def url_remove_auth(url): - parts = urlsplit(url) - auth = parts.username, parts.password - netloc = parts.hostname - if parts.port is not None: - netloc += f":{parts.port}" - parts = parts._replace(netloc=netloc) - return auth, urlunsplit(parts) -- cgit v1.3