Skip to content
This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
/ accelpy Public archive

Commit

Permalink
1.0.0-beta.23
Browse files Browse the repository at this point in the history
  • Loading branch information
JGoutin committed Aug 20, 2019
1 parent 259a034 commit 7def58c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 24 deletions.
2 changes: 1 addition & 1 deletion accelpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
__version__ = '1.0.0-beta.22'
__version__ = '1.0.0-beta.23'
__copyright__ = "Copyright 2019 Accelize"
__licence__ = "Apache 2.0"

Expand Down
25 changes: 8 additions & 17 deletions accelpy/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
expanduser as _expanduser, isdir as _isdir, realpath as _realpath,
join as _join, dirname as _dirname, basename as _basename,
isfile as _isfile, splitext as _splitext)
from platform import system as _system
from subprocess import run as _run, PIPE as _PIPE
from time import time as _time

Expand All @@ -23,13 +24,15 @@
HOME_DIR = _expanduser('~/.accelize')

# Cached values storage
_cache = dict()
CACHE_DIR = _join(HOME_DIR, '.cache')

# Ensure directory exists and have restricted access rights
_makesdirs(CACHE_DIR, exist_ok=True)
_chmod(HOME_DIR, 0o700)

# ANSI shell colors
_COLORS = dict(RED=31, GREEN=32, YELLOW=33, BLUE=34, PINK=35, CYAN=36, GREY=37)


def json_read(path, **kwargs):
"""
Expand Down Expand Up @@ -241,7 +244,8 @@ def no_color():
Returns:
bool: True if no color mode.
"""
return bool(_environ.get("ACCELPY_NO_COLOR", False))
return (bool(_environ.get("ACCELPY_NO_COLOR", False)) and
_system() != 'Windows')


def debug():
Expand Down Expand Up @@ -275,23 +279,10 @@ def color_str(text, color):
Returns:
str: Colored text.
"""
# Disable color output if not in CLI mode or if color is disabled
if not is_cli() or no_color():
# Disable color output if not in CLI mode or if color is disabled
return text

# Lazy import colorama only if required and skip if not available.
try:
from colorama import init, Fore
except ImportError: # pragma: no cover
return text

# Init colorama if not already done
if not _cache.get('colorama_initialized'):
init()
_cache['colorama_initialized'] = True

# Return colored output
return f'{getattr(Fore, color)}{text}{Fore.RESET}'
return f'\033[{_COLORS[color]}m{text}\033[30m'


def error(text):
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
'requests>=2.20.0',
'ansible>=2.8',
'argcomplete>=1.10',
# 'colorama', Let dependency version be managed by "awscli"
# 'jinja2', Let dependency version be managed by "ansible"
# 'mitogen>=0.2.7',
'awscli>=1.16' # To remove once Terraform support spot instance tagging
Expand Down
6 changes: 1 addition & 5 deletions tests/test_core_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,7 @@ def no_color():

# Test: ClI enabled
is_cli = True
assert 'test' in color_str('test', 'RED')

# Test: Colorama already initialized
assert common._cache.get('colorama_initialized')
assert 'test' in color_str('test', 'RED')
assert color_str('test', 'RED') == '\033[31mtest\033[30m'

# Restore mocked functions
finally:
Expand Down

0 comments on commit 7def58c

Please sign in to comment.