Skip to content

Commit

Permalink
Support for NO_COLOR
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Aug 26, 2020
1 parent c15eec9 commit 3428e3f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 33 additions & 5 deletions xdoctest/docstr/convert_google_to_numpy.py
Original file line number Diff line number Diff line change
@@ -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')
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
11 changes: 10 additions & 1 deletion xdoctest/utils/util_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 3428e3f

Please sign in to comment.