From 3428e3f087103d0538490adb152c0a4a9f69df8b Mon Sep 17 00:00:00 2001 From: joncrall Date: Wed, 26 Aug 2020 18:36:26 -0400 Subject: [PATCH] Support for NO_COLOR --- CHANGELOG.md | 1 + xdoctest/docstr/convert_google_to_numpy.py | 38 +++++++++++++++++++--- xdoctest/utils/util_str.py | 11 ++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2207f546..36698ea0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm * The REQUIRES directive can now inspect existence or values of environment variables. * Added top-level `doctest_callable` function, which executes the doctests of a function or class. +* Support for `NO_COLOR` environment variable. ### Fixed * `IPython.embed` and `ipdb.launch_ipdb_on_exception` now correctly work from diff --git a/xdoctest/docstr/convert_google_to_numpy.py b/xdoctest/docstr/convert_google_to_numpy.py index b8ef1ff7..7acf21fd 100644 --- a/xdoctest/docstr/convert_google_to_numpy.py +++ b/xdoctest/docstr/convert_google_to_numpy.py @@ -1,6 +1,6 @@ -def convert(path_to_convert): +def convert_file_docstrings(path_to_convert, dry=True): """ path_to_convert = ub.expandpath('~/code/networkx/networkx/algorithms/isomorphism/_embeddinghelpers/balanced_sequence.py') """ @@ -48,18 +48,25 @@ def recnone(val, default): new_lines = prefix + mid + suffix new_text = '\n'.join(new_lines) - print(new_text) - import xdev - dry = 0 + # print(new_text) if dry: + import xdev print(xdev.misc.difftext(old_text, new_text, context_lines=10, colored=True)) + print('^^^ modpath = {!r}'.format(modpath)) else: ub.writeto(modpath, new_text, verbose=3) - print('^^^ modpath = {!r}'.format(modpath)) def google_to_numpy_docstr(docstr): """ + Convert a google-style docstring to a numpy-style docstring + + Args: + docstr (str): contents of ``func.__doc__`` for some ``func``, assumed + to be in google-style. + + Returns: + str: numpy style docstring """ import ubelt as ub from xdoctest.docstr import docscrape_google @@ -105,3 +112,24 @@ def google_to_numpy_docstr(docstr): new_docstr = '\n'.join(new_parts) new_docstr = new_docstr.strip('\n') return new_docstr + + +def main(): + import scriptconfig as scfg + class Config(scfg.Config): + default = { + 'src': scfg.Value(None, help='path to file to convert'), + 'dry': scfg.Value(True, help='set to false to execute'), + } + config = Config(cmdline=True) + path_to_convert = config['src'] + dry = config['dry'] + convert_file_docstrings(path_to_convert, dry=dry) + + +if __name__ == '__main__': + """ + CommandLine: + python -m xdoctest.docstr.convert_google_to_numpy --src ~/code/networkx/networkx/algorithms/isomorphism/_embeddinghelpers/balanced_sequence.py + """ + main() diff --git a/xdoctest/utils/util_str.py b/xdoctest/utils/util_str.py index 1a8cc476..22c9086c 100644 --- a/xdoctest/utils/util_str.py +++ b/xdoctest/utils/util_str.py @@ -8,9 +8,16 @@ import textwrap # import warnings import re +import os import sys +# Global state that determines if ANSI-coloring text is allowed +# (which is mainly to address non-ANSI complient windows consoles) +# complient with https://no-color.org/ +NO_COLOR = bool(os.environ.get('NO_COLOR')) + + def strip_ansi(text): r""" Removes all ansi directives from the string. @@ -63,7 +70,7 @@ def color_text(text, color): >>> assert color_text(text, 'red') == 'raw text' >>> assert color_text(text, None) == 'raw text' """ - if color is None: + if NO_COLOR or color is None: return text try: import pygments @@ -170,6 +177,8 @@ def highlight_code(text, lexer_name='python', **kwargs): >>> new_text = highlight_code(text) >>> print(new_text) """ + if NO_COLOR: + return text # Resolve extensions to languages lexer_name = { 'py': 'python',