Skip to content
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

Merged
merged 1 commit into from
Mar 5, 2019
Merged

Respect PY_COLORS variable #1533

merged 1 commit into from
Mar 5, 2019

Conversation

ssbarnea
Copy link
Member

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

@decentral1se
Copy link
Contributor

Nice one @ssbarnea! Any way to test this also?

@ssbarnea
Copy link
Member Author

I don't know an easy way to test it for CI but you can easily run PY_COLOR=0 molecule test, also try with PY_COLOR=1 molecule test &>mo.log and it should be enough.

Because --help does not have any coloring is not a good way of testing it. I will try to rebase my change to get rid of the unrelated change and bring it closer to mergeable.

@ssbarnea
Copy link
Member Author

BTW, do you know a way to make the signing automated on git, the DCO stuff is a real PITA.

Copy link
Contributor

@gundalow gundalow left a 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?

molecule/__main__.py Outdated Show resolved Hide resolved
molecule/logger.py Outdated Show resolved Hide resolved
@ssbarnea ssbarnea changed the title Repect PY_COLOR variable Respect PY_COLOR variable Nov 4, 2018
@ssbarnea ssbarnea force-pushed the color branch 2 times, most recently from feb1383 to 6735860 Compare November 9, 2018 16:57
@ssbarnea ssbarnea changed the title Respect PY_COLOR variable Respect PY_COLORS variable Nov 9, 2018
@decentral1se decentral1se added this to the v.2.21 milestone Jan 31, 2019
@themr0c
Copy link
Contributor

themr0c commented Feb 21, 2019

I believe we can move forward now, what you thing @lwm and @gundalow ?

Copy link
Contributor

@decentral1se decentral1se left a 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! 🚀

@webknjaz
Copy link
Member

@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 Show resolved Hide resolved
molecule/logger.py Outdated Show resolved Hide resolved
return True
if os.environ.get('PY_COLORS') == '0':
return False
return hasattr(file, 'isatty') and file.isatty() and \
Copy link
Member

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?

Copy link
Member

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:

Suggested change
return hasattr(file, 'isatty') and file.isatty() and \
return sys.stdout.isatty() and \

Copy link
Member Author

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".

Copy link
Member

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?

test/unit/test_logger.py Outdated Show resolved Hide resolved
test/unit/test_logger.py Outdated Show resolved Hide resolved
test/unit/test_logger.py Outdated Show resolved Hide resolved
test/unit/test_logger.py Outdated Show resolved Hide resolved
@ssbarnea ssbarnea force-pushed the color branch 3 times, most recently from 7bc29b6 to f8a0b1b Compare February 27, 2019 15:57
test/unit/test_logger.py Outdated Show resolved Hide resolved

def test_markup_detection_tty_no(monkeypatch):
monkeypatch.delenv('PY_COLORS', raising=False)
assert not logger.should_do_markup(FileHandleTTY(False))
Copy link
Member

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):

os.environ.get('TERM') != 'dumb')


def colorama_init():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test this method in conjunction with the other one, please?
Because currently it looks like

molecule/logger.py Outdated Show resolved Hide resolved
@xoxys
Copy link

xoxys commented Feb 28, 2019

This pull request was moved and moved since nearly five months. Anything we can to bring it forward?

@webknjaz
Copy link
Member

webknjaz commented Mar 1, 2019

@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.

@decentral1se
Copy link
Contributor

This pull request was moved and moved since nearly five months.
Anything we can to bring it forward?

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!

@xoxys
Copy link

xoxys commented Mar 3, 2019

@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
Copy link
Member

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?

@webknjaz webknjaz requested a review from decentral1se March 4, 2019 09:22
@webknjaz webknjaz dismissed decentral1se’s stale review March 4, 2019 09:23

request fulfilled

Copy link
Contributor

@decentral1se decentral1se left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 🔥 🔥

@decentral1se decentral1se added the test Improvement to quality assurance: CI/CD, testing, building label Mar 4, 2019
@decentral1se
Copy link
Contributor

decentral1se commented Mar 4, 2019

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]>
Copy link
Contributor

@gundalow gundalow left a 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

@decentral1se
Copy link
Contributor

This was much needed in the end! See ansible/ansible-runner#200 👍

@ssbarnea ssbarnea deleted the color branch October 19, 2019 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs enhancement test Improvement to quality assurance: CI/CD, testing, building
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants