Skip to content
Back to top
View this page

# twine.utils module

twine.utils.get_config(path: str) → Dict[str, Dict[str, str | None]]

  Read repository configuration from a file (i.e. ~/.pypirc).

  Format: https://packaging.python.org/specifications/pypirc/

  If the default config file doesn’t exist, return a default configuration for pypyi and testpypi.

twine.utils.sanitize_url(url: str) → str

  Sanitize a URL.

  Sanitize URLs, removing any user:password combinations and replacing them with asterisks. Returns the original URL if the string is a non-matching pattern.

  Parameters:

    url – str containing a URL to sanitize.

  return:

    str either sanitized or as entered depending on pattern match.

twine.utils._validate_repository_url(repository_url: str) → None

  Validate the given url for allowed schemes and components.

twine.utils.get_repository_from_config(config_file: str, repository: str, repository_url: str | None = None) → Dict[str, str | None]

  Get repository config command-line values or the .pypirc file.

twine.utils._config_from_repository_url(url: str) → Dict[str, str | None]

twine.utils.normalize_repository_url(url: str) → str

twine.utils.get_file_size(filename: str) → str

  Return the size of a file in KB, or MB if >= 1024 KB.

twine.utils.check_status_code(response: Response, verbose: bool) → None

  Generate a helpful message based on the response from the repository.

  Raise a custom exception for recognized errors. Otherwise, print the response content (based on the verbose option) before re-raising the HTTPError.

twine.utils.get_userpass_value(cli_value: str | None, config: Dict[str, str | None], key: str, prompt_strategy: Callable[[], str] | None = None) → str | None

  Get a credential (e.g. a username or password) from the configuration.

  Uses the following rules:

  1. If `cli_value` is specified, use that.

  2. If `config[key]` is specified, use that.

  3. If `prompt_strategy` is specified, use its return value.

  4. Otherwise return `None`

  Parameters:

    - cli_value – The value supplied from the command line.

    - config – A dictionary of repository configuration values.

    - key – The credential to look up in `config`, e.g. `"username"` or `"password"`.

    - prompt_strategy – An argumentless function to get the value, e.g. from keyring or by prompting the user.

  Returns:

    The credential value, i.e. the username or password.

twine.utils.get_cacert(cli_value: str | None, config: Dict[str, str | None], *, key: str = 'ca_cert', prompt_strategy: Callable[[], str] | None = None) → str | None

  Get the CA bundle via `get_userpass_value()`.

twine.utils.get_clientcert(cli_value: str | None, config: Dict[str, str | None], *, key: str = 'client_cert', prompt_strategy: Callable[[], str] | None = None) → str | None

  Get the client certificate via `get_userpass_value()`.

twine.utils.make_requests_session() → Session

  Prepare a requests Session with retries & twine’s user-agent string.

class twine.utils.EnvironmentDefault

  Get values from environment variable.

  __init__(env: str, required: bool = True, default: str | None = None, **kwargs: Any) → None

class twine.utils.EnvironmentFlag

  Set boolean flag from environment variable.

  __init__(env: str, **kwargs: Any) → None
  static bool_from_env(val: str | None) → bool

    Allow ‘0’ and ‘false’ and ‘no’ to be False.