Skip to content

Commit

Permalink
Strip out unnecessary dependencies
Browse files Browse the repository at this point in the history
- Remove `vistir` and `crayons`
- Reimplement heavily relied-upon functionality
- Copy over `vistir` utilities used heavily in tests to avoid this as a
  test dependency as well
- Rewrite mocks that relied on vistir-specific invocations
- Regenerate lockfile
- Begins to address #78

Signed-off-by: Dan Ryan <[email protected]>
  • Loading branch information
techalchemy committed Jan 7, 2020
1 parent fbd35fa commit fc054fd
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 181 deletions.
99 changes: 25 additions & 74 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions news/78.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduced dependencies by removing ``vistir``,, ``crayons`` and intermediate calls.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ install_requires =
attrs
cached-property
click
crayons
six
packaging
vistir[spinner]>=0.4
backports.functools_lru_cache;python_version=="2.7"
pathlib2;python_version<"3.5"

[options.packages.find]
where = src
Expand Down
22 changes: 10 additions & 12 deletions src/pythonfinder/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import sys

import click
import crayons

from . import __version__
from .pythonfinder import Finder
Expand Down Expand Up @@ -32,10 +29,11 @@ def cli(
if version:
click.echo(
"{0} version {1}".format(
crayons.white("PythonFinder", bold=True), crayons.yellow(__version__)
click.style("PythonFinder", fg="white", bold=True),
click.style(str(__version__), fg="yellow")
)
)
sys.exit(0)
ctx.exit()
finder = Finder(ignore_unsupported=ignore_unsupported)
if findall:
versions = [v for v in finder.find_all_python_versions()]
Expand All @@ -54,7 +52,7 @@ def cli(
),
fg="yellow",
)
sys.exit(0)
ctx.exit()
else:
click.secho(
"ERROR: No valid python versions found! Check your path and try again.",
Expand All @@ -78,22 +76,22 @@ def cli(
),
fg="yellow",
)
sys.exit(0)
ctx.exit()
else:
click.secho("Failed to find matching executable...", fg="yellow")
sys.exit(1)
ctx.exit(1)
elif which:
found = finder.system_path.which(which.strip())
if found:
click.secho("Found Executable: {0}".format(found), fg="white")
sys.exit(0)
ctx.exit()
else:
click.secho("Failed to find matching executable...", fg="yellow")
sys.exit(1)
ctx.exit(1)
else:
click.echo("Please provide a command", color="red")
sys.exit(1)
sys.exit()
ctx.exit(1)
ctx.exit()


if __name__ == "__main__":
Expand Down
38 changes: 38 additions & 0 deletions src/pythonfinder/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding=utf-8 -*-
import sys

import six

if sys.version_info[:2] <= (3, 4):
from pathlib2 import Path # type: ignore # noqa
else:
from pathlib import Path

if six.PY3:
from functools import lru_cache
else:
from backports.functools_lru_cache import lru_cache # type: ignore # noqa


def getpreferredencoding():
import locale
# Borrowed from Invoke
# (see https://github.com/pyinvoke/invoke/blob/93af29d/invoke/runners.py#L881)
_encoding = locale.getpreferredencoding(False)
if six.PY2 and not sys.platform == "win32":
_default_encoding = locale.getdefaultlocale()[1]
if _default_encoding is not None:
_encoding = _default_encoding
return _encoding


DEFAULT_ENCODING = getpreferredencoding()


def fs_str(string):
"""Encodes a string into the proper filesystem encoding"""

if isinstance(string, str):
return string
assert not isinstance(string, bytes)
return string.encode(DEFAULT_ENCODING)
4 changes: 2 additions & 2 deletions src/pythonfinder/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import attr
import six
from vistir.compat import fs_str

from ..compat import fs_str
from ..environment import MYPY_RUNNING
from ..exceptions import InvalidPythonVersion
from ..utils import (
Expand All @@ -35,7 +35,7 @@
TypeVar,
Type,
)
from vistir.compat import Path
from ..compat import Path

BaseFinderType = TypeVar("BaseFinderType")

Expand Down
4 changes: 2 additions & 2 deletions src/pythonfinder/models/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import attr
import six
from cached_property import cached_property
from vistir.compat import Path, fs_str
from vistir.misc import dedup
from ..compat import Path, fs_str

from ..environment import (
ASDF_DATA_DIR,
Expand All @@ -26,6 +25,7 @@
from ..utils import (
Iterable,
Sequence,
dedup,
ensure_path,
expand_paths,
filter_pythons,
Expand Down
2 changes: 1 addition & 1 deletion src/pythonfinder/models/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import attr
import six
from packaging.version import Version
from vistir.compat import Path, lru_cache

from ..compat import Path, lru_cache
from ..environment import ASDF_DATA_DIR, MYPY_RUNNING, PYENV_ROOT, SYSTEM_ARCH
from ..exceptions import InvalidPythonVersion
from ..utils import (
Expand Down
30 changes: 23 additions & 7 deletions src/pythonfinder/pythonfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import six
from click import secho
from vistir.compat import lru_cache

from . import environment
from .compat import lru_cache
from .exceptions import InvalidPythonVersion
from .utils import Iterable, filter_pythons, version_re

Expand Down Expand Up @@ -51,7 +51,8 @@ def __init__(
:param system: bool, optional
:param global_search: Whether to search the global path from os.environ, defaults to True
:param global_search: bool, optional
:param ignore_unsupported: Whether to ignore unsupported python versions, if False, an error is raised, defaults to True
:param ignore_unsupported: Whether to ignore unsupported python versions, if False, an
error is raised, defaults to True
:param ignore_unsupported: bool, optional
:param bool sort_by_path: Whether to always sort by path
:returns: a :class:`~pythonfinder.pythonfinder.Finder` object.
Expand Down Expand Up @@ -133,8 +134,16 @@ def which(self, exe):
return self.system_path.which(exe)

@classmethod
def parse_major(cls, major, minor=None, patch=None, pre=None, dev=None, arch=None):
# type: (Optional[str], Optional[int], Optional[int], Optional[bool], Optional[bool], Optional[str]) -> Dict[str, Union[int, str, bool, None]]
def parse_major(
cls,
major, # type: Optional[str]
minor=None, # type: Optional[int]
patch=None, # type: Optional[int]
pre=None, # type: Optional[bool]
dev=None, # type: Optional[bool]
arch=None, # type: Optional[str]
):
# type: (...) -> Dict[str, Union[int, str, bool, None]]
from .models import PythonVersion

major_is_str = major and isinstance(major, six.string_types)
Expand Down Expand Up @@ -289,11 +298,18 @@ def find_python_version(

@lru_cache(maxsize=1024)
def find_all_python_versions(
self, major=None, minor=None, patch=None, pre=None, dev=None, arch=None, name=None
self,
major=None, # type: Optional[Union[str, int]]
minor=None, # type: Optional[int]
patch=None, # type: Optional[int]
pre=None, # type: Optional[bool]
dev=None, # type: Optional[bool]
arch=None, # type: Optional[str]
name=None, # type: Optional[str]
):
# type: (Optional[Union[str, int]], Optional[int], Optional[int], Optional[bool], Optional[bool], Optional[str], Optional[str]) -> List[PathEntry]
# type: (...) -> List[PathEntry]
version_sort = operator.attrgetter("as_python.version_sort")
python_version_dict = getattr(self.system_path, "python_version_dict")
python_version_dict = getattr(self.system_path, "python_version_dict", {})
if python_version_dict:
paths = (
path
Expand Down
Loading

0 comments on commit fc054fd

Please sign in to comment.