-
Notifications
You must be signed in to change notification settings - Fork 666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect PY_COLORS variable #1533
Conversation
Nice one @ssbarnea! Any way to test this also? |
I don't know an easy way to test it for CI but you can easily run Because |
BTW, do you know a way to make the signing automated on git, the DCO stuff is a real PITA. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where in the documentation do you think we should we mention this?
feb1383
to
6735860
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just need that documentation patch and we're done. Thanks! 🚀
@decentral1se did you register @lwm as a backup account? if yes, you might want to add a status/bio there pointing to your new handle... |
molecule/logger.py
Outdated
return True | ||
if os.environ.get('PY_COLORS') == '0': | ||
return False | ||
return hasattr(file, 'isatty') and file.isatty() and \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more pythonic to use brackets for multiline expressions, using EOL escaping is discouraged in typical pythonic style guides.
Also, why do you need to check whether file.isatty()
method is present? Isn't that guaranteed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking closer I realized that you use file()
builtin which is a deprecated alias for open()
. So you basically ignore this check under Python 3 and always disable colors under it, which is probably not what you want.
Moreover, file
itself doesn't really point to any io stream: you should be checking stdout stream.
Here's what you should be doing instead:
return hasattr(file, 'isatty') and file.isatty() and \ | |
return sys.stdout.isatty() and \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will replace use of reserved file with "fh" as a file-handle is what it can receive. I wonder why our linting does not do anything about that. This is something we should fix inside "lint".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you cannot lint this because linters except for pylint do only static analysis + in some version file
builtin exists. What do you mean by fh
?
7bc29b6
to
f8a0b1b
Compare
test/unit/test_logger.py
Outdated
|
||
def test_markup_detection_tty_no(monkeypatch): | ||
monkeypatch.delenv('PY_COLORS', raising=False) | ||
assert not logger.should_do_markup(FileHandleTTY(False)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to pytest
practices, it is typical to use fixtures for such things.
You could do
@pytest.fixture
def not_tty():
return FileHandleTTY(False)
def test_markup_detection_tty_no(monkeypatch, not_tty):
molecule/logger.py
Outdated
os.environ.get('TERM') != 'dumb') | ||
|
||
|
||
def colorama_init(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pull request was moved and moved since nearly five months. Anything we can to bring it forward? |
@xoxys I'm happy to accept it once the harmful code is eliminated. I'd prefer to see some regression/integration test included as well, though. |
bdbd3ec
to
b2a837e
Compare
Yeah, apologies about that @xoxys. If you makes you feel any better, I also had to wait 5 months (and endure 99 comments!) for #1458 ... :) but it's just due transition and https://molecule.readthedocs.io/en/latest/contributing.html#move-to-red-hat etc. We're much improved lately, I think. Just needs concerns in #1533 (comment) addressed! |
@decentral1se no need to apologize for anything. You all do a great job for the open source community! I am currently moving to a new apartment so there is not much time to do something by my own. But i would love to see this feature in the next time :) |
import sys | ||
|
||
import colorama | ||
from ansible.module_utils.parsing.convert_bool import boolean as to_bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind putting this import one line above to maintain alphabetic order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 🔥 🔥
Inclined to push this on the v2.20 release! The original report talked about improving the readability of Molecule CI logs and I think that is definitely something we want sooner than later. I'll merge later on (assuming CI passes) today unless I hear otherwise. |
Allow user to define PY_COLORS=1 or 0 to force use or not use of ANSI coloring sequences. Use of this option overrided default behavior of detecting TTY status of stdout and allows users to control its use CI systems which may support ANSI but where there is no TTY. Implementation based on https://github.com/pytest-dev/py/blob/master/py/_io/terminalwriter.py#L131 which is also used by tox and many other similar tools. Fixes: ansible#1510 Signed-off-by: Sorin Sbarnea <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to adding to v2.20
This was much needed in the end! See ansible/ansible-runner#200 👍 |
Allow PY_COLORS to control coloring support.
Allow user to define PY_COLORS=1 or 0 to force use or not use of
ANSI coloring sequences.
Use of this option overrided default behavior of detecting TTY status
of stdout and allows users to control its use CI systems which
may support ANSI but where there is no TTY.
Implementation based on https://github.com/pytest-dev/py/blob/master/py/_io/terminalwriter.py#L131
which is also used by tox and many other similar tools.
Fixes: #1510