refactor: move validate spec into ViVenv

This commit is contained in:
Daylin Morgan 2023-05-28 21:15:31 -05:00
parent 0ec266775c
commit 9a33a99480
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -52,7 +52,7 @@ from typing import (
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.request import urlopen from urllib.request import urlopen
__version__ = "23.5a3" __version__ = "23.5a2-2-gebb657c-dev"
class Config: class Config:
@ -559,7 +559,7 @@ class ViVenv:
name: str = "", name: str = "",
path: Path | None = None, path: Path | None = None,
) -> None: ) -> None:
self.spec = spec self.spec = self._validate_spec(spec)
self.exe = str(Path(sys.executable).resolve()) if track_exe else "N/A" self.exe = str(Path(sys.executable).resolve()) if track_exe else "N/A"
self.id = id if id else get_hash(spec, track_exe) self.id = id if id else get_hash(spec, track_exe)
self.name = name if name else self.id self.name = name if name else self.id
@ -583,6 +583,18 @@ class ViVenv:
return vivenv return vivenv
def _validate_spec(self, spec: Tuple[str, ...]) -> List[str]:
"""ensure spec is at least of sequence of strings
Args:
spec: sequence of package specifications
"""
if not set(map(type, spec)) == {str}:
error("unexepected input in package spec")
error(f"check your packages definitions: {spec}", code=1)
else:
return sorted(spec)
def create(self, quiet: bool = False) -> None: def create(self, quiet: bool = False) -> None:
if not quiet: if not quiet:
echo(f"new unique vivenv -> {self.name}") echo(f"new unique vivenv -> {self.name}")
@ -636,7 +648,6 @@ def use(*packages: str, track_exe: bool = False, name: str = "") -> Path:
track_exe: if true make env python exe specific track_exe: if true make env python exe specific
name: use as vivenv name, if not provided id is used name: use as vivenv name, if not provided id is used
""" """
validate_spec(packages)
vivenv = ViVenv(list(packages), track_exe=track_exe, name=name) vivenv = ViVenv(list(packages), track_exe=track_exe, name=name)
if vivenv.name not in [d.name for d in c.venvcache.iterdir()] or os.getenv( if vivenv.name not in [d.name for d in c.venvcache.iterdir()] or os.getenv(
@ -650,18 +661,6 @@ def use(*packages: str, track_exe: bool = False, name: str = "") -> Path:
return vivenv.path return vivenv.path
def validate_spec(spec: Tuple[str, ...]) -> None:
"""ensure spec is at least of sequence of strings
Args:
spec: sequence of package specifications
"""
# ? make this a part of ViVenv?
if not set(map(type, spec)) == {str}:
error("unexepected input in package spec")
error(f"check your packages definitions: {spec}", code=1)
def modify_sys_path(new_path: Path) -> None: def modify_sys_path(new_path: Path) -> None:
# remove user-site # remove user-site
for i, path in enumerate(sys.path): for i, path in enumerate(sys.path):