Skip to content

Commit

Permalink
Initial support for persistent user settings (#58)
Browse files Browse the repository at this point in the history
Includes:
 - pypi index aliases (#132)
 - default conda format
  • Loading branch information
analog-cbarber committed Apr 22, 2024
1 parent a23b6d2 commit 9c3de73
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# whl2conda changes

## [24.1.1] - *in progress*
### Features
* Added persistent user settings for:
* default conda format
* whether to automatically update stdrenames table
* specify aliases for extra pypi indexes

## [24.4.0] - 2024-4-14
### Changes
* Only use classic installer in `whl2conda install` environments if
Expand Down
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ theme:
hljs_languages:
python
json
logo: whl2conda.svg
favicon: whl2conda.svg
logo: whl2conda.jpg
favicon: whl2conda.jpg
palette:
- media: "(prefers-color-scheme: light)"
scheme: default
Expand Down
3 changes: 2 additions & 1 deletion src/whl2conda/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
24.4.0
24.4.1

50 changes: 49 additions & 1 deletion src/whl2conda/cli/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Christopher Barber
# Copyright 2023-2024 Christopher Barber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
from __future__ import annotations

import argparse
import json
import sys
from http.client import HTTPException
from pathlib import Path
Expand All @@ -29,6 +30,7 @@
from .common import add_markdown_help, dedent
from ..impl.pyproject import add_pyproject_defaults
from ..api.stdrename import user_stdrenames_path
from ..settings import settings

__all__ = ["config_main"]

Expand Down Expand Up @@ -76,6 +78,26 @@ def config_main(
%(const)s.
"""),
)

settings_opts = parser.add_argument_group("user settings options")

settings_opts.add_argument(
"--remove", metavar="<key>", help="Unset user setting with given key."
)
settings_opts.add_argument(
"--set",
metavar=("<key>", "<value>"),
nargs=2,
)

settings_opts.add_argument(
"--show",
metavar="<key>",
nargs="?",
const="",
help=f"Show user settings from {settings._settings_file}",
)

parser.add_argument(
"-n",
"--dry-run",
Expand All @@ -95,6 +117,15 @@ def config_main(
except Exception as ex: # pylint: disable=broad-exception-caught
parser.error(str(ex))

if parsed.remove:
remove_user_setting(parsed.remove)

if parsed.set:
set_user_setting(*parsed.set)

if parsed.show is not None:
show_user_settings(parsed.show)


def update_std_renames(renames_file: Path, *, dry_run: bool) -> None:
"""
Expand Down Expand Up @@ -124,5 +155,22 @@ def update_std_renames(renames_file: Path, *, dry_run: bool) -> None:
sys.exit(0)


def set_user_setting(key: str, value: str) -> None:
settings.set(key, value)
settings.save()


def show_user_settings(key: str) -> None:
if key:
print(f"{key}: {json.dumps(settings.get(key))}")
else:
print(json.dumps(settings.to_dict(), indent=2))


def remove_user_setting(key: str) -> None:
settings.unset(key)
settings.save()


if __name__ == "__main__": # pragma: no cover
config_main()
3 changes: 2 additions & 1 deletion src/whl2conda/cli/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ..impl.prompt import is_interactive, choose_wheel
from ..api.converter import Wheel2CondaConverter, CondaPackageFormat, DependencyRename
from ..impl.pyproject import read_pyproject, PyProjInfo
from ..settings import settings
from .common import (
add_markdown_help,
dedent,
Expand Down Expand Up @@ -452,7 +453,7 @@ def convert_main(args: Optional[Sequence[str]] = None, prog: Optional[str] = Non
elif pyproj_info.conda_format:
out_fmt = pyproj_info.conda_format
else:
out_fmt = CondaPackageFormat.V2
out_fmt = settings.conda_format

if verbosity < -1:
level = logging.ERROR
Expand Down
13 changes: 12 additions & 1 deletion src/whl2conda/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Christopher Barber
# Copyright 2023-2024 Christopher Barber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -20,10 +20,12 @@

import argparse
import sys
from pathlib import Path
from typing import Optional, Sequence

from ..__about__ import __version__
from .common import dedent, Subcommands, add_markdown_help
from ..settings import settings

__all__ = ["main"]

Expand Down Expand Up @@ -83,11 +85,20 @@ def __call__(self, *args, **kwargs):
"--list-subcommands", action=ListSubcommands, nargs=0, help=argparse.SUPPRESS
)

parser.add_argument(
"--settings",
metavar="<filepath>",
help="Override default settings file",
)

add_markdown_help(parser)
parser.add_argument("--version", action="version", version=__version__)

parsed = parser.parse_args(args)

if parsed.settings:
settings.load(Path(parsed.settings).expanduser())

subcmds.run(parsed)


Expand Down
30 changes: 30 additions & 0 deletions src/whl2conda/impl/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,46 @@

from __future__ import annotations

import configparser
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Optional

from ..settings import settings

__all__ = [
"download_wheel",
"lookup_pypi_index",
]


def lookup_pypi_index(index: str) -> str:
"""
Translate index aliases
First looks for exact match in user settings
pyproject_indexes table and then looks for
matching entry in the ~/.pypirc file.
Otherwise returns the original string
"""
if new_index := settings.pypi_indexes.get(index):
return new_index

pypirc_path = Path("~/.pypirc").expanduser()
if pypirc_path.exists():
pypirc = configparser.ConfigParser()
pypirc.read(pypirc_path)
try:
return pypirc[index]["repository"]
except Exception:
pass
return index


def download_wheel(
spec: str,
index: str = "",
Expand Down Expand Up @@ -61,6 +89,8 @@ def download_wheel(
"--implementation",
"py",
]
if index:
index = lookup_pypi_index(index)
if index:
cmd.extend(["-i", index])
cmd.extend(["-d", str(tmpdirname)])
Expand Down
2 changes: 1 addition & 1 deletion src/whl2conda/impl/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
]


class CondaPackageFormat(enum.Enum):
class CondaPackageFormat(str, enum.Enum):
"""
Supported output package formats
Expand Down
Loading

0 comments on commit 9c3de73

Please sign in to comment.