diff --git a/hive/conf.py b/hive/conf.py index 65ae207377533e9166d933e1c363063fc17996ce..37cbf99b418cc13bcd4a930392c56004b95b86cf 100644 --- a/hive/conf.py +++ b/hive/conf.py @@ -5,21 +5,7 @@ import configargparse from hive.steem.client import SteemClient from hive.db.adapter import Db - -def strtobool(val): - """Convert a booleany str to a bool. - - True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values - are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if - 'val' is anything else. - """ - val = val.lower() - if val in ('y', 'yes', 't', 'true', 'on', '1'): - return True - elif val in ('n', 'no', 'f', 'false', 'off', '0'): - return False - else: - raise ValueError("not booleany: %r" % (val,)) +from hive.utils.normalize import strtobool class Conf(): """ Manages sync/server configuration via args, ENVs, and hive.conf. """ diff --git a/hive/utils/normalize.py b/hive/utils/normalize.py index 118e3fe5e7d4bfa8862715766fb21f0c33dc28c1..b1bd7b3bd6965a4579d31edbc3794cc33bad3aaf 100644 --- a/hive/utils/normalize.py +++ b/hive/utils/normalize.py @@ -140,3 +140,18 @@ def safe_img_url(url, max_size=1024): and url[0:4] == 'http'): return url.strip() return None + +def strtobool(val): + """Convert a booleany str to a bool. + + True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values + are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if + 'val' is anything else. + """ + val = val.lower() + if val in ('y', 'yes', 't', 'true', 'on', '1'): + return True + elif val in ('n', 'no', 'f', 'false', 'off', '0'): + return False + else: + raise ValueError("not booleany: %r" % (val,)) diff --git a/hive/utils/system.py b/hive/utils/system.py index d6eed13f2f28ae8889c5bc90ed4e4166a34cb5f4..21982a8e4ecc873036221f7ca48558d6dd008cd0 100644 --- a/hive/utils/system.py +++ b/hive/utils/system.py @@ -5,9 +5,9 @@ import resource USE_COLOR = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() -def colorize(string, color='93'): +def colorize(string, color='93', force=False): """Colorizes a string for stdout, if attached to terminal""" - if not USE_COLOR: + if not USE_COLOR and not force: return string return "\033[%sm%s\033[0m" % (color, string) diff --git a/tests/utils/test_utils_normalize.py b/tests/utils/test_utils_normalize.py index a71435e2344c1440ee192c40f87ab00dbe86a778..0d139a81d44a06fd15bc87c122c57eb4c05a3212 100644 --- a/tests/utils/test_utils_normalize.py +++ b/tests/utils/test_utils_normalize.py @@ -1,4 +1,6 @@ #pylint: disable=missing-docstring +import pytest + from datetime import datetime from decimal import Decimal @@ -18,6 +20,7 @@ from hive.utils.normalize import ( rep_log10, safe_img_url, secs_to_str, + strtobool, ) def test_secs_to_str(): @@ -85,3 +88,20 @@ def test_safe_img_url(): max_size = len(url) + 1 assert safe_img_url(url, max_size) == url assert safe_img_url(url + 'x', max_size) is None + +def test_strtobool(): + assert strtobool('t') == True + assert strtobool('T') == True + assert strtobool('1') == True + assert strtobool('true') == True + assert strtobool('yes') == True + + assert strtobool('f') == False + assert strtobool('F') == False + assert strtobool('0') == False + assert strtobool('false') == False + assert strtobool('n') == False + assert strtobool('no') == False + + with pytest.raises(ValueError): + strtobool('foo') diff --git a/tests/utils/test_utils_profiler.py b/tests/utils/test_utils_profiler.py new file mode 100644 index 0000000000000000000000000000000000000000..2e01f1be8ced5a377e60bc7abcdbf0aeffec1164 --- /dev/null +++ b/tests/utils/test_utils_profiler.py @@ -0,0 +1,15 @@ +#pylint: disable=missing-docstring,expression-not-assigned +from hive.utils.profiler import Profiler + +def test_profiler(): + p = Profiler('.tmp.test-prof') + with p: + [i for i in range(100000)] + p.save() + p.echo() + +def test_profiler_passthru(): + p = Profiler(None) + with p: + [i for i in range(100000)] + p.echo() diff --git a/tests/utils/test_utils_system.py b/tests/utils/test_utils_system.py new file mode 100644 index 0000000000000000000000000000000000000000..e0ea1bbbdfbb0633b6fcc1f7c5ddf46da7797491 --- /dev/null +++ b/tests/utils/test_utils_system.py @@ -0,0 +1,14 @@ +#pylint: disable=missing-docstring +from hive.utils.system import ( + colorize, + peak_usage_mb, +) + +def test_colorize(): + plain = 'teststr' + colored = '\x1b[93mteststr\x1b[0m' + assert colorize(plain, color='93') in [plain, colored] + assert colorize(plain, color='93', force=True) == colored + +def test_peak_usage_mb(): + assert peak_usage_mb() > 1