wemake-python-styleguide icon indicating copy to clipboard operation
wemake-python-styleguide copied to clipboard

Add `wemake-plain` output formatter

Open sobolevn opened this issue 4 years ago • 10 comments

It should be the same as wemake, but without colors. Why?

When I try to save the output to file, I get this:

  106:29   WPS220 Found too deep nesting: 28 > 20
  [34mtry[39;49;00m:
  ^

And I want to have this:

  106:29   WPS220 Found too deep nesting: 28 > 20
  try:
  ^

sobolevn avatar Jul 01 '20 12:07 sobolevn

I can help with this if it is still available!

ydocsgnillats avatar Aug 25 '20 18:08 ydocsgnillats

Thanks a lot, @ydocsgnillats!

sobolevn avatar Aug 25 '20 20:08 sobolevn

To make sure I'm understanding correctly, I need to make an output formatter that is similar to the existing one, but without including different colors/styling? What file is the existing output formatter in?

ydocsgnillats avatar Aug 26 '20 13:08 ydocsgnillats

I need to make an output formatter that is similar to the existing one, but without including different colors/styling?

Yes.

What file is the existing output formatter in?

formatter.py 🙂

sobolevn avatar Aug 26 '20 14:08 sobolevn

Hey @sobolevn, can I take this one?

hhsdev avatar Oct 01 '20 16:10 hhsdev

Sure! Thanks!

sobolevn avatar Oct 01 '20 18:10 sobolevn

@sobolevn I couldn't reproduce the bug. Am I missing something?

Making a test file test.py:

# test.py
1 * 0

and running:

$ flake8 --format wemake test.py > output

gives in output:


test.py

  2:1      WPS428 Found statement that has no effect
  1 * 0
  ^

  2:1      WPS345 Found meaningless number operation
  1 * 0
  ^

  2:5      WPS345 Found meaningless number operation
  1 * 0
      ^

Full list of violations and explanations:
https://wemake-python-stylegui.de/en/0.14.1/pages/usage/violations/

hhsdev avatar Oct 07 '20 07:10 hhsdev

Or we can add NO_COLOR support https://no-color.org/ Which is better in my perspective

sobolevn avatar Oct 15 '20 14:10 sobolevn

Ok, just to make sure I understand correctly,

Current behaviour:

  • Output has color codes if outputted to the terminal
  • Output doesn't have color codes if piped into another program or a file
  • Setting NO_COLOR does not change the behaviour

Desired behaviour

  • Presence of color codes depends solely on whether or not NO_COLOR environment variable is defined

Am I correct?

hhsdev avatar Oct 15 '20 19:10 hhsdev

Output has color codes if outputted to the terminal

Correct!

Output doesn't have color codes if piped into another program or a file

Well, that's complicated. Because it might be very tricky. Different shells, different terminals, different OSes, different programs, different pipe methods, etc. I cannot say that it works. But, I cannot say that it does not work.

Presence of color codes depends solely on whether or not NO_COLOR environment variable is defined

Yes, this way we would have smooth way to control the output without the need to tweak the formatter manually.

sobolevn avatar Oct 15 '20 20:10 sobolevn

Hey I'm a beginner and complete newbie. I would like to attempt and solve this issue. Can you please guide me through the process and assign me this issue?

0xsoydev avatar Sep 30 '23 18:09 0xsoydev

Sure, @0x00zer0day! You need to tweak https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/formatter.py#L52 class. My understanding of this problem has changed.

So:

  • If FORCE_COLOR env var is set to 0, do not add colored output
  • Otherwise, keep things as-is

sobolevn avatar Sep 30 '23 18:09 sobolevn

Sure, @0x00zer0day! You need to tweak https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/formatter.py#L52 class. My understanding of this problem has changed.

So:

  • If FORCE_COLOR env var is set to 0, do not add colored output
  • Otherwise, keep things as-is

Just making sure if I'm on the right track here, I need to check if the system environment variables has the FORCE_COLOR flag set to 0 to disable colored output right?

0xsoydev avatar Oct 01 '23 09:10 0xsoydev

Yes :)

sobolevn avatar Oct 01 '23 09:10 sobolevn

@sobolevn, Sorry for being a complete noob here but does this seem correct? :

class WemakeFormatter(BaseFormatter):  # noqa: WPS214
    """
    We need to format our style :term:`violations <violation>` beatifully.

    The default formatter does not allow us to do that.
    What things do we miss?

    1. Spacing, everything is just mixed up and glued together
    2. Colors and decoration, some information is easier
       to gather just with colors or underlined text
    3. Grouping, we need explicit grouping by filename
    4. Incomplete and non-informative statistics

    """
    if bool(os.environ.get('FORCE_COLOR')) is False:
        _doc_url: ClassVar[str] = DOCS_URL_TEMPLATE.format(pkg_version)

    # API:

    def after_init(self):
        """Called after the original ``init`` is used to set extra fields."""
        self._lexer = PythonLexer()
        self._formatter = TerminalFormatter()
       ...

0xsoydev avatar Oct 01 '23 10:10 0xsoydev

Nope, you should change these three functions: https://github.com/wemake-services/wemake-python-styleguide/blob/d1d6f9541b1da85f3fb547ad761b290456c623e1/wemake_python_styleguide/formatter.py#L203-L238

sobolevn avatar Oct 01 '23 10:10 sobolevn

@sobolevn , So if i understand this correctly, I should perform os.environ.get('FORCE_COLOR') check on these three functions right?

0xsoydev avatar Oct 01 '23 10:10 0xsoydev

Yes :)

sobolevn avatar Oct 01 '23 11:10 sobolevn

@sobolevn, Is this correct?


# Formatting text:
force_color_check = str(int(bool(environ.get('FORCE_COLOR'))))

def _bold(text: str) -> str:
    r"""
    Returns bold formatted text.

    >>> _bold('Hello!')
    '\x1b[1mHello!\x1b[0m'

    """
#    return '\033[1m{0}\033[0m'.format(text)
    if force_color_check == '0':
        return '\033[1m{0}\033[0m'.format(text)
    else:
        return text


def _underline(text: str) -> str:
    r"""
    Returns underlined formatted text.

    >>> _underline('Hello!')
    '\x1b[4mHello!\x1b[0m'

    """
    if force_color_check == '0':
        return '\033[4m{0}\033[0m'.format(text)
    else:
        return text


def _highlight(source: str, lexer, formatter) -> str:
    """
    Highlights source code. Might fail.

    See also:
        https://github.com/wemake-services/wemake-python-styleguide/issues/794

    """
    if force_color_check == '0':
        try:
            return highlight(source, lexer, formatter)
        except Exception:  # pragma: no cover
            # Might fail on some systems, when colors are set incorrectly,
            # or not available at all. In this case code will be just text.
            return source
    else:
        return source


0xsoydev avatar Oct 01 '23 11:10 0xsoydev

_DISABLE_COLORS: Final = environ.get('FORCE_COLOR', '1') == '0'

def _bold(text: str) -> str:
    if _DISABLE_COLORS:
         return text
    return ...

sobolevn avatar Oct 01 '23 11:10 sobolevn

@sobolevn pardon my sloppy code. Have opened up a PR, please do check if I have done anything wrong

0xsoydev avatar Oct 01 '23 12:10 0xsoydev