Skip to content

Commit

Permalink
feat: support forcing color/nocolor (#575)
Browse files Browse the repository at this point in the history
* feat: support forcing color/nocolor

* tests: ignore occasional PyPy error

* chore: remove Python 3 __div__ workaround

* fix: guard style
  • Loading branch information
henryiii committed Jan 24, 2022
1 parent 34fa7e4 commit bd29e7f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.8.0
-----

* Drop Python 2.7 and 3.5 support (`#573 <https://github.com/tomerfiliba/plumbum/pull/573>`_)
* Color: support ``NO_COLOR``/``FORCE_COLOR`` (`#575 <https://github.com/tomerfiliba/plumbum/pull/575>`_)



1.7.2
-----

Expand Down
16 changes: 10 additions & 6 deletions plumbum/colorlib/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,26 @@

def get_color_repr():
"""Gets best colors for current system."""
if "NO_COLOR" in os.environ:
return 0
if os.environ.get("FORCE_COLOR", "0") in {"0", "1", "2", "3", "4"}:
return int(os.environ["FORCE_COLOR"])
if not sys.stdout.isatty():
return False
return 0

term = os.environ.get("TERM", "")

# Some terminals set TERM=xterm for compatibility
if term.endswith("256color") or term == "xterm":
return 3 if platform.system() == "Darwin" else 4
elif term.endswith("16color"):
if term.endswith("16color"):
return 2
elif term == "screen":
if term == "screen":
return 1
elif os.name == "nt":
if os.name == "nt":
return 0
else:
return 3

return 3


class ColorNotFound(Exception):
Expand Down
6 changes: 2 additions & 4 deletions plumbum/machines/paramiko_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
except ImportError:

class paramiko: # type: ignore
def __nonzero__(self):
def __bool__(self):
return False

__bool__ = __nonzero__

def __getattr__(self, name):
raise ImportError("No module named paramiko")

Expand Down Expand Up @@ -433,7 +431,7 @@ def _path_stat(self, fn):
except OSError as e:
if e.errno == errno.ENOENT:
return None
raise OSError(e.errno)
raise
res = StatRes(
(
st.st_mode,
Expand Down
4 changes: 1 addition & 3 deletions plumbum/path/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ class Path(str, ABC):
def __repr__(self):
return f"<{self.__class__.__name__} {str(self)}>"

def __div__(self, other):
def __truediv__(self, other):
"""Joins two paths"""
return self.join(other)

__truediv__ = __div__

def __getitem__(self, key):
if type(key) == str or isinstance(key, Path):
return self / key
Expand Down
1 change: 1 addition & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def test_session(self):
_, out, _ = sh.run("ls -a")
assert ".bashrc" in out or ".bash_profile" in out

@pytest.mark.xfail(env.PYPY, reason="PyPy sometimes fails here", strict=False)
def test_env(self):
with self._connect() as rem:
with pytest.raises(ProcessExecutionError):
Expand Down

0 comments on commit bd29e7f

Please sign in to comment.